Wednesday, January 13, 2010

MSDOS batch scripting pains

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 is not near as powerful as the shell scripts in Unix. Following are the lessons that I learnt today.


a. 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 "myblog".


For this I would create a file called ftptemp.ftp as follows

cd mydirectory
mget *
bye

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:

cd c:\mylocaldirectory
ftp -i -s:ftptemp.ftp ftpserver

Option -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.
Option -s specifies the file that contains the ftp commands.

b. 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 time. So date could be displayed using the following command:

echo %date%

In DOS batch file processing, a portion of a string can be extracted by using the following command :

%var start,length%
In MS DOS, Jan 13 2010 would be stored as
Wed 01/13/2010
So to extract month portion of the date we would have to use the command %date 4,2%.

The following code can be used to create a folder containing current date and time :
set a=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%md %a% cd %a%
md %a%
cd %a%

c. 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 following code could be used:
dtmYesterday = Date() - 1day1 =CStr(DatePart("d",dtmYesterday))
if len(day1)=1 then set day1="0"&day1 end if
year1=Mid(CStr(DatePart("yyyy",dtmYesterday)),3,2)
WScript.StdOut.Write (day1+Mid(MonthName(DatePart"m",dtmYesterday)),1,3)+year1)


This code above will throw an error "invalid handle" , if the vbscript is execute directly at the command prompt :
c:\>vbscriptname.vbs



The code would work if the above script is called using cscript.
c:\>cscript vbscriptname.vbs



In conclusion if I have to write the script again, I would use vbscript instead of msdos batch script.










1 comment:

  1. Here is Mr Benjamin contact Email details, 247officedept@gmail.com. /  Or Whatsapp +1 989-394-3740 that helped me with loan of 90,000.00 Euros to startup my business and I'm very grateful,It was really hard on me here trying to make a way as a single mother things hasn't be easy with me but with the help of Mr Benjamin put smile on my face as i watch my business growing stronger and expanding as well.I know you may surprise why me putting things like this here but i really have to express my gratitude so anyone seeking for financial help or going through hardship with there business or want to startup business project can see to this and have hope of getting out of the hardship..Thank You.

    ReplyDelete