0% found this document useful (0 votes)
54 views6 pages

Bat Tutorial

This document is a tutorial on basic batch commands, covering command execution output, file and directory operations, conditional and looping commands, user input commands, process and system commands, network commands, and registry management. It provides explanations for each command and includes examples for practical usage. The tutorial concludes with an example batch file that combines several commands.

Uploaded by

Arun R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

Bat Tutorial

This document is a tutorial on basic batch commands, covering command execution output, file and directory operations, conditional and looping commands, user input commands, process and system commands, network commands, and registry management. It provides explanations for each command and includes examples for practical usage. The tutorial concludes with an example batch file that combines several commands.

Uploaded by

Arun R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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

You might also like