BAT TUTORIAL
Basic Commands
Hide Command Execution Output
• @echo off
echo Hello, World!
Explanation: @echo off hides unnecessary text output, and echo prints "Hello,
World!" on the screen.
• Display Text on the Screen
echo Welcome to my Batch Script!
Explanation: Prints "Welcome to my Batch Script!" on the screen.
• Add Comments
rem This is a comment
:: This is also a comment
Explanation: rem or :: adds comments that are ignored by the system.
• Pause Execution
pause
Explanation: Pauses the script and waits for the user to press any key.
• Clear the Screen
cls
Explanation: Clears everything from the command prompt screen.
• Exit the Script
exit
Explanation: Closes the batch file or command prompt window.
File and Directory Operations
• List Files and Directories
dir
Explanation: Shows all files and folders in the current directory.
• Change Directory
cd C:\Users
Explanation: Moves to the C:\Users directory.
• Create a Folder
md MyFolder
mkdir MyFolder
Explanation: Creates a new folder named "MyFolder".
• Delete a Folder
rd MyFolder
rmdir MyFolder
Explanation: Deletes the "MyFolder" directory.
• Delete a File
del myfile.txt
Explanation: Deletes myfile.txt.
• Copy a File
copy myfile.txt D:\Backup\
Explanation: Copies myfile.txt to D:\Backup\.
• Copy a Folder (with subfolders)
xcopy C:\MyFolder D:\Backup\ /s /e
Explanation: Copies MyFolder and its contents to D:\Backup.
• Move a File
move myfile.txt D:\Backup\
Explanation: Moves myfile.txt to D:\Backup\.
• Rename a File
ren oldname.txt newname.txt
Explanation: Renames oldname.txt to newname.txt.
Conditional and Looping Commands
• If Condition (Check if a File Exists)
if exist myfile.txt echo File exists!
Explanation: If myfile.txt exists, it prints "File exists!".
• If Condition (Check if a File Does Not Exist)
if not exist myfile.txt echo File does not exist!
Explanation: If myfile.txt does not exist, it prints "File does not exist!".
• For Loop (Loop Through Files)
for %%f in (*.txt) do echo Found file: %%f
Explanation: Loops through all .txt files and prints their names.
• Goto (Jump to a Label)
@echo off
goto start
:label1
echo This is Label 1
exit
:start
echo This is Start
goto label1
Explanation: goto jumps to labels within the script.
User Input Commands
• Ask User for Input
set /p username=Enter your name:
echo Hello, %username%!
Explanation: Asks for a name and prints a greeting.
• Create a Menu with Choices
choice /c 123 /m "Choose an option: 1, 2, or 3"
if %errorlevel%==1 echo You chose 1
if %errorlevel%==2 echo You chose 2
if %errorlevel%==3 echo You chose 3
Explanation: Creates a simple menu where the user can choose 1, 2, or 3.
Process and System Commands
• List Running Processes
tasklist
Explanation: Shows all currently running processes.
• Kill a Process
taskkill /im notepad.exe /f
Explanation: Forcefully closes Notepad.
• Shutdown the Computer
shutdown /s /t 30
Explanation: Shuts down the PC in 30 seconds.
• Restart the Computer
shutdown /r /t 30
Explanation: Restarts the PC in 30 seconds.
• Open a Program
start notepad.exe
Explanation: Opens Notepad.
Network Commands
• Check IP Configuration
ipconfig
Explanation: Shows your network settings.
• Ping a Website
ping google.com
Explanation: Tests the connection to Google.
• Check Active Network Connections
netstat -an
Explanation: Displays network connections.
• Map a Network Drive
net use Z: \\server\share
Explanation: Maps a network folder to drive Z:.
Registry and System Configuration
• Add a Registry Key
reg add HKCU\Software\MyApp /v MyValue /t REG_SZ /d "Hello"
Explanation: Adds a registry entry under MyApp.
• Delete a Registry Key
reg delete HKCU\Software\MyApp /v MyValue
Explanation: Deletes the registry key.
Example Batch File (Combining Commands)
@echo off
title My First Batch Script
echo Hello! Welcome to my batch script.
set /p name=What is your name?
echo Nice to meet you, %name%!
pause
exit
Explanation:
Prints a welcome message
Asks for the user's name
Greets the user
Waits for key press
Exits
Practical example