lørdag 22. september 2012

Make µTorrent execute ftp script to upload files back to your home computer

Not a Powershell script this time, but still a useful one. Let's say you have a MS Windows based server and µTorrent running. How to get the files back to your home computer as fast as possible?

I'm usually more interested in event based programming/scripting, then schedule based. I had some requirements:
  • Command line based, 
  • All info must be passable in a single command
  • Parallel transfers to take full use of bandwidth.
A cygwin compiled lftp was my solution. Along with a bit of VB Scripting to take care of some syntax issues, I arrived with the script below. It supports single file torrents as well as multifile torrents.

Run this program when a torrent finishes:
C:\Utils\Scripts\ftp.vbs %K "%D" "%N" %L "%F"

ftp.vbs:

Dim path,label,cmd,nm,file,tp,host,user,pass,dir,drive,login,lftp,wd, context
host = "myHostOrIP"
user = "myUsername"
pass = "myPassword"
login = user & ":" & pass & "@" & host
lftp = "C:\Utils\lftp\lftp.exe"
wd = "C:\"

tp = WScript.Arguments.Item(0) 'single/multi
dir = WScript.Arguments.Item(1)
drive = LCase(Mid(dir, 1, 1))
path = "/cygdrive/" & drive & Right(dir,Len(dir)-2)
path = Replace(Replace(path,"\","/")," ","\ ")
nm = Replace(Replace(WScript.Arguments.Item(2),"\","/")," ","\ ")
file = path & "/" & Replace(Replace(WScript.Arguments.Item(4),"\","/")," ","\ ")
label = WScript.Arguments.Item(3)

If tp = "single" Then
context = "cd /" & label & "; put " & file
Else
'multifile
context = "mirror -R -v --parallel=3 --depth-first " & path & " /" & label & "/" & nm
End If

cmd = "ftp://" & login & " -e " & chr(34) & "set ftp:nop-interval 1; set net:timeout 5; "& context & "; quit" & chr(34)

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute lftp, cmd, wd, "runas", 1
Set objShell = Nothing


Note: This solution requires that you have a FTP Server running on your home computer. FileZilla for example.

Ingen kommentarer:

Legg inn en kommentar