Rob van der Woude's Scripting Pages

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

  1. ℹ️ DOs and DON'Ts When Writing Batch Files
  2. ℹ️ Comments
    How to add comments to your batch files.
  3. ℹ️ How to prevent code insertion exploits
  4. ℹ️ Input validation

Back to the top of this page . . .

 

Debugging Batch Files

  1. ℹ️ 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

  1. ℹ️ Read and SET environment variables
  2. ℹ️ Dynamic environment variables
  3. ℹ️ Verify if variables are defined
    With a chapter on "hidden" (dynamic) variables.
  4. ℹ️ Validate Variables
    Check if a string matches specific formats.
  5. ℹ️ Arrays
    Who said the batch language doesn't know arrays? Think again.
  6. ℹ️ Delayed variable expansion and FOR loops
    Setting variables inside FOR loops or inside other code blocks.
  7. ℹ️ Escape characters
    How to tell CMD.EXE not to interpret certain characters.
  8. ℹ️ 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?
  9. ℹ️ Boolean Logic in Batch Files
    Emulate logical operators in IF statements.
  10. ℹ️ 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.
  11. ℹ️ Parameters
    How to parse command line parameters (or arguments).
  12. ℹ️ Random Numbers
    Sample batch files to generate random numbers.
  13. Strings:
  14. ℹ️ Environment Variables in Windows XP and in Windows 7

Back to the top of this page . . .

 

Devices

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

Back to the top of this page . . .

 

Elevated Privileges (UAC)

  1. ℹ️ Check and set elevated privileges

Back to the top of this page . . .

 

Files

  1. ℹ️ Rename files
    The use of wildcards in the REN command
  2. Read files
  3. Write files
  4. ℹ️ Read File Properties
    Read a file's attributes, size, extension, last-modified date, parent folder, and more.
  5. ℹ️ Read File and Product Versions
    Read a file's file version, product version, and more.
  6. File Encoding
  7. ℹ️ Open Files
    Use the START command to open a file with its associated program.
  8. ℹ️ Read INI Files
    Sample batch file to read INI files.
  9. ℹ️ Temporary Files
    Some notes on temporary files as used by my batch samples.

Back to the top of this page . . .

 

Folders

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

Back to the top of this page . . .

 

Internet

  1. ℹ️ Get the Default Browser
  2. ℹ️ Prompt to download
    Check if utilities are available, and prompt to download if they aren't.
  3. ℹ️ E-mail
    Send e-mails from a command line or batch file.
  4. ℹ️ FTP
    Automate FTP uploads and downloads.
  5. ℹ️ User-Agent strings (Browser IDs)

Back to the top of this page . . .

 

Inventory

  1. ℹ️ 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. ℹ️ 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. ℹ️ 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)
  4. ℹ️ 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

  1. ℹ️ 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. ℹ️ Format numbers in batch files
    Display numbers in octal or hexadecimal notation, or right aligned.
  3. ℹ️ PHP based batch files
    Why not use the powerful features of PHP in batch files?
  4. ℹ️ Validate Variables
    Check if a string matches specific formats.

Back to the top of this page . . .

 

Miscellaneous

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

Back to the top of this page . . .

 

Network

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

 

Wildcards

Back to the top of this page . . .


page last modified: 2024-03-18; loaded in 0.0066 seconds