Batch Files

Introduction

Batch files are simply text files that contain commands for the computer system to execute. Along with code specific to batch files, especially for the use of variables and loops, they are a way to automate tasks and save the user from having to type multiple commands in the Command Line Interface (CLI)

The files usually have a .bat file type but, depending on the computer system, .cmd or .btm can be used.

Batch files have now been largely superseded by PowerShelll as it has specific commands not allowed in batch files and can be easier to use and write scripts.

Thes pages show some of the problems I have come across and what I did to solve them with batch files.


Some Notes

Command Prompt

The command prompt (cmd.exe) is the Command Line Interface (CLI) of the Windows Command Processor. It is not the old Disk Operating System (DOS) command prompt (command.com) but behaves very similarly and its commands emulate many of the original DOS ones.

The original prompt in DOS opened at the C:/ prompt, which it was often referred to. In early versions of Windows it opened at C:\Windows\system32\ which is where cmd.exe is stored. In later versions of Windows it opens at the environment variable of USERPROFILE which is SYSTEMDRIVE\Users\[username] where SYSTEMDRIVE is another environment variable describing which drive Windows is installed on and so usually appears at C:\Users\[username]

Casing

Windows, like MacOS, is case insensitive. Folders and diretories can be created in all uppercase, lowercase or mixed case such as NEWFILE.TXT, newfile.txt, or NewFile.Txt but the operating system sees them all as a single entity and can be referenced using any casing but will not let you create a new folder or file with the same name.

This is unlike *unix type operating systems where they are separate entities and the casing must match when referencing them.

Environment Variables

Windows hides and protects the actual base file system from users. In File Explorer the user's home folder contains folders such as Desktop, Downloads, Documents and so on. The user's Documents folder (properly "directory" as this is now at system level, is not at C:\documents but is usually at C:\Users\[username]\Documents. Windows provides a number of shortcuts known as environment variables which can be thought of as bridging the difference between user folders and system directories.

Some little known variables are %~dp0, %~n0, and %~x0

%~dp0 - gives the folder the batch file is being run from
%~n0 - gives the name of the batch file
%~x0 - gives the batch file extension

Try this batch file:

@echo off
echo The batch file is being run from %~dp0
echo The name of the batch file is %~n0
echo The extension of the batch file is %~x0
echo The full path and batch file name is %~dp0%~n0%~x0

Folders vs Directories

The terms folders and directories are often used interchangeably but there are subtle differences between them. Directories are a system construct and are part of the system hierarchy. Folders are a user-friendly construct, especially in a graphical user interface (GUI) such as Windows where an office file folder icon is often used to represent them.

Properly speaking, when using a command line interface (CLI), the term directory should be used as it is the system hierarchy that is being manipulated, but I use the terms folder and directory interchangeably.

Slashes

With the rise in use of the internet is is common to see the character "/" used as a folder spearator. Windows, and DOS before it, does not use this convention, it has always used the "\" character. Using the forwardslash instead of the backslash in the command line or batch files will nearly always result in an error message.such as "The syntax of the command is incorrect."

User Folder Locations

It needs to be noted that a user may change the location of their common Windows folders such as Desktop, Downloads, Documents and so on. This may cause some problems in a multi-user envronment.


Contents

Character Codes - Using ANSI, ASCII, code pages, control codes, UTF-8 and more!
Creating monthly folders
Escape character - using <, >, |, &, % and ^
File Type Count - Counting file types by extension in folders
Testing for empty strings, spaces and the length of a string - user input, DEFINE
SET: Odd behavior in loops, not updating - SETLOCAL EnableDelayedExpansion
SET: Scope and life of variables - SETLOCAL, ENDLOCAL
Pausing and delaying batch files - pause, ping, choice, sleep, timeout, waitfor
Finding the drive letters from volume labels - vol, find, mountvol, vol, WMIC


Sources and Resources

alt.msdos.batch (Google Grups)
alt.msdos.batch.nt (Google Grups)
An A-Z Index of Windows CMD commands (SS64)
Batch file help and support (Computer Hope)
DOS and Windows command line overview (Computer Hope)
DosTips
Environment Variables (SS64)
Microsoft DOS and Windows command line (Computer Hope)
Ron van der Woude's Scripting Pages
Windows CMD Shell Forum (SS64)
Windows Commands (Microsoft Learn)