Batch File Scripting Techniques
In this section you will find a collection of scripting techniques and best practices for batch files.
Most of these techniques apply to either "DOS batch" (also related to as "real DOS" batch or "true DOS" batch), or "NT batch", only in rare cases to both.
If you want to use "true DOS" batch techniques in Windows NT 4 or later (2000, XP, Server 2003, Vista) you may have to invoke these batch files with COMMAND
/C
.
Check the "classification" for a batch technique by hovering your mouse pointer over the ℹ️ information icon at the left of the titles before asking why a batch file doesn't work in your environment.
Best Practices
- ℹ️ DOs and DON'Ts When Writing Batch Files
- ℹ️ Comments
How to add comments to your batch files.
- ℹ️ How to prevent code insertion exploits
- ℹ️ Input validation
Back to the top of this page . . .
Debugging Batch Files
- ℹ️ Debug your batch files
For those rare (?) occasions when your batch files don't do what they were expected to...
Back to the top of this page . . .
Data & Environment Variables
- ℹ️ Read and
SET
environment variables
- ℹ️ Dynamic environment variables
- ℹ️ Verify if variables are defined
With a chapter on "hidden" (dynamic) variables.
- ℹ️ Validate Variables
Check if a string matches specific formats.
- ℹ️ Get string length
Count the number of characters in a string.
- ℹ️ Arrays
Who said the batch language doesn't know arrays? Think again.
- ℹ️ Delayed variable expansion and FOR loops
Setting variables inside FOR loops or inside other code blocks.
- ℹ️ Escape characters
How to tell CMD.EXE not to interpret certain characters.
- ℹ️ Doing math in batch files
You knew you can use the SET
command for simple integer math, but how about really big numbers, or floating point?
- ℹ️ Boolean Logic in Batch Files
Emulate logical operators in IF
statements.
- ℹ️ Add/remove leading zeroes
This seemingly simple task often causes batch files to fail, so I listed several techniques here, with their pros and cons.
- ℹ️ Parameters
How to parse command line parameters (or arguments).
- ℹ️ Random Numbers
Sample batch files to generate random numbers.
- Strings:
- ℹ️ Environment Variables in Windows XP and in Windows 7
Back to the top of this page . . .
Devices
- ℹ️ Devices
Check if a "file" name is a device name.
- ℹ️ DEVCON
Manage devices and drivers, a command line utility alternative to the Device Manager.
Back to the top of this page . . .
Elevated Privileges (UAC)
- ℹ️ Check and set elevated privileges
Back to the top of this page . . .
Files
- ℹ️ Rename files
The use of wildcards in the REN
command
- Read files
- ℹ️ FOR /F overview
Read lines from a file or from another command's output.
- ℹ️ FOR /F's tokens and delims
Explanation of "tokens=... delims=..."
with hands-on examples.
- Write files
- ℹ️ Redirection overview
Send screen output to a file or to the next command.
- ℹ️ Redirection and streams explained
Standard error, standard output and console "streams" explained.
- ℹ️ The TEE command
Redirect output to a file and simultaneously display it on screen (requires "third party" software and/or scripts).
- ℹ️ Read File Properties
Read a file's attributes, size, extension, last-modified date, parent folder, and more.
- ℹ️ Read File and Product Versions
Read a file's file version, product version, and more.
- File Encoding
- ℹ️ Open Files
Use the START
command to open a file with its associated program.
- ℹ️ Read INI Files
Sample batch file to read INI files.
- ℹ️ Temporary Files
Some notes on temporary files as used by my batch samples.
- ℹ️ Useful PDF Commands
A collection of command lines to merge or print PDF files, or count their pages.
Back to the top of this page . . .
Folders
- ℹ️ Check if a folder exists
- ℹ️ Rename folders
How to use the MOVE
command to rename folders
- ℹ️ FOR /D and FOR /R
Use the FOR
command's /D
and /R
switches to handle sets of folders instead of files.
- ℹ️ RD
RD /Q /S
works like a DELTREE
replacement.
Back to the top of this page . . .
Internet
- ℹ️ Get the Default Browser
- ℹ️ Prompt to download
Check if utilities are available, and prompt to download if they aren't.
- ℹ️ E-mail
Send e-mails from a command line or batch file.
- ℹ️ FTP
Automate FTP uploads and downloads.
- ℹ️ User-Agent strings
(Browser IDs)
Back to the top of this page . . .
Inventory
- ℹ️ WMIC
WMI queries from the command line.
WMI(C) is software/OS oriented, and is known to sometimes return incorrect values for hardware queries (e.g. SATA/IDE/SCSI, serial ports, multiple monitors and screen resolutions).
Reports can be created by redirection only.
- ℹ️ MSINFO32.EXE is one of Windows' native inventory tools.
It is usually located in the folder %ProgramFiles%\Common Files\Microsoft Shared\MSInfo
.
Without command line arguments it will run in GUI mode.
Type MSINFO32 /?
for a list of available command line arguments.
Reliability is not better than WMI(C)'s, but in some cases MSINFO32
's command line may be preferred over WMIC
's.
For reports, use the /REPORT
switch.
- ℹ️ DXDIAG.EXE is Windows' native DirectX diagnostic tool.
It is usually located in the %windir%\System32
directory.
Without command line arguments it will run in GUI mode.
Type DXDiag /?
for a list of available command line arguments.
DXDiag
is much more reliable than WMIC
and MSINFO32
when it comes to video and sound hardware, but it is also much slower, and it can be used for the local computer only.
Reports can be saved in plain text (/t
switch) or XML (/x
switch)
- ℹ️ DMIDecode is a port of a Linux tool to read, interpret and display DMI (SMBIOS) data.
It is by far the fastest tool available for detailed information on mainboard, CPU and memory, but it can be used for the local computer only.
Back to the top of this page . . .
Math
- ℹ️ Doing math in batch files
You knew you can use the SET
command for simple integer math, but how about really big numbers, or floating point?
- ℹ️ Format numbers in batch files
Display numbers in octal or hexadecimal notation, or right aligned.
- ℹ️ PHP based batch files
Why not use the powerful features of PHP in batch files?
- ℹ️ Validate Variables
Check if a string matches specific formats.
Back to the top of this page . . .
Miscellaneous
- ℹ️ Comments
How to add comments to your batch files.
- ℹ️ Play with colors in the console
- ℹ️ AUTOEXEC.BAT
- ℹ️ Undocumented NT commands
Hard to find documentation for some commands available in Windows NT and later.
- ℹ️ PHP based batch files
Why not use the powerful features of PHP in batch files?
- ℹ️ UNIX ports
"Ports" of UNIX commands to Windows NT 4+.
- ℹ️ Useless tips?
Strange or unexpected behaviour of some DOS commands, and how to use it to your advantage.
- ℹ️ (Ab)using the DEBUG command.
Back to the top of this page . . .
Network
- ℹ️ Login scripts
- ℹ️ DS Tools
Directory Services Tools, command line utilities to query and manage Active Directory.
- ℹ️ E-mail
Send e-mails from a command line or batch file.
- ℹ️ RAS
Manage dial-up connections.
- ℹ️ Terminal Server
Command reference for some Terminal Server-only commands.
Back to the top of this page . . .
Printing
Back to the top of this page . . .
Processes & Services
- ℹ️ Processes
Manage processes.
- ℹ️ Shutdown
Shutdown, reboot and logoff commands for "all" DOS, Windows and OS/2 versions, in batch as well as other languages.
- ℹ️ Command line DDE
Control programs that can act as DDE servers.
- ℹ️ SC
Manage services with SC.EXE.
Back to the top of this page . . .
Program Flow
Back to the top of this page . . .
Registry
- ℹ️ REGEDIT
Use REGEDIT
non-interactively.
- ℹ️ REG
Query the registry with REG Query
.
- ℹ️ WMIC
Use WMIC
to access the registry, including checking access permissions.
- ℹ️ Search the registry
Use REG Query /F
to search the registry.
Back to the top of this page . . .
Samples Collections
- ℹ️ Browse the MS-DOS examples
An old collection of batch file scripting techniques, most of them for MS-DOS only.
- ℹ️ Clever Tips and Tricks
Several tips sent to me by visitors of this site.
- ℹ️ Batch HowTos
An new collection of batch file scripting techniques, most of them for Windows NT 4 and later only.
- ℹ️ How To ...
A really old collection of batch file scripting techniques, most of them for MS-DOS only.
The page will be opened in a new window because the frameset is incompatible with the DHTML menu and the stylesheet switcher.
- ℹ️ Poor man's admin tools
A collection of scripts that can be used to replace resource kit utilities or commands no longer available in recent Windows versions.
- ℹ️ Scripts for Windows Administrators
Batch files, KiXtart, Perl, Rexx and VBScript.
- ℹ️ Solutions found on alt.msdos.batch
Several clever scripting solutions from the alt.msdos.batch and alt.msdos.batch.nt newsgroups.
Most of these are from the previous millennium, but still hold their own.
- ℹ️ Third Party Batch Files
Complete scripts sent to me by visitors of this site.
- ℹ️ Useful NT commands for Windows Administrators
Powerful "one-liners" for the command prompt.
- ℹ️ Useless tips?
Strange and unexpected behaviour, for which I can't always find any good use.
- ℹ️ UNIX Ports
Some scripts to compensate for the missing (UNIX) commands in DOS & Windows.
- ℹ️ PHP based batch files
Not true batch files, as they require PHP, but if you do happen to have PHP installed on your PC...
Back to the top of this page . . .
Schedulers
Back to the top of this page . . .
Security
Back to the top of this page . . .
Time & Date
Back to the top of this page . . .
UNIX Ports
- ℹ️ UNIX Ports
Some scripts to compensate for the missing (UNIX) commands in DOS & Windows.
Back to the top of this page . . .
User Interaction
Back to the top of this page . . .
Wildcards
- ℹ️ Wildcards Mess
A short explanation of DOS wildcards, and how they may get you into trouble.
Back to the top of this page . . .
page last modified: 2024-08-31; loaded in 0.0016 seconds