Posted By admin On 04/08/21

Run the examples in this article using ‘Running Steps’ by Stepping Software ( It will help you visualize very easily the concepts here explained.

This post explains how to get current date and time from command prompt or in a batch file. How to get date and time in a batch file. Below is a sample batch script which gets current date and time. Note: A note on NT 4's SET /A switch from Walter Zackery in a message on alt.msdos.batch.nt: 'The SET /A command has a long list of problems. I wouldn't use it for much more than simple arithmetic, although even then it truncates all answers to integers.' The id command can check if a file exists don't exist and the same thing for seeing if a variable is defined. IF EXIST 'file.ext' echo found Remember to prevent batch files from getting lost when a file has spaces in the name add quotes.

So, what are they, and do they serve any special purpose? Environment variables make part of any normal batch script that reaches a minimal level of complexity. Why the need for delayed expansion? Let’s consider the typical example

set Missingfiles=
for %%p in (required1.exe required2.exe required3.exe) do (
if “%%~$PATH:p””” set MissingFiles=%MissingFiles% %%p )

Nov 13, 2018 You can use the goto command in a batch file to 'branch' the execution of your script, skipping to another section of the program.If you skip to a later part of the program, you can bypass lines of the script. On Windows 10, a batch file is a special text file that typically has a.bat extension, and it includes one or more commands that Command Prompt can understand and run in sequence to perform various.

This piece of codes searches for the 3 required files (required1.exe … required3.exe) in the path, and if not found, it will be added to the list. Let’s assume for the sake of the example that the 3 files are missing. What is the value of MissingFiles after this code has executed? It will not be the 3 files, it will be


So, if the files did not exist, how come only the 3rd (last) file got into the list? Because the value %MissingFiles% is resolved before the for command is executed. Therefore, the loop is the same as replacing the value before the loop is executed. The loop instruction is

set MissingFiles= %%p (equal sign – space -space – %%p)

So MissingFiles will be set 3 times with each file name, and the last one erases the 2 previous ones.

OK, so the intention of the code is to make a list. How should it be changed to do what it’s intended to do? By using delayed expansion, like this: (ENABLEDELAYEDEXPANSION only works in batch files. It does not work in the command line).

set Missingfiles=
for %%p in (required1.exe required2.exe required3.exe) do (
if “%%~$PATH:p””” set MissingFiles=!MissingFiles! %%p )

Now, at the end of the execution, the value of MissingFiles will be ‘required1.exe required2.exe required3.exe’ without the quotes, of course. By using delayed expansions and indicating the variable name within exclamation marks, the expression !MissingFiles! is resolved when the statement is executed, and not when the statement is parsed. Even with delayed expansion turned on, any variable expressed with percent signs (e.g. %MissingFiles%) will be resolved at parse time.

Cool! Now, are there any other benefits from using delayed expansion? Well yes. There is a very subtle and not well known benefit. Since the values are taken post-parse and during execution, any special characters will live literally. This can be explained with an example. Assume you want to write some xml file using a batch file (you may think me crazy, right? so many conflicting symbols like <, >, &, quotes, etc). Here we go.

Batch Set Options

set line=^<MovieData title=”Pillow Talk” Classification=”Comedy”^>Out of Stock^</Movie^>
echo processing line ‘%line%’
if “%line%””” echo Done Processing Data. Terminating

In a real world example the value of line would not be hardwired but most likely read from a file. Since here it’s hardwired, I added carets (^) before the special characters.

The output to the 2nd line is: The system cannot find the file specified.

The output to the 3rd line is: Talk” Classification=”Comedy”>Out of Stock</Movie>””” was unexpected at this time.

Why? Well, all the < and > and quotes made the commands go way wrong. Even when the carets were there in line 1. But how could this be? Well the carets in line 1 instructed the command processor when setting the variables, but that’s it. The caret is not part of the value of variable ‘line’. Symbols like < and > are used for redirection, and will make the command mean something different that what we intended. Quotes will drive the if command nuts, since the parser for the command will think it’s the closing quote. Quotes should appear twice in the condition for the command to work. Like this:

if “he said:””I’m typing””””he said:””I’m typing””” echo strings match

The parser sees double quotes as a quote inside quotes. The above works fine. The following is wrong:

if “he said:”I’m typing””he said:”I’m typing” echo strings match

will yield the results ‘typing””he was unexpected at this time.

To avoid all these complications, and when special symbols will make part of variable values, use delayed expanded variables. Using the same example, try the following code:

Caution: If you are running this from the command line and not a batch file, replace the first line with cmd.exe /V:oninstead. The options for setlocal only works inside a batch file and not in the command line.

set line=^<MovieData title=”Pillow Talk” Classification=”Comedy”^>Out of Stock^</Movie^>
echo processing line ‘!line!’
if “!line!””” echo Done Processing Data. Terminating

Now line 2 produced this output:

processing line ‘<MovieData title=”Pillow Talk” Classification=”Comedy”>Out of Stock</Movie>’

That’s exactly what we wanted. Since the string is parsed prior to execution, all redirections have already taken place and the special characters are taken as literals. The same is true for strings with quotes. No need to have double quotes when using delayed expansions.

And now, I will close with another example

set x=echo ^> is the greater than symbol

Line 2 (%x%) echoed the string ‘the greater than symbol’ to a file named is. Line 3 produced the following output in the console: > is the greater than symbol

So now you know. If you are processing file contents, and you want to be on the safe side. Always use delayed expansion.



We are done with all the basic preliminary concepts like variables, operators, conditional, branching and looping commands. Now this tutorial deals with the basic batch file commands that are used generally to make a batch file more complicated.

Sub commands, Switches, Parameters

Before jumping into the commands, I’d like to give a brief overview of the parts of commands. A command in batch program may consist of sub commands and switches. The sub commands are the supportive commands that are accompanied with the main commands. These sub commands generally serve to improve the accuracy of the result that we are looking for. Like the below example,

This command is used to list all the users on a computer. As you can see here, ‘net’ is a main command and the sub command ‘users’ is used to narrow down the results.

Switches allow us to run different variations of commands. You might have seen the switches in the ‘set’ command like p,a.

We can also pass parameters to the batch file which are nothing but the command line arguments. These parameters can be accessed by a number each in order. The syntax to pass the parameters from the command line is as shown below.

Eg: new HelloWorld!

Here ‘new’ is the batch file name and ‘Helloworld!’ is the argument that I will pass through the command line to the program. The parameters are delimited by spaces and commas, if I had given a space or a comma, Hello and world will be treated as two different parameters. So, here hello and world are treated as single parameter. Try out the below program to access the parameters through command line.

Program #1: To access the parameters given from command line


If you see the program you can easily say that the program prints ‘Welcome to (something in the place of %1%) program’. Here %1% is replaced with the first command line parameter. If you have a second and third one also, you can access it in the program by %2%, %3%, …. so on.

C:Usersuser1Desktop>new HelloWorld!!
Welcome to HelloWorld!! program
Press any key to continue . . .

Color Command

This command is used to set the color for the foreground as well as background of the command prompt window. The color is specified by the hexadecimal color codes. See the below image to list some color codes.

The foreground color is nothing but the text color that is set as default 7 and the background color is set to a default of 0.


Program #2: To change the foreground and background of the prompt window

color ED


Cls Command

Cls is the command that is used to clear the contents on the screen. There are many situations where you will be interacting with the user, displaying him a list of options and getting inputs and printing status dialogues. In such cases, you may eventually need to clear the contents on the screen. You can use this command to wipe of the screen in the current windows and start with a fresh window.


Title Command

When you double click on the batch file and run it, you may have observed that the name on the title bar is path of the cmd.exe program located in the system32 folder of your windows directory. If you want to change the title to your desired title, this command is utilized. Title command sets the title to the current window.



Program # 3: To illustrate title and cls commands

echo Welcome toMy window.
echo Did you observe that the previous message wiped off

The output for this program must be shown in two windows as the screen wiped after executing the first echo command.

Observe that the title of the window has been changed to the text which we have given in the program. Now immediately after I press any key, ‘cls’ command is executed and the text in the prompt is wiped off.

Set Batch Files File

Try it out!
Develop at least two versions of programs using the color, title, cls commands along with some conditional commands.


Setbatchlines 1

You May Also Like: