Batch and CMD file still have their uses
Category FTP Batch
I bitch about Batch files in IBM installers (for good reason I believe), but they still have their uses in other areas. I recently had a situation where I wanted a customer to be able to easily capture logs on their Sametime server, and have them FTPd to my server so that I could look at them. A couple of things needed to happen, I needed to automate the FTP stuff on the server, for uploading a file. I needed each file name uploaded to be unique, as I had no idea how many times a day they were going to send me logs. So here's how a BAT/CMD file can be used to address both issues.
First, how do we create a unique filename. Well it's going to required the date and time, and in my case I prefix it with the name of the server. So the following CMD assumes I have a c:\TEMP\Logs.zip and renames it to the new unique name. It sets a variable as the current year month day hour minutes seconds, why set it as a variable? Well if we don't and we use the filename again later in the CMD file the seconds and minutes will have changed so we no longer know the name of the file.
set hh=%time:~0,2%
REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set yyyymmdd_hhmmss=20%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
REN "C:\TEMP\Logs.zip" 85MTLogs%yyyymmdd_hhmmss%.zip
OK so that renames a file to a unique file name, how about automating FTP. Well the hardest thing here is passing in the various commands for things like login name and password and more importantly the unique filename. If the filename was constant it wouldn't be an issue, but the filename changes every time. The key was having the CMD file build a text file containing the instructions for the ftp command to execute, by using the -s and passing the name of the text file. So assuming the filename to be uploaded is the same as above, this bit of CMD file, will create the file containing the FTP commands then execute the FTP command, before deleting the temporary config file.
echo open ftp.ftpserver.com >>"C:\TEMP\tempFTP.tmp"
echo FTPUSERNAME>> "C:\TEMP\tempFTP.tmp"
echo FTPPASSWORD>> "C:\TEMP\tempFTP.tmp"
echo binary>> "C:\TEMP\tempFTP.tmp"
rem echo CD STLogs >> "C:\TEMP\tempFTP.tmp"
echo put "C:\TEMP\85MTLogs%yyyymmdd_hhmmss%.zip" >> "C:\tempFTP.tmp"
echo quit >> "C:\temp\tempFTP.tmp"
FTP -s:"C:\TEMP\tempFTP.tmp"
DEL /Q "C:\TEMP\tempFTP.tmp"
There you you go, that's it.
I bitch about Batch files in IBM installers (for good reason I believe), but they still have their uses in other areas. I recently had a situation where I wanted a customer to be able to easily capture logs on their Sametime server, and have them FTPd to my server so that I could look at them. A couple of things needed to happen, I needed to automate the FTP stuff on the server, for uploading a file. I needed each file name uploaded to be unique, as I had no idea how many times a day they were going to send me logs. So here's how a BAT/CMD file can be used to address both issues.
First, how do we create a unique filename. Well it's going to required the date and time, and in my case I prefix it with the name of the server. So the following CMD assumes I have a c:\TEMP\Logs.zip and renames it to the new unique name. It sets a variable as the current year month day hour minutes seconds, why set it as a variable? Well if we don't and we use the filename again later in the CMD file the seconds and minutes will have changed so we no longer know the name of the file.
set hh=%time:~0,2%
REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set yyyymmdd_hhmmss=20%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
REN "C:\TEMP\Logs.zip" 85MTLogs%yyyymmdd_hhmmss%.zip
OK so that renames a file to a unique file name, how about automating FTP. Well the hardest thing here is passing in the various commands for things like login name and password and more importantly the unique filename. If the filename was constant it wouldn't be an issue, but the filename changes every time. The key was having the CMD file build a text file containing the instructions for the ftp command to execute, by using the -s and passing the name of the text file. So assuming the filename to be uploaded is the same as above, this bit of CMD file, will create the file containing the FTP commands then execute the FTP command, before deleting the temporary config file.
echo open ftp.ftpserver.com >>"C:\TEMP\tempFTP.tmp"
echo FTPUSERNAME>> "C:\TEMP\tempFTP.tmp"
echo FTPPASSWORD>> "C:\TEMP\tempFTP.tmp"
echo binary>> "C:\TEMP\tempFTP.tmp"
rem echo CD STLogs >> "C:\TEMP\tempFTP.tmp"
echo put "C:\TEMP\85MTLogs%yyyymmdd_hhmmss%.zip" >> "C:\tempFTP.tmp"
echo quit >> "C:\temp\tempFTP.tmp"
FTP -s:"C:\TEMP\tempFTP.tmp"
DEL /Q "C:\TEMP\tempFTP.tmp"
There you you go, that's it.
Comments
Thanks for this Carl. I'll be using this soon with a project.
Posted by John James At 09:47:40 AM On 04/28/2010 | - Website - |
Posted by Carl Tyler At 09:48:54 AM On 04/28/2010 | - Website - |
Tugether with USBDLM it even works as simple as pluging in my USB Drive.
Posted by Jan Schulz At 04:56:52 PM On 04/29/2010 | - Website - |