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 information icon at the left of the titles before asking why a batch file doesn't work in your environment.

 

 

Best Practices

  1. AllDOs and DON'Ts When Writing Batch Files
  2. AllHow to prevent code insertion exploits
  3. AllInput validation

Back to the top of this page . . .

 

Debugging Batch Files

  1. AllDebug your batch files
    For those rare (?) occasions when your batch files don't do what they were expected to...
  2. XP+Use BatCodeCheck to check your batch files for common errors and vulnerabilities

Back to the top of this page . . .

 

Data & Environment Variables

  1. NT4+Arrays
    Who said the batch language doesn't know arrays? Think again.
  2. AllComments
    How to add comments to your batch files.
  3. W2K+Delayed variable expansion and FOR loops
    Setting variables inside FOR loops or inside other code blocks.
  4. NT4+ OS/2Escape characters
    How to tell CMD.EXE not to interpret certain characters.
  5. NT4+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?
  6. NT4+Boolean Logic in Batch Files
    Emulate logical operators in IF statements.
  7. NT4+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.
  8. AllParameters
    How to parse command line parameters (or arguments).
  9. AllRandom Numbers
    Sample batch files to generate random numbers.
  10. Strings:
  11. AllVerify if Variables are Defined
    With a chapter on "hidden" (dynamic) variables.
  12. NT4+Environment Variables in Windows XP and in Windows 7

Back to the top of this page . . .

 

Devices

  1. AllDevices
    Check if a "file" name is a device name.
  2. W2K+DEVCON
    Manage devices and drivers, a command line utility alternative to the Device Manager.

Back to the top of this page . . .

 

Files

  1. NT4+Rename files
    The use of wildcards in the REN command
  2. Read files
  3. Write files
  4. NT4+Read File Properties
    Read a file's attributes, size, extension, last-modified date, parent folder and more.
  5. W2K+ASCII vs. Unicode
    Convert ASCII to Unicode vv.
  6. NT4+Open Files
    Use the START command to open a file with its associated program.
  7. NT4+Read INI Files
    Sample batch file to read INI files.
  8. AllTemporary Files
    Some notes on temporary files as used by my batch samples.

Back to the top of this page . . .

 

Folders

  1. AllCheck if a folder exists
  2. AllRename folders
    How to use the MOVE command to rename folders
  3. NT4+FOR /D and FOR /R
    Use the FOR command's /D and /R switches to handle sets of folders instead of files.
  4. NT4+RD
    RD /Q /S works like a DELTREE replacement.

Back to the top of this page . . .

 

Internet

  1. NT4+Prompt to download
    Check if utilities are available, and prompt to download if they aren't.
  2. W9* NT4+E-mail
    Send e-mails from a command line or batch file.
  3. NT4+FTP
    Automate FTP uploads and downloads.
  4. AllUser-Agent strings (Browser IDs)

Back to the top of this page . . .

 

Inventory

  1. XP+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.
  2. 2000+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.
  3. DirectX6+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)

Back to the top of this page . . .

 

Math

  1. NT4+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?
  2. NT4+PHP based batch files
    Why not use the powerful features of PHP in batch files?

Back to the top of this page . . .

 

Miscellaneous

  1. AllAUTOEXEC.BAT
  2. NT4+Undocumented NT commands
    Hard to find documentation for some commands available in Windows NT and later.
  3. NT4+PHP based batch files
    Why not use the powerful features of PHP in batch files?
  4. NT4+UNIX ports
    "Ports" of UNIX commands to Windows NT 4+.
  5. AllUseless tips?
    Strange or unexpected behaviour of some DOS commands, and how to use it to your advantage.
  6. All(Ab)using the DEBUG command.

Back to the top of this page . . .

 

Network

  1. Login scripts
  2. W2K+DS Tools
    Directory Services Tools, command line utilities to query and manage Active Directory.
  3. W9* NT4+E-mail
    Send e-mails from a command line or batch file.
  4. W9* NT4+RAS
    Manage dial-up connections.
  5. NT4+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

Back to the top of this page . . .

 

Program Flow

Back to the top of this page . . .

 

Registry

Back to the top of this page . . .

 

Samples Collections

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

Back to the top of this page . . .

 

User Interaction

Back to the top of this page . . .


page last uploaded: 7 November 2014, 21:28
Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools