Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

MSDOS Batch Scripting Pains

0.00/5 (No votes)
19 Jan 2010CPOL2 min read 24.2K  
I wanted to write a script that could collect the logs from the different servers and bring it to my desktop each morning.

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.

  1. 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.

  2. 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%
  3. 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
    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 executed 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)