Introduction to Windows Batch Commands and Scripting
This article provides a comprehensive beginner-friendly guide to Windows batch (BAT) commands, covering common file operations, control structures, variable handling, and practical code examples for automating tasks on Windows virtual machines.
Recently I needed to automate configuration tasks on a Windows virtual machine using BAT commands, so I studied batch scripting and prepared this introductory guide.
Batch commands are DOS/Windows scripts with the .bat extension; they include a set of symbols and commands divided into three categories, which are introduced below.
Common file and system commands: Dir (list files), md (make directory), cd (change directory), copy (copy files), del (delete files), ren (rename), type (display file content), format (format disk), discopy (duplicate disk), deltree (delete directory tree), mem (show memory), chkdsk (check disk), cls (clear screen), time (show system time), date (show system date), var (show OS version).
Typical batch programming commands: PAUSE to halt execution ("Press any key to continue"), echo %errorlevel% to view the return code, CALL to invoke another batch file, and setlocal enabledelayedexpansion for delayed variable expansion. The following examples illustrate variable behavior with and without delayed expansion:
@echo off set a=4 set a=5 & echo %a% pause
Output is 4 because %a% is expanded before the second assignment. With delayed expansion:
@echo off&setlocal enabledelayedexpansion set a=4 set a=5 & echo !a! pause
Now the output is 5.
Variable assignment (SET) examples:
set var=123 echo %var% set /p var=请输入你的名字 ; /p prompts for input set /a var=3-1 ; /a evaluates arithmetic expression
Conditional statements (IF): checking existence (if exist d:\test.txt), string equality (if "abc"=="xyz"), numeric equality (if 1 equ 2), and defined variables (if defined str).
FOR loop syntax: FOR %%variable IN (set) DO command [command-parameters] with parameters /d (directories), /r (recursive), /l (numeric range), /f (file/content parsing). Example usages:
@echo off for /d %%i in (*) do @echo %%i pause
Lists all sub‑directories.
@echo off for /r c: %%i in (*.exe) do @echo %%i pause
Recursively lists all .exe files on C:.
@echo off for /l %%i in (1,1,5) do @echo %%i pause
Prints numbers 1 through 5.
File‑content parsing examples:
for %%i in (a.txt) do echo %%i ; show file name for /f %%i in (a.txt) do echo %%i ; show each line for /f "delims= " %%i in (a.txt) do echo %%i ; first column for /f "tokens=2 delims= " %%i in (a.txt) do echo %%i ; second column for /f "tokens=2,3 delims= " %%i in (a.txt) do echo %%i %%j ; second and third columns
Batch script parameters: %0, %1 … %9, %* represent the script name and arguments passed from the command line.
Common batch symbols: @ and echo for output control, redirection operators (> , >> , <), pipe (|), escape (^), logical operators (&, &&, ||), variable syntax (%%, %CD%, %DATE%, %HOMEPATH%, %PATH%), and comment markers (:: and REM).
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.