Search notes:

cmd.exe: Batch files

Comments

Lines in a batch file can be commented with rem.
rem this is a comment

Exiting batch files

If exit is used to terminate a batch file, the /b option must be specified in order not to terminate the cmd.exe session as well.
@echo line 1
@echo line 2

@exit /b

@echo line 3
@echo line 4
Github repository about-cmd.exe, path: /batch-files/exit.bat
A batch file can «return» an integer value to a caller with exit /b N. The caller can query the returned value with %errorlevel%.

Good practices for batch files

set echo off

I like to start a batch file with
@echo off
The echo off prevents the script file from echoing every command it encounters.
The att sign prevents the echoing of the following command, that is echoing echo off.

setlocal enableextension

It's a good practice to have a setlocal enableextensions somewhere at the top of a batch file.
setlocal prevents the script from clobbering already existing variables or polluting the environment with new variables.
enableextensions turns on command processor extensions which usually are a good thing.

Batch argument/parameter variable expansion

In a batch file, the numbered variables %1 through %9 store the values of the arguments (or parameters) with which the batch file was called.
%0 stores the (relative) path and name of the batch file itself.
With variable expansion, the values of these numbered variables can be subsituted for related values:
Applying ~n and ~x gets the filename (with suffix) of the batch file:
@echo path and filename   : %0
@echo filename with suffix: %~n0%~x0
Github repository about-cmd.exe, path: /variables/expansion/name-of-bat-file.bat
Similarly, the (absolute) directory can be found by applying ~dp on a variable:
@rem
@rem  Note the trailing backslash
@rem

@set  current_dir=%~dp0
@echo the currenct directory is %current_dir%
Github repository about-cmd.exe, path: /variables/expansion/current-directory.bat
Table of modifiers
%~1 Remove quotes
%~f1 Full (absolute) path (of possibly relative path)
%~d1 Drive letter
%~p1 Path (without drive or file)
%~n1 File name
%~x1 Extension of file name
%~s1 Like %~f1 but with short names
%~a1 File attributes
%~t1 Date and time of file
%~z1 Size of file
%~$PATH:1 First directory in %PATH% where %1 is found
With such modifiers, the working directory of a batch file can be set to the directory where the batch file is located so that the batch file can be executed from any location:
@rem
@rem  CD into «own» directory
@rem

@cd %~dp0

@echo Current directory is %cd%
@echo %time% >> run-in-own-dir-run-times
Github repository about-cmd.exe, path: /batch-files/run-in-own-dir.bat
See also the tilde in variable names.

Name of bat file

These variable expansion technique allows to assign the name of a script (without .bat suffix) to a variable which then can be used, for example, for debugging messages:
@echo off

rem
rem Assign name of bat file (without .bat
rem suffix) to variable bat_file:
rem
set bat_file=%~n0

echo Bat file is %bat_file%
Github repository about-cmd.exe, path: /batch-files/script-name.bat

Pausing for a while

Sometimes, it's necessary to pause execution of the script for a few seconds (or longer). timeout.exe is explicitly created for that.
The following line pauses for 5 seconds:
timeout /t 5 > nul

Getting input from a user

choice.exe presents the user of script with a list of items from which the user can choose from.

See also

batch file arguments (stored in %1, %2 etc.) and passing arguments to a batch file
Execute a PowerShell script in a cmd.exe batch file
Executing batch files with Oracle jobs.

Index