I find that some
everyday tasks can be simplified by writing a quick batch script.
“When a batch file
is run, the shell
program (usually COMMAND.COM
or cmd.exe
) reads the file
and executes its commands, normally line-by-line. Batch files are useful for
running a sequence of executables
automatically and are often used by system
administrators to automate tedious processes.” http://en.wikipedia.org/wiki/Batch_file
The most common way to
write a batch script is via notepad. The file must be saved with a ‘bat’ file
extension. For example ‘script.bat’
Most scripts start off
with the following line of code:
@echo
off
This
turns command echoing off. ECHO is a command which displays text. When you run
the following script note that the ECHO command isn’t displayed – instead you
only see the result of the ECHO command (“Hello World!”). This is what I mean
by command echoing off.
@echo
off
ECHO
“Hello World!”
pause
ECHO can
print text into a txt file. Then open the file with notepad.exe. NOTE: I’ve
found that ECHO can do this for xls, doc, vbs, and other file formats
respective to their application.
@echo
off
ECHO
“Hello World!” > texting123.txt
notepad.exe
texting123.txt
(see the
more advanced vbs example below)
@echo
off
ECHO
Set objShell = WScript.CreateObject("WScript.Shell") > NetConnect.vbs
ECHO.
>> NetConnect.vbs
ECHO
With objShell >> NetConnect.vbs
ECHO .Run "Control ncpa.cpl" >> NetConnect.vbs
ECHO wscript.sleep 2000 >> NetConnect.vbs
ECHO .AppActivate "Network
Connections" >> NetConnect.vbs
ECHO.
>> NetConnect.vbs
ECHO
END With >> NetConnect.vbs
NetConnect.vbs
Adding a
second > symbol will add a line below the previous. Also note that adding a period
after ECHO will make a blank space.
@echo
off
ECHO
“Hello > texting123.txt
ECHO.
>> texting123.txt
ECHO
World!” >> texting123.txt
notepad.exe
texting123.txt
Setting
variables can be very useful when requiring input. Here we use the SET command
to create a variable to call upon. Note that we use the percent symbols when
the variable ‘name’ is called upon.
@echo
off
SET /p
name=Please type your name here:
ECHO
“Hello, %name%” > texting123.txt
notepad.exe
texting123.txt
Windows
has built-in system variables which you can call on without declaring them
through the SET command such as %userprofile% and %windir%.
@echo
off
ECHO
%userprofile% > texting123.txt
ECHO
%windir% >> texting123.txt
notepad.exe
texting123.txt
Conditional
statements can be made. For example: If “C:\temp” exists, then delete the
directory and subdirectories, otherwise declare that the folder doesn’t exist.”
@echo
off
IF
EXIST C:\Temp (
RD /S
C:\Temp
) ELSE
(
ECHO
The C:\Temp directory doesn't exist.
ECHO.
ECHO.
)
PAUSE
The purpose
in writing this tutorial was to get a beginner up to speed enough to understand
some basic batch script principles. More advanced batch language can be found
if you STFW (the way I learned most of this stuff).
Here are
some real world examples to download:
NOTE: This site is a purely
private homepage for the author. It has absolutely no associations with ANY
hardware or software manufacturer, NOR retail store, service center or company.
The opinions expressed on this site are not to be construed as anything other
than the purely personal opinions and amusements of the author. That said, if
you are offended or insulted by this site, don’t come back. I didn’t make it
for you anyway. This site is brought to you via the First Amendment and someone
with too much spare time on their hands.