Click here to Skip to main content
13,256,446 members (54,822 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 19 Jan 2010

MSDOS Batch Scripting Pains

, 19 Jan 2010
Rate this:
Please Sign up or sign in to vote.
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 *

    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
    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:


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.


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerCouple of suggestions/observations. Pin
Member 104740025-Jan-10 10:20
memberMember 104740025-Jan-10 10:20 
QuestionPowerShell? Pin
scottb225-Jan-10 10:08
memberscottb225-Jan-10 10:08 
AnswerWhy DOS, for heaven's sake? Pin
knockNrod25-Jan-10 12:05
memberknockNrod25-Jan-10 12:05 
GeneralRe: Why DOS, for heaven's sake? Pin
xoox22-May-11 4:41
memberxoox22-May-11 4:41 
QuestionHow to makes this even better Pin
alex turner19-Jan-10 2:06
memberalex turner19-Jan-10 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 19 Jan 2010
Article Copyright 2010 by lkurian
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid