Advertisement
Guest User

move completed torrents to correct tv folder (vb)

a guest
Oct 24th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dim skiplist, slist, directorylist, dlist, shares, tmp, found, dsplit, myloc, i, j, k
  2. dim skip, yr, dirmatch, t, fileFrom, fileTo, copyCommand, filesys, oshellapp, kAdded
  3. dim root
  4.  
  5. '' if you want to see the log and pause on completion
  6. const debuggy = true
  7.  
  8. root = "\\QNAP\Qdownload\transmission\completed\"
  9.  
  10. Set oShellApp = WScript.CreateObject("WScript.Shell")
  11.  
  12. set filesys = CreateObject("Scripting.FileSystemObject")
  13. t = timer
  14. logmsg "process begun"
  15.  
  16. '' the various folders where tv shows are stored
  17. shares = Split("\\pvr\c\,\\pvr\d\tv\,\\pvr\e\tv\",",")
  18.  
  19. if filesys.FileExists(root & "skip.list") then
  20.     set tmp = filesys.OpenTextFile(root & "skip.list", 1, false)
  21.     slist = tmp.ReadAll
  22.     skiplist = split(trim("" & slist), vbNewLine)
  23.     tmp.close
  24. else
  25.     Set tmp = filesys.CreateTextFile(root & "skip.list", True)
  26.     tmp.writeline "run.vbs"
  27.     tmp.writeline "directory.list"
  28.     tmp.writeline "skip.list"
  29.     tmp.close
  30.     redim preserve skiplist(3)
  31.     slist = tmp.ReadAll
  32.     skiplist = split(trim("" & slist),vbNewLine)
  33.     tmp.close
  34. end if
  35.  
  36. '' make an array of all the directories we know about and their shortname to make matching easier
  37. k = 0
  38. kAdded = false
  39. for i = 0 to ubound(shares)
  40.     set fold = filesys.getfolder(shares(i))
  41.     for each fol in fold.subfolders
  42.         k = k + 1
  43.     next
  44. next
  45. redim directorylist(k)
  46. k = 0
  47. for i = 0 to ubound(shares)
  48.     set fold = filesys.getfolder(shares(i))
  49.     for each fol in fold.subfolders
  50.         directorylist(k) = shares(i) & fol.name & ":" & cleanDir(fol.name)
  51.         k = k + 1
  52.     next
  53. next
  54. logmsg "finished precaching " & ubound(directorylist) & " destinations"
  55.  
  56. '' process the files in source to find their matching directories, if they exist
  57. set myloc = filesys.getfolder(root)
  58. for each fil in myloc.files
  59.     skip = false
  60.     for j = 0 to ubound(skiplist)
  61.         if lcase(fil.name) = lcase(skiplist(0)) or left(fil.name, 1) = "." or trim(fil.name) = "" then
  62.             skip = true
  63.             exit for
  64.         end if
  65.     next
  66.     if not skip then
  67.         yr = ""
  68.         sname = ""
  69.         name = getname(fil.name, sname, yr)
  70.         if name > " " then
  71.             dirmatch = ""
  72.             for j = 0 to ubound(directorylist)
  73.                 if instr(directorylist(j),":") > 0 then
  74.                     dsplit = split(directorylist(j),":")
  75.                     ' some shows have (2011) etc after their folder name for tvdb matching reasons
  76.                     if dsplit(1) = trim(name & yr) or dsplit(1) = trim(name) then
  77.                         dirmatch = dsplit(0)
  78.                         exit for
  79.                     end if
  80.                 end if
  81.             next
  82.             if dirmatch > "" then
  83.                 fileFrom = quote(unicodeToAscii(myloc)) ' wrap quotes around the path, if needed
  84.                 fileTo = quote(unicodeToAscii(dirmatch)) ' remove non-printing characters
  85.                 ' copyCommand = "%comspec% /k xcopy " & fileFrom & " " & fileTo & " /C /D /Y" ' returns "parse error"
  86.                 ' copyCommand = "%comspec% /k xcopy " & fileFrom & " " & fileTo & " /C /D /Y" ' returns "parse error"
  87.                 ' copyCommand = "%comspec% /k robocopy " & fileFrom & " " & fileTo & " " & fil.name & " /R:3 /W:10"' /MOV"
  88.                 copyCommand = "%comspec% /k robocopy " & fileFrom & " " & fileTo & " " & fil.name & " /R:3 /W:10 /MOV"
  89.                 logmsg copyCommand
  90.                 ' oShellApp.run copyCommand
  91.             else
  92.                 logmsg "no matching directory found for: " & fil.name
  93.             end if
  94.         else
  95.             logmsg "file not processed: " & name & " (" & fil.name & ")"
  96.         end if
  97.     end if
  98. next
  99.  
  100. logmsg "process complete"
  101.  
  102. if debuggy then
  103.     wscript.echo "press return to quit"
  104.     foo = WScript.StdIn.ReadLine
  105. end if
  106.  
  107. ' --------------------------------------------------------------------------------------------------------
  108.  
  109. '' if a string has a space, put double quotes around it
  110. function quote(byval msg)
  111.     if instr(msg," ") > 0 then
  112.         quote = chr(34) & msg & chr(34)
  113.     else
  114.         quote = msg
  115.     end if
  116. end function
  117.  
  118. '' turn
  119. ''  666.Park.Avenue.(2012).S01E04.HDTV.x264-LOL.[VTV].mp4
  120. '' into
  121. ''  666parkavenue
  122. function getname(byval name, byref spaceName, byref yeer)
  123. Dim ret, rex, s, z, c, q
  124.     spaceName = ""
  125.     yeer = ""
  126.     q = ""
  127.  
  128.     ret = lcase(name)
  129.     set rex = new regexp
  130.     rex.global = true
  131.  
  132.     ' replace name.name.2012.s01e02 with name.name.s01e02
  133.     rex.pattern = "[.]\d{4}[.]"
  134.     set matches = rex.execute(ret)
  135.     if matches.count > 0 then
  136.         yeer = replace(matches(0).value, ".", "")
  137.     end if
  138.     ret = rex.replace(ret,".")
  139.  
  140.     ' replace .US. and .UK. with .
  141.     rex.pattern = "[.]u[sk][.]"
  142.     ret = rex.replace(ret,".")
  143.  
  144.     ' get name before season/episode
  145.     rex.pattern = "[s]\d{2}[e]\d{2}"
  146.     if rex.test(ret) then
  147.         s = left(ret, instr(ret, ".s"))
  148.         spl = split(replace(s,".", " ")," ")
  149.         for z = 0 to ubound(spl)
  150.             spaceName = spaceName & ucase(left(spl(z),1)) & mid(spl(z),2) & " "
  151.         next
  152.         spaceName = trim(spaceName)
  153.         getname = replace(s, ".","")
  154.     end if
  155.     set rex = nothing
  156. end function
  157.  
  158. function cleanDir(byval name)
  159. Dim ret, rex
  160.     ret = name
  161.     set rex = new regexp
  162.     rex.global = true
  163.     rex.pattern = "[(]\d{4}[)]" ' date
  164.     ret = rex.replace(ret,"")
  165.     rex.pattern = "[^a-zA-Z0-9]" ' alphanumeric
  166.     ret = rex.replace(ret,"")
  167.     cleanDir = lcase(ret)
  168.     set rex = nothing
  169. end function
  170.  
  171. sub logmsg(msg)
  172.     if debuggy then wscript.echo hhmmss(Timer - t) & ": " & msg
  173. end sub
  174.  
  175. Function hhmmss(byval i)
  176. Dim hr, min, sec, remainder
  177.  
  178.     elap = Int(i) 'Just use the INTeger portion of the variable
  179.     hr = elap \ 3600 '1 hour = 3600 seconds
  180.     remainder = elap - hr * 3600
  181.     min = remainder \ 60
  182.     remainder = remainder - min * 60
  183.     sec = cint(0 + remainder)
  184.     min = right("00" & min,2)
  185.     sec = right("00" & sec,2)
  186.     If hr = 0 Then
  187.         hhmmss = min & ":" & sec
  188.     Else
  189.         hhmmss = hr & ":" & min & ":" & sec
  190.     End If
  191. End Function
  192.  
  193. '' handle if fso has been reading unicode encodings for filenames
  194. '' e.g. source is on a linux box with smb
  195. Function unicodeToAscii(sText)
  196.   Dim x, aAscii, ascval, l
  197.   l = len(sText)
  198.   If l = 0 Then Exit Function
  199.   redim aAscii(l)
  200.   For x = 1 To l
  201.     ascval = AscW(Mid(sText, x, 1))
  202.     If (ascval < 0) Then
  203.       ascval = 65536 + ascval ' http://support.microsoft.com/kb/272138
  204.    End If
  205.     aAscii(x) = chr(ascval)
  206.   Next
  207.   unicodeToAscii = join(aAscii,"")
  208. End Function
  209.  
  210. set filesys = nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement