I am a software developer supporting a production application. We have a Java - .NET -Oracle system. I wanted to write a script that could collect the logs from the different servers and bring it to my desktop each morning. As my desktop has Windows XP operating, I thought I could just create a batch script as I would have if it were a Unix operating system, but to my dismay, I found out that batch scripts in DOS are not near as powerful as the shell scripts in Unix. Following are the lessons that I learnt today.
- One of the requirements of the script was to use FTP to download files from a Unix box. For this, it is better to create a separate file that contains the commands that need to be run on the FTP prompt and use the file within the batch script. Let us say I needed to connect to the FTP server, change the directory to mydirectory and get all the files having names starting with "
For this, I would create a file called ftptemp.ftp as follows:
I would include this file in the batch script. Let us call this file mybatch.bat. In this batch file, I first want to change to the directory mylocaldirectory and then download the files to mylocaldirectory. I would create the batch file as follows:
ftp -i -s:ftptemp.ftp ftpserver
-i turns off interactive prompting for multiple file transfers during the FTP process. This would help when the script has the
mget command because it can download multiple files.
-s specifies the file that contains the FTP commands.
- Another requirement was to create a folder that includes the create date and time in the name and then download the files into the newly created folder. In DOS batch files, current date is stored in a system variable called
date and time is stored in a variable called
date could be displayed using the following command:
In DOS batch file processing, a portion of a
string can be extracted by using the following command:
In MS DOS, Jan 13 2010 would be stored as:
So to extract month portion of the
date, we would have to use the command:
The following code can be used to create a folder containing current
2%%time:~6,2%%time:~9,2%md %a% cd %a%
- MSDOS batch scripts do not support arrays. There is a convoluted way of simulating arrays that I didn't want to use. So instead of using batch files, I used wscript. So to create a
string with yesterday's date in the format
ddMonyyyy, the following code could be used:
dtmYesterday = Date() - 1day1 =CStr(DatePart("d",dtmYesterday))
if len(day1)=1 then set day1="0"&day1 end if
This code above will throw an error "
invalid handle", if the VBScript is executed directly at the command prompt:
The code would work if the above script is called using
In conclusion, if I have to write the script again, I would use VBScript instead of MSDOS batch script.