Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dim skiplist, slist, directorylist, dlist, shares, tmp, found, dsplit, myloc
- dim skip, yr, dirmatch, t, fileFrom, fileTo, copyCommand, filesys, oshellapp
- dim root
- root = "\\QNAP\Qdownload\transmission\completed\"
- Set oShellApp = WScript.CreateObject("WScript.Shell")
- set filesys = CreateObject("Scripting.FileSystemObject")
- t = timer
- logmsg "process begun"
- ' the various folders where tv shows are stored
- shares = Split("\\pvr\c\,\\pvr\d\tv\,\\pvr\e\tv\",",")
- if filesys.FileExists(root & "directory.list") then
- set tmp = filesys.OpenTextFile(root & "directory.list", 1, false)
- dlist = tmp.ReadAll
- directorylist = split(dlist, chr(13))
- tmp.close
- else
- Set tmp = filesys.CreateTextFile(root & "directory.list", True)
- tmp.writeline ""
- tmp.close
- redim directorylist(1)
- end if
- if filesys.FileExists(root & "skip.list") then
- set tmp = filesys.OpenTextFile(root & "skip.list", 1, false)
- slist = tmp.ReadAll
- skiplist = split(trim("" & slist), chr(13))
- tmp.close
- else
- Set tmp = filesys.CreateTextFile(root & "skip.list", True)
- tmp.writeline "run.vbs"
- tmp.writeline "directory.list"
- tmp.writeline "skip.list"
- tmp.close
- redim preserve skiplist(3)
- slist = tmp.ReadAll
- skiplist = split(trim("" & slist), chr(13))
- tmp.close
- end if
- set dirlist = filesys.opentextfile(root & "directory.list", 8, true)
- for i = 0 to ubound(shares)
- set fold = filesys.getfolder(shares(i))
- for each fol in fold.subfolders
- found = false
- for j = 0 to ubound(directorylist)
- if instr(directorylist(j),":") > 0 then
- dsplit = split(directorylist(j),":")
- if cleanDir(fol.name) = lcase(dsplit(1)) then
- found = true
- exit for
- end if
- end if
- next
- if not found then
- dirlist.writeline shares(i) & fol.name & ":" & cleanDir(fol.name)
- directorylist(ubound(directorylist)) = shares(i) & fol.name & ":" & cleanDir(fol.name)
- redim preserve directorylist(ubound(directorylist)+1) ' horrible, works, inefficient as hell, works
- end if
- next
- next
- dirlist.close
- logmsg "finished processing directories"
- set myloc = filesys.getfolder(root)
- for each fil in myloc.files
- skip = false
- for j = 0 to ubound(skiplist)
- if lcase(fil.name) = lcase(skiplist(0)) or left(fil.name, 1) = "." or trim(fil.name & "") = "" then
- skip = true
- exit for
- end if
- next
- if not skip then
- yr = ""
- sname = ""
- name = getname(fil.name, sname, yr)
- if name > " " then
- dirmatch = ""
- for j = 0 to ubound(directorylist)
- if instr(directorylist(j),":") > 0 then
- dsplit = split(directorylist(j),":")
- ' some shows have (2011) etc after their name for tvdb matching reasons
- if dsplit(1) = trim(name & yr) or dsplit(1) = trim(name) then
- dirmatch = dsplit(0)
- exit for
- end if
- end if
- next
- if dirmatch > "" then
- fileFrom = quote(myloc) ' & fil.name) 'fil.name will not need to be quoted
- fileTo = quote(dirmatch)
- ' copyCommand = "%comspec% /k xcopy " & fileFrom & " " & fileTo & " /C /D /Y" ' returns "parse error"
- copyCommand = "%comspec% /k robocopy " & fileFrom & " " & fileTo & " " & fil.name & " /R:3 /W:10"' /MOV"
- logmsg copyCommand
- oShellApp.run copyCommand
- else
- logmsg "no matching directory found for: " & fil.name
- end if
- else
- logmsg "file not processed: " & name & " (" & fil.name & ")"
- end if
- end if
- next
- logmsg "process complete"
- wscript.echo "press return to quit"
- foo = WScript.StdIn.ReadLine
- function quote(byval msg)
- if instr(msg," ") > 0 then
- quote = chr(34) & msg & chr(34)
- else
- quote = msg
- end if
- end function
- function getname(byval name, byref spaceName, byref yeer)
- Dim ret, rex, s, z, c, q
- spaceName = ""
- yeer = ""
- q = ""
- ret = lcase(name)
- set rex = new regexp
- rex.global = true
- ' replace name.name.2012.s01e02 with name.name.s01e02
- rex.pattern = "[.]\d{4}[.]"
- set matches = rex.execute(ret)
- if matches.count > 0 then
- yeer = replace(matches(0).value, ".", "")
- end if
- ret = rex.replace(ret,".")
- ' replace .US. and .UK. with .
- rex.pattern = "[.]u[sk][.]"
- ret = rex.replace(ret,".")
- ' get name before season/episode
- rex.pattern = "[s]\d{2}[e]\d{2}"
- if rex.test(ret) then
- s = left(ret, instr(ret, ".s"))
- spl = split(replace(s,".", " ")," ")
- for z = 0 to ubound(spl)
- spaceName = spaceName & ucase(left(spl(z),1)) & mid(spl(z),2) & " "
- next
- spaceName = trim(spaceName)
- getname = replace(s, ".","")
- end if
- set rex = nothing
- end function
- function cleanDir(byval name)
- Dim ret, rex
- ret = name
- set rex = new regexp
- rex.global = true
- rex.pattern = "[^a-zA-Z0-9]"
- ret = rex.replace(ret,"")
- rex.pattern = "[(]\d{4}[)]"
- ret = rex.replace(ret,"")
- cleanDir = lcase(ret)
- set rex = nothing
- end function
- sub logmsg(msg)
- wscript.echo PrintHrMinSec(Timer - t) & ": " & msg
- wscript.echo ""
- end sub
- Function PrintHrMinSec(elap)
- Dim hr, min, sec, remainder
- elap = Int(elap) 'Just use the INTeger portion of the variable
- hr = elap \ 3600 '1 hour = 3600 seconds
- remainder = elap - hr * 3600
- min = remainder \ 60
- remainder = remainder - min * 60
- sec = remainder
- min = right("00" & min,2)
- sec = right("00" & sec,2)
- If hr = 0 Then
- PrintHrMinSec = min & ":" & sec
- Else
- PrintHrMinSec = hr & ":" & min & ":" & sec
- End If
- End Function
- set filesys = nothing
Add Comment
Please, Sign In to add comment