#eSolia Scripting Notes

This set of notes is to help a beginner at batch scripting get a basic level of understanding and competence in the topic.

Rick Cogley, 25 Nov 2014

eSolia Scripting - Part I

This section is intended to help get a beginning batch scripter started.

What is scripting?

Scripting generally describes the programming one does when working with batch or script files, and often is used in systems admininistration work, or website design and build work.

See Scripting Language on Wikipedia for a lot of information on this topic.

What is a batch file?

A “batch script” is a text file with commands which are executed line by line, by the OS’s command interpreter, cmd.exe on Windows, and a shell like BaSH or Z Shell on *nix systems.

A batch file has an extension of either .bat or .cmd.

On Windows, it’s usually called a batch file or “batch script”. On Unix and Mac, it’s usually called a script or “shell script”.

This outline addresses batch scripts in a Windows environment.


Where should I store batch files?

Store your batch files in a folder somewhere: either a project folder, or a common location such as c:¥eSolia.


You can create your storage folder with Explorer, or, using the cd and mkdir commands on the command line.

How do I edit a batch file?

Use a good “programmer’s editor” such as the following:

Note: the free editors that come with the OS, such as Notepad or メモ帳 on Windows or TextEdit on Mac, are often too basic to be useful. It is a worthwhile investment to purchase a good text editor to work with, as an IT professional.


Get a programmer’s editor, and create a batch file, saving it in c:¥eSolia. Be sure to save it with the correct extension.

How do I run a batch file?

To run a batch file, simply enter the name of the batch file at the command prompt. For example, if your batch file is called mybat.bat, just type “mybat.bat” (without the quotes) and press enter.

When I say “command prompt” in the context of this outline, I am talking about the command line program in Windows that “prompts” you to enter a command with its > symbol. On other operating systems it could also of course mean the Terminal program on a *nix-like system.

Win cmd.exe prompt screenshot


Confirm whether you need to type the .bat or .cmd extension, or, whether you can simply type the filename without the extension, when you run a batch.


When it does run, what do you observe?

How do I suppress console output?

When you run a batch for the first time, you will notice that all commands are repeated on the console. To suppress the repetitive display, enter this at the top of your batch file:

echo off

You will notice that the echo off line will itself be displayed twice when you run the batch. Enter a @ in front of that line, to supress the extra echo:

@echo off

Once you do that, now each line’s output only will be output on the command line.

Use @echo off at the beginning of every Windows batch file.


Using your simple batch as an example, trying it with echo off or @echo off at the top.

How can I put comments into a batch file to describe what it is doing?

Engineers seem to dislike documentation, but we should do it to help our brothers and sisters out, when they take over maintenance of programs and systems.

It is a convention to make your scripts (and indeed any programming code) self-documenting, and you do this by adding “commented out” lines to describe what is going on, in your batch.

Use REM or # followed by a space, at the front of any line to “comment it out”, or, cause the batch interpreter to ignore it.



Edit your batch to include both REM and # comments. When you run it, do you see them on the command line?

How can I cause the batch to display text as it runs?

If you want to have your batch display text as it runs, for status messages and so on, use the echo command, followed by the text you want to display, in parentheses.

echo "this will be displayed"

You can also use system variables with echo. Use the “set” command to see a list of the variables, then reference them between percentage signs, like so:

echo "Computer: %computername%"

How can I display a blank line between commands?

It begs the question, then, how do you echo a blank line? Simple: use echo followed immediately by a period, with no space.



How do I get help with commands?

You see a lot of commands being used here, but how do you get help on their usage and syntax? You can Google them, of course, but the OS has information about them which you can access from the command prompt.

Just use /? after the command you want to act upon, to view its help. For example, get help on echo and set.

>set /?

Some commands use the syntax --help for this, so you should try both.

>thecommand --help

The net command in Windows has an unusual help syntax which uses neither /? nor --help. Get help like this:

>net help use

Many commands have multilingual help. If you do mycommand /? and get an unexpected language, use the chcp command to change code pages. For instance:

>chcp 437

See this page for more info on code pages.


C:\Windows\system32>echo /? 
メッセージを表示したり、コマンド エコーの ON と OFF を切り替えます。

  ECHO [メッセージ]

現在のエコー設定を表示するには、パラメーターを指定せずに ECHO と入力してください。

How can I capture the output from the script in a text file?

If you want to create a file from the results of the commands inside your batch, then use > and >> to redirect their output to a file.

set > myoutput.txt

It’s common to use > at the top, and >> for the rest of the commands that put their result into your output file.



Of course you can use your editor to view the contents of the output file. However, also try using the type command.

>type results.txt

How can I use a variable instead of a static filename for output?

Instead of repeatedly entering results.txt to capture output, you can create a variable to use, and then set the variable to any string.

A variable has a name and a value, and you use the set command to set one.

>set myvar=thevalue

Notice the difference between setting and calling a variable.

>set favoritedrink=coffee
>echo %favoritedrink%

Notice there are no spaces around the equals sign.


Edit your batch file to replace results.txt with a variable. Try changing the variable value, and rerunning the batch. Does the filename of the file containing the output change as expected?

Set a variable inside a batch. Can you echo the variable on the command prompt, after the batch finishes?

Set a variable outside a batch and use echo to display its contents. Does the variable persist after you restart the command prompt?


How can I set my ping target at run time?

So far, our batch has a fixed value to ping, but if you were using this batch file at various support client sites, for instance, you would want to be able to vary the ping target each time.

You can use batch “arguments” to do that. Arguments are strings you enter after the batch name when you run it, which are then referenced inside the batch. Here’s an example with a single argument:

>mybatch.bat www.microsoft.com

Inside mybatch.bat you would reference the ping target like so:

@echo off
ping %1 > %computername%.txt


Edit your batch file to replace the fixed ping target with the first batch argument.

Edit the batch file to process two arguments as ping targets.

>mybatch.bat www.microsoft.com www.google.com


@echo off
# Author: Rick Cogley
# Last Update: 1 Nov 2014
# Description: this batch is a test, to show how to output command results to a text file, which filename is defined by a variable.
set myresults=results.txt
echo "== STARTING BATCH =="
echo "Results from %computername%:" > %myresults% 
echo "Pinging..." >> %myresults%
ping -n 4 %1 >> %myresults%
echo. >> %myresults%
echo "Running SET..." >> %myresults%
set >> %myresults%
echo. >> %myresults%
echo "PC info..." >> %myresults%
echo "Computer: %computername%" >> %myresults%
echo "Domain: %computerdomain%" >> %myresults%
echo "== ENDING BATCH =="

How do I echo the date and time into a filename?

Next, let’s figure out how to get the date or time into the filename.
first test it. Outside a batch do:

time /t
?date /t
date /t
then try:
echo %date%
echo %time%
then try what’s called “substring” parameters on the echo commands. for example:
Thank you. I will do it from now.
echo %time:~0,2%
the :~0,2 part means:
start at character 0 and show 2
first char = 0th char, in terms of computer language
try echoing, changing :~N,M as needed, to get the parts of the time or date strings you want
then assemble them using set and echo the result
the goal is to have an output file like - eSolia-MyName-MyComputerName-201412xxxxxx.txt
…showing the date and time at the end
You can substitute spaces for zeroes or any other char, this way -
set somevar=%somevar: =0%
It means “take the space and replace with 0”
you’d set somevar first, then set it again, replacing space with 0, or another char. Try it.

How can I loop?

for in do

Put date in output
Read file
Get help
Prove it worked
Drive map
What’s running ps
Net session
Start and stop
Run batch from cmd line or, start menu
User input



Try a gist: