Catalog / Windows Batch Scripting Cheatsheet

Windows Batch Scripting Cheatsheet

A comprehensive cheat sheet covering essential Windows Batch scripting commands, syntax, and techniques for automating tasks.

Basic Commands & Syntax

Essential Commands

ECHO

Displays messages on the console.
Example: ECHO Hello, world!

@

Suppresses command echoing.
Example: @ECHO OFF

SET

Defines or modifies environment variables.
Example: SET myVar=value

PAUSE

Pauses script execution and waits for user input.
Example: PAUSE

CLS

Clears the console screen.
Example: CLS

EXIT

Exits the batch script.
Example: EXIT

REM or ::

Adds comments to the script.
Example: REM This is a comment or :: This is also a comment

Variables

SET variable_name=value - Assigns a value to a variable.
%variable_name% - Accesses the variable’s value.

Example:

SET name=John
ECHO Hello, %name%!

SET /P variable_name=Prompt message: - Prompts the user for input.

Example:

SET /P input=Enter your name: 
ECHO Your name is %input%

!variable_name! - Enables delayed expansion (needed within loops).

Example:

SETLOCAL EnableDelayedExpansion
FOR /L %%i IN (1,1,5) DO (
  SET /A num+=1
  ECHO !num!
)
ENDLOCAL

Operators

Arithmetic

+, -, *, /, % (Modulo)

Comparison

EQU (equal), NEQ (not equal),
LSS (less than), LEQ (less than or equal),
GTR (greater than), GEQ (greater than or equal)

Logical

AND, OR, NOT (within IF statements)

Control Flow

Conditional Statements

IF condition (commands) ELSE (commands) - Executes commands based on a condition.

Example:

IF %errorlevel% NEQ 0 (
  ECHO An error occurred.
) ELSE (
  ECHO Script completed successfully.
)

IF EXIST filename (commands) - Checks if a file exists.

Example:

IF EXIST myfile.txt (
  ECHO File exists.
) ELSE (
  ECHO File does not exist.
)

IF DEFINED variable (commands) - Checks if a variable is defined.

Example:

IF DEFINED myVar (
  ECHO Variable is defined.
) ELSE (
  ECHO Variable is not defined.
)

Looping

FOR %variable IN (set) DO command - Iterates over a set of items.
FOR /L %variable IN (start,step,end) DO command - Iterates a numerical range.
FOR /F %variable IN (file) DO command - Parses a file line by line.

Examples:

FOR %%i IN (apple banana cherry) DO ECHO %%i

FOR /L %%i IN (1,1,5) DO ECHO %%i

FOR /F "tokens=*" %%i IN (myfile.txt) DO ECHO %%i

GOTO and Labels

GOTO label - Jumps to a specified label.
:label - Defines a label.

Example:

:start
ECHO Doing something...
GOTO end

:end
ECHO Script finished.

File and Directory Operations

File Manipulation

COPY

Copies files.
Example: COPY file.txt newfile.txt

MOVE

Moves files.
Example: MOVE file.txt newlocation\file.txt

DEL

Deletes files.
Example: DEL file.txt

RENAME or REN

Renames files.
Example: REN oldfile.txt newfile.txt

TYPE

Displays the content of the file.
Example: TYPE file.txt

Directory Manipulation

MKDIR or MD

Creates a new directory.
Example: MKDIR newdirectory

RMDIR or RD

Removes a directory.
Example: RMDIR directorytodelete

CHDIR or CD

Changes the current directory.
Example: CD newdirectory

DIR

Lists files and directories in the current directory.
Example: DIR

File Attributes

ATTRIB

Displays or changes file attributes.
Example: ATTRIB +R file.txt (sets read-only attribute)

Advanced Techniques

Subroutines

CALL :label - Calls a subroutine.
:label - Defines the subroutine.
EXIT /B - Exits the subroutine (returns to the caller).

Example:

CALL :mySubroutine
ECHO Back in main script
EXIT

:mySubroutine
ECHO Inside subroutine
EXIT /B

Error Handling

IF %ERRORLEVEL% NEQ 0 - Checks the error level of the last command.

Example:

command_that_might_fail
IF %ERRORLEVEL% NEQ 0 (
  ECHO An error occurred.
  EXIT /B %ERRORLEVEL%
)

Command Chaining

command1 && command2 - Executes command2 only if command1 succeeds.
command1 || command2 - Executes command2 only if command1 fails.

Example:

dir && echo Directory listing successful
del non_existent_file || echo File not found

Piping and Redirection

>

Redirects output to a file (overwrites existing).
Example: DIR > filelist.txt

>>

Redirects output to a file (appends).
Example: ECHO Additional info >> filelist.txt

|

Pipes output from one command to another.
Example: DIR | FIND "txt"