Blog Pro de Jean-Baptiste HEREN - Tag - windowsNotes d'un consultant Freelance en Informatique2015-11-03T21:34:19+01:00JB HERENurn:md5:e39389b5ec134d99645112fce3d957dfDotclearautomatic ftp tranfers on windows OS client using the command lineurn:md5:095889337a165f98a9556881fb9ade072011-06-27T20:55:00+02:002011-12-02T11:07:36+01:00Jean-Baptiste HerenVeille technobatchChilkat FTPcommand lineftpvbswindowswinSCP<p>Here is a little trick we used on a recent project.</p>
<p>We needed to automatize :</p>
<ol>
<li>(local <-- remote) download files from an ftp server specified folder</li>
<li>(remote --> remote/subfolder) move the downloaded files on the source server so that we do not download them again on next run.</li>
</ol> <h2>Needed Tools</h2>
<h3>ftp Client</h3>
<p>I decided to use the WINSCP Command wich is part of the WinSCP client package. This tool works as a command line client and is scriptable.
It is possible also to use the ftp command directly but it does not work properly whan you want to send ftp commands on the fly.</p>
<h3>Scripting language</h3>
<p>The following example uses Visual basic scripting.</p>
<h2>The Script</h2>
<p>After creating a new conenction in WINSCP client, we can use it on command line by running the following script :</p>
<pre>[script]
cscript move.vbs D:\FTPROOT\IN\LOCAL IN/FOO OUT/FOO DONE MYACCOUNT
</pre>
<pre>[VB]
'contact@jbheren.com
'this code builds a move command from the file list in specified directory & runs it in WSCP
'usage: cscript move.vbs source_local_full_path remote_initial_directory remote_final_subdirectory
Dim fso, folder, files, localPath, localFolder, remoteFolder, moveSubFolder, connexionName, oShell, oExec, winscp
' Check parameters
If( WScript.Arguments.Count < 4 ) Then
WScript.Echo( vbCrLf &" usage: cscript move.vbs source_local_full_path local_ftp_path remote_ftp_path move_to_path connection_name")
WScript.Echo( vbCrLf &" example: " &vbCrLf &" cscript move.vbs D:\FTPROOT\IN\LOCAL IN/FOO OUT/FOO DONE MYACCOUNT")
WScript.Echo( vbCrLf &" " &vbCrLf &" 1 - Gets files from OUT/FOO -> IN/FOO using the recorded MYACCOUNT WINSCP connection")
WScript.Echo( vbCrLf &" " &vbCrLf &" 2 - moves files names found in D:\FTPROOT\IN\LOCAL, on remote from OUT/FOO -> OUT/FOO/DONE")
WScript.Quit
End If
' -------------------------------------------
'| MAIN |
' -------------------------------------------
' Set Winscp com path
winscp = "C:\Program Files\WinSCP\winscp.com"
commandTxt = "" ' Empty current command
Set fso = CreateObject("Scripting.FileSystemObject")
' retreive parameters
localPath = Wscript.Arguments.Item(0)
localFolder = Wscript.Arguments.Item(1)
remoteFolder = Wscript.Arguments.Item(2)
moveSubFolder = Wscript.Arguments.Item(3)
connexionName = Wscript.Arguments.Item(4)
If localPath = "" Then
Wscript.Echo "No Folder parameter was passed"
Wscript.Quit
Else
Wscript.Echo "Runing For " & localPath & " , " & localFolder & " , " & remoteFolder & " , " & moveSubFolder & " on connection " & connexionName
End If
' Open ftp command
Set oShell = CreateObject("wscript.shell")
'Set oExec = oShell.Exec("ftpgenerix.bat")
Set oExec = oShell.Exec(winscp)
' Build the list of Commands to be ran by FTP command.
' login
oExec.StdIn.Writeline "option batch abort"
WScript.Sleep 100
oExec.StdIn.Writeline "open " & connexionName
WScript.Sleep 100
' 00 - Set Current Folders
oExec.StdIn.Writeline("cd " & remoteFolder)
WScript.Sleep 100
oExec.StdIn.Writeline("lcd " & localFolder)
WScript.Sleep 100
' 01 - DOWNLOAD
oExec.StdIn.Writeline("get *.*")
Wscript.Echo "Getting All files from " & remoteFolder & " to " & localFolder
WScript.Sleep 1000
' 02 - RENAME (Move files)
' list current local files
Set folder = fso.GetFolder(localPath)
Set files = folder.Files
' move each one on distant server from current folder to dFolder
For each file In files
commandTxt = "rename " & file.Name & " " & moveSubFolder & "/" & file.Name
oExec.StdIn.Writeline(commandTxt)
Wscript.Echo commandTxt
WScript.Sleep 100
Next
' Finish session
oExec.StdIn.Writeline "exit"
Wscript.Echo "exit"
WScript.Sleep 100
WScript.Echo oExec.Status
</pre>
<p>Hope it will help !</p>
<h2>Alternatives to WinSCP</h2>
<p>I just reworked on this task (same actions) and wrote a vbs script doing the same task, but replaced winscp with an ActiveX control object : <a href="http://www.chilkatsoft.com/ftp-activex.asp" hreflang="en" title="http://www.chilkatsoft.com/ftp-activex.asp">Chilkat FTP ActiveX</a></p>
<p>Here is the code</p>
<pre>[VB]
'moveocx.vbs
'contact@jbheren.com
'this code builds a move command from the file list in specified directory & runs it FTP Session
'uses ActiveX component from http://www.chilkatsoft.com/ftp-activex.asp
'usage: cscript move.vbs source_local_full_path remote_initial_directory remote_final_subdirectory
Dim fso, ftpo, folder, files, localPath, localFolder, remoteFolder, moveSubFolder, host, user, pass, ok
' Check parameters
If( WScript.Arguments.Count < 4 ) Then
WScript.Echo( vbCrLf &" usage: cscript moveocx.vbs source_local_full_path local_ftp_path remote_ftp_path move_to_path user pass host")
WScript.Echo( vbCrLf &" example: " &vbCrLf &" cscript moveocx.vbs D:\FTPROOT\IN\LOCAL IN/FOO OUT/FOO DONE MYLOGIN MYPASS MYHOST")
WScript.Echo( vbCrLf &" " &vbCrLf &" 1 - Gets files from OUT/FOO -> IN/FOO using the recorded MYLOGIN connection")
WScript.Echo( vbCrLf &" " &vbCrLf &" 2 - moves files names found in D:\FTPROOT\IN\LOCAL, on remote from OUT/FOO -> OUT/FOO/DONE")
WScript.Quit
End If
' -------------------------------------------
'| MAIN |
' -------------------------------------------
' Set Basic path
commandTxt = "" ' Empty current command
' instanciate chilkat FTP Component
Set fso = CreateObject("Scripting.FileSystemObject")
' retreive parameters
localPath = Wscript.Arguments.Item(0)
localFolder = Wscript.Arguments.Item(1)
remoteFolder = Wscript.Arguments.Item(2)
moveSubFolder = Wscript.Arguments.Item(3)
user = Wscript.Arguments.Item(4)
pass = Wscript.Arguments.Item(5)
host = Wscript.Arguments.Item(6)
If localPath = "" Then
Wscript.Echo "No Folder parameter was passed"
Wscript.Quit
Else
Wscript.Echo "Runing For " & localPath & " , " & localFolder & " , " & remoteFolder & " , " & moveSubFolder & " on connection " & host
End If
' JBH - Open ftp command
'Set ftpo = CreateObject("InetCtls.Inet.1") 'Msinet.ocx
Set ftpo = CreateObject("ChilkatFTP.ChilkatFTP.1")
ftpo.Username = user
ftpo.Password = password
ftpo.Hostname = host
ftpo.Password = pass
ftpo.Passive = true
Wscript.Echo "Connecting..."
if (ftpo.Connect()) <> 1 Then
Wscript.Echo "Failed Connection : " & ftpo.LastErrorText
ftpo.Disconnect()
Wscript.Quit
else
' 00 - Set Remote Folder
Wscript.Echo "00 - Set remote folder to " & remoteFolder
If (ftpo.ChangeRemoteDir( remoteFolder )) <> 1 Then
Wscript.Echo "Failed setting remote dir : " & ftpo.LastErrorText
ftpo.Disconnect()
Wscript.Quit
End If
' 01 - DOWNLOAD
Wscript.Echo "01 - Getting All files from " & remoteFolder & " to " & localFolder
ok = ftpo.MGetFiles("*.*", localPath)
If (ok) <> 1 Then
Wscript.Echo " Download returned "& ok &" : " & ftpo.LastErrorText
'ftpo.Disconnect()
'Wscript.Quit
End If
' 02 - RENAME (Move files)
' list current local files
Set folder = fso.GetFolder(localPath)
Set Files = folder.Files
' move each one on distant server from current folder to dFolder
Wscript.Echo " Move files present in " & localPath & " to remote folder " & remoteFolder & "/" & moveSubFolder
For each file In Files
Wscript.Echo " Rename " & file.Name & " to " & moveSubFolder & "/" & file.Name
ok = ftpo.RenameRemoteFile(file.Name, moveSubFolder & "/" & file.Name)
If (ok) <> 1 Then
Wscript.Echo " Rename Returned unexpected value (" & ok & ") " & ftpo.LastErrorText
'ftpo.Disconnect()
'Wscript.Quit
End If
Next
' Finish session
WScript.Echo "Closing"
ftpo.Disconnect()
WScript.Echo "Done!"
End if
</pre>