Thursday, April 7, 2011

mget new files from a FTP

The following batch script will get new files from a ftp server. 


The actual scenario is, 
A file is generated from application server A and needs to be uploaded to application server B. 


Flow of the operation,
  1. Create a ftp connection to application server A
  2. Generate a list of files to upload to application server B
  3. Dynamically generate a ftp script to mget and rename each file. (This is required as ftp doesn't allow rename or move with wild cards)
  4. Backup logs and files transferred for debug purposes

echo "=================Begin======================"

echo %date% %time%
Rem Connect to the ftp server and get a list of files to be transfered.

Rem The following command with execute the commands in "list_of_commands.txt" text file on the x.x.x.x ftp server. Refer List of commands heading below for a sample.
echo Generate a list of files to transfer.
ftp -s:list_of_commands.txt x.x.x.x > ftp.log

Rem Following will check the size of the file. If there is no data (no file listed) the size is zero.
echo check whether there are any files to transfer. If no files to transfer delete temporary files.
FOR /F "usebackq" %%A IN ('filelist.txt') DO set size=%%~zA
if %size% EQU 0 (
    echo No file to transfer. Deleting temporary files
    del ftplist.txt
    del ftp.log
    goto :eos
)ELSE (
    echo Files found to be transferred to Swift.
)

Rem The following will generate a ftp script dynamically based on the file list.
echo Generate ftp command file to transfer swift msgs.
>copy_FTP_Files.txt echo username
>>copy_FTP_Files.txt echo password
>>copy_FTP_Files.txt echo cd directory path
>>copy_FTP_Files.txt echo prompt n
>>copy_FTP_Files.txt echo ascii or binary
Rem The following for script will go through the list of files names in the filelist text file and generate mget and rename commands.
for /f %%U in (filelist.txt) do (
>>copy_FTP_Files.txt echo mget %%U
>>copy_FTP_Files.txt echo rename %%U %%U.sent
) 
>>copy_FTP_Files.txt echo bye

execute command to upload file to application B

Rem Execute ftp commands generated from the above step.
Echo Execute the dynamically generated ftp commands on the ftp connection.
ftp -s:copy_FTP_Files.txt x.x.x.x >> ftp.log

Rem Backup files for debug purposes.
echo Create a folder based on date time
SET DD=%DATE:~7,2%
SET MM=%DATE:~4,2%
SET YYYY=%DATE:~10,4%
SET HH=%TIME:~0,2%
SET MN=%TIME:~3,2%
SET SS=%TIME:~6,2%

SET FOLDER="%YYYY%-%MM%-%DD%_%HH%-%MN%-%SS%"
mkdir folder path\%FOLDER%

echo Move swift files copied to fromsymbols drirectory to the folder created with current date and time.
move list of files mget to
folder path\%FOLDER%
move ftplist.txt folder path\%FOLDER%
move ftp.log folder path\%FOLDER%
move copy_FTP_Files.txt folder path\%FOLDER%

:eos
echo %date% %time%
Echo "=================end======================"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.