Good old BATCH makes trouble

I am sure that everybody knows BATCH even they are coming from Unix-like side. Well, I had a little problem with it because it did not do what I wanted (and we know always the computer is the responsible for our all mistakes). Let me share with you my experience.

It began when I tried to collect all of the information about BATCH (it is not important why I wanted to do this but I did) and after a while I reached a point where I stucked. This point was the logging. I think is not necessary to accentuate why it is so important – I mean – the logging.

Okay, okay I know there is a simple solution if I want to log something and that solution is not so complicated (put these >> after ECHO) but naturallwanted to find an elegant solution that won’t be so much complex. Why did I want it? Because who want to use something in BATCH if that thing is not simple enough.

Which is the most simple (and probably the ugliest) way of logging?

The only thing what you have to do put these two characters (>>) behind the ECHO command.

I am showing an example:

@ECHO off

ECHO Hello Let IT Know >>SAMPLE.log

ECHO How do you do? >>SAMPLE.log

ECHO I am fine. Thanks.>>SAMPLE.log

Output of this example (open the SAMPLE.log)

Hello Let IT Know

How are you?

I am fine. Thanks.

As far as I mentioned this is the most simple solution but as we can see this is not an “elegant” one. Do you like it? Then wait I will show you an another one which will prove you that sometimes everybody should take care about design of the code.

Let’s see that “magical” solution:

I recommend to use CALL command to call a section (section e.g. :MAIN)

Pay attention to use GOTO :EOF in the next line to be able to exit with proper result.


In the first row (which is actually the second) CALL command calls the :MAIN section to write the text out, of course it will not appear on the display but it will be redirected to the SAMPLE.log file. When the last ECHO command has been executed the “cursor” will be on the end of the second row and will continue with the next command that is (here) GOTO :EOF

It means that the script will be terminated without twice execution of the :MAIN section.

@ECHO off




ECHO Hello Let IT Know

ECHO How are you?

ECHO I am fine. Thanks.

Output of the execution (open SAMPLE.log to see):

Hello Let IT Know

How are you?

I am fine. Thanks.

Can I tell you more “interesting” information?

Of course I can tell. Have a look at the following lines:

Here you can see as I am using parameters and as I am giving these parameters to the :MAIN section.

If I did not do this then the log file would not get the entries and wrong values would be in the log file.

Put representation of the parameters (%1 %2 %3) behind the name of the section (e.g. :MAIN %1 %2 %3 >>SAMPLE.log) and do not forget the write GOTO :EOF rightafter the next line.

It will provide you that your script will run properly and make a good, accurate and usable log file with BATCH solutions.

@ECHO off

IF “%1″==”” GOTO FIRST

IF “%2″==”” GOTO SEC

IF “%3″==”” GOTO THIRD

CALL :MAIN %1 %2 %3 >>SAMPLE.log



ECHO First parameter is missing



ECHO Second parameter is missing



ECHO Third parameter is missing






I agree with that most of the readers this solution is not a big deal but even so I think it is much better then putting “>>” all of the ECHO line.

Finally I would like to say:

Thank you for your time and rate my post!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s