Blog Pro de Jean-Baptiste HEREN

Notes d'un consultant Freelance en Informatique

To content | To menu | To search

automatic ftp tranfers on windows OS client using the command line

Here is a little trick we used on a recent project.

We needed to automatize :

  1. (local <-- remote) download files from an ftp server specified folder
  2. (remote --> remote/subfolder) move the downloaded files on the source server so that we do not download them again on next run.

Needed Tools

ftp Client

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.

Scripting language

The following example uses Visual basic scripting.

The Script

After creating a new conenction in WINSCP client, we can use it on command line by running the following script :

cscript move.vbs D:\FTPROOT\IN\LOCAL IN/FOO OUT/FOO DONE MYACCOUNT '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

Hope it will help !

Alternatives to WinSCP

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 : Chilkat FTP ActiveX

Here is the code

'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

Post updated on Monday 27 June 2011, 20:55

Jean-Baptiste Heren

Author: Jean-Baptiste Heren

Stay in touch with the latest news and subscribe to the RSS Feed about this category

Be the first to comment on this article

Add a comment This post's comments feed


You Might Also Like

apostrophe.jpg

Apostrophe, le CMS naturel

Apostrophe est un CMS d'un genre différent. Finies les interfaces d'administration aux structures compliquées et aux fonctions absconses. Avec Apostrophe, vous éditez le contenu directement là ou il...

Continue reading

Convergence des applications Bureau / Web / Mobile

Il se passe quelque chose du côté des outils de développement. ce n'est pas une nouveauté.  La convergence progresse et l'on trouve de plus en plus de sociétés allant dans ce sens. Un peu d'histoire...

Continue reading