PrepLogic.com

 

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.

 

 

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+Integer Math
    Use the SET command for simple integer math.
  6. AllParameters
    How to parse command line parameters (or arguments).
  7. AllRandom Numbers
    Sample batch files to generate random numbers.
  8. Strings:
  9. AllVerify if Variables are Defined
    With a chapter on "hidden" (dynamic) variables.

Back to the top of this page . . .

 

Working with 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 . . .

 

Working with Files

  1. Read files
  2. Write files
  3. NT4+Open Files
    Use the START command to open a file with its associated program.
  4. NT4+Read INI Files
    Sample batch file to read INI files.
  5. AllTemporary Files
    Some notes on temporary files as used by my batch samples.

Back to the top of this page . . .

 

Working with Folders

  1. NT4+FOR /D and FOR /R
    Use the FOR command's /D and /R switches to handle sets of folders instead of files.
  2. 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.

Back to the top of this page . . .

 

Inventory

  1. XP+WMIC
    WMI queries from the command line.
  2. 2000+MSINFO32.EXE is one of Windows' native inventory tools.
    It is usually located in the folder "C:\Program Files\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.
  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.

Back to the top of this page . . .

 

Miscellaneous

  1. AllAUTOEXEC.BAT
  2. AllDebug your batch files
    For those rare cases when your batch file doesn't do what it was expected to...
  3. NT4+Undocumented NT commands
    Hard to find documentation for some commands available in Windows NT and later.
  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.

Back to the top of this page . . .

 

Network

  1. W2K+DS Tools
    Directory Services Tools, command line utilities to query and manage Active Directory.
  2. W9* NT4+E-mail
    Send e-mails from a command line or batch file.
  3. W9* NT4+RAS
    Manage dial-up connections.
  4. NT4+Terminal Server
    Command reference for some Terminal Server-only commands.

Back to the top of this page . . .

 

Printing

  1. NT4+Print files
    Find the print command for a specific file type, and print the file.
  2. AllUse your installed applications' command line switches to print files from the command line.
  3. W2K+Control printers
    Manage printers and print queues on the command line.
  4. NT4+Command line DDE
    Control programs that can act as DDE servers.
  5. NT4+Use GetPrint.vbs to list (non-DDE) Print and PrintTo commands for all registered file types.

Back to the top of this page . . .

 

Program Flow

  1. NT4+ OS/2Conditional execution
  2. AllErrorlevels
    Use error levels (or actually return codes) to control the flow of a batch file.

Back to the top of this page . . .

 

Processes & Services

  1. NT4+Processes
    Manage processes.
  2. AllShutdown
    Shutdown, reboot and logoff commands for "all" DOS, Windows and OS/2 versions, in batch as well as other languages.
  3. Command line DDE
    Control programs that can act as DDE servers.

Back to the top of this page . . .

 

Working with the Registry

  1. W95+REGEDIT
    Use REGEDIT non-interactively.
  2. NT4+REG
    Query the registry with REG Query.

Back to the top of this page . . .

 

Samples Collections

  1. DOSBrowse the MS-DOS examples
    An old collection of batch file scripting techniques, most of them for MS-DOS only.
  2. AllClever Tips and Tricks
    Several tips sent to me by visitors of this site.
  3. DOSHow To ...
    An 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.
  4. NT4+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.
  5. NT4+Scripts for Windows Administrators
    Batch files, KiXtart, Perl, Rexx and VBScript.
  6. AllSolutions 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.
  7. AllThird Party Batch Files
    Complete scripts sent to me by visitors of this site.
  8. NT4+Useful NT commands for Windows Administrators
    Powerful "one-liners" for the command prompt.
  9. AllUNIX Ports
    Some scripts to compensate for the missing (UNIX) commands in DOS & Windows.

Back to the top of this page . . .

 

Security

  1. W2K+Security Configuration with SeCEdit and MMC
    Set permissions, inheritance and ownership on files, folders and registry keys.

Back to the top of this page . . .

 

Time & Date

  1. DOSDATE and TIME in MS-DOS
    The basic syntax and some strange ways of using the DATE and TIME commands in COMMAND.COM.
  2. NT4+DATE and TIME in NT
    The basic syntax and some advanced samples of date math in CMD.EXE.

Back to the top of this page . . .

 

UNIX Ports

  1. AllUNIX Ports
    Some scripts to compensate for the missing (UNIX) commands in DOS & Windows.

Back to the top of this page . . .

 

User Interaction

  1. AllUser Input
    How to make your batch files prompt for user input.
  2. User Output

Back to the top of this page . . .