Saturday, November 1, 2014

FTP - Solution to only delete files that have been successfully downloaded

I know ftp and mget have been a round for a long time; however, just doing an mget followed by an mdel has a few major flaws. 1) mget does not have a delete on successful download and 2) mdel does not check to see if the file was received. With some of the notoriously bad internet connections out there it is very possible that the mget failed and the mdel is not aware of this when it is executed.

So this solution actually took a while to come up with, as the true art of windows batch script development has almost all but been lost. To start with we need to script our ftp connection to connect to our remote host, change directories and then download all files. I call this script ftpdown.scp,

ftpdown.scp
 
onerror exit
USER myuser
PASS mypass
CONNECT 10.10.10.90
CD /u02/out
lcd c:\in
mget *.*
close


Next I create a main batch file I call ftpdownmain.bat. This is this main script a user will execute. The script will first execute the windows ftp command along with the above script and download all the files. Afterwards it queries a directory listing of just file names and puts these names into a file I call downlist.txt. Important Note: downlist.txt and ftpgetlist.scp are created and removed automatically so there is nothing for you to do with these.

ftpdownmain.bat
 
c:
cd gensrvnt\scripts
ftp -s:ftpdown.scp

dir /b c:\in\*.* > c:\gensrvnt\scripts\downlist.txt
call c:\gensrvnt\scripts\ftpscpcreate.bat
c:
cd gensrvnt\scripts
ftp -s:ftpgetlist.scp

del c:\gensrvnt\scripts\downlist.txt
del c:\gensrvnt\scripts\ftpgetlist.scp

exit

ftpscpcreate.bat - Use this to Move files
 
echo onerror exit > c:\gensrvnt\scripts\ftpgetlist.scp
echo USER myuser >> c:\gensrvnt\scripts\ftpgetlist.scp
echo PASS mypass >> c:\gensrvnt\scripts\ftpgetlist.scp
echo CONNECT 10.10.10.90 >> c:\gensrvnt\scripts\ftpgetlist.scp
echo CD /u02/out >> c:\gensrvnt\scripts\ftpgetlist.scp
for /f  %%i in (c:\gensrvnt\scripts\downlist.txt) do (
  echo rename %%i archive/%%i >> c:\gensrvnt\scripts\ftpgetlist.scp
)
echo close >> c:\gensrvnt\scripts\ftpgetlist.scp


ftpscpcreate.bat - Use this to Delete files
 
echo onerror exit > c:\gensrvnt\scripts\ftpgetlist.scp
echo USER myuser >> c:\gensrvnt\scripts\ftpgetlist.scp
echo PASS mypass >> c:\gensrvnt\scripts\ftpgetlist.scp
echo CONNECT 10.10.10.90 >> c:\gensrvnt\scripts\ftpgetlist.scp
echo CD /u02/out >> c:\gensrvnt\scripts\ftpgetlist.scp
for /f  %%i in (c:\gensrvnt\scripts\downlist.txt) do (
  echo rm %%i >> c:\gensrvnt\scripts\ftpgetlist.scp
)
echo close >> c:\gensrvnt\scripts\ftpgetlist.scp


Just a final note, you may have to play around with the commands depending on the server you are connecting to. Feel free to contact at support@ediassociates.com if you have a special requests.