A batch file is a type of script that contains a list of commands. These commands are executed in sequence and can be used to automate processes.Batch files are run by the COMMAND.COM program, which is part of DOS and Windows. Therefore, batch files can only be run within the Windows operating system. Macintosh and Unix have other scripting tools, such as AppleScript and Unix shell commands, that can be used for similar tasks. Because batch files contain executable commands, it is important not to open unknown batch files on your hard disk or in e-mail attachments.
It is a text file containing a series of commands to be executed by the command interpreter.
file extension for batch file—— .bat , .cmd , .btm
Simple example for batch file—–
This example batch file displays “Hello World!”, prompts and waits for the user to press a key, and terminates.
@ECHO off
ECHO Hello World!
PAUSE
To execute this,first save this file with file extension .bat in notepad.This file is called bath file.
After execution this will be displayed like this–
Hello World!
Press any key to continue . . .
EXPLANATION—–
The interpreter executes each line in turn, starting with the first. The @ symbol at the start of the line turns off the prompt from displaying that command. The command ECHO off turns off the prompt permanently, or until it is turned on again. Then the next line is executed, the ECHO Hello World! command outputs Hello World!, as only off and on have special functions. Then the next line is executed, the PAUSE command displays Press any key to continue . . . and pauses the script’s execution until a key is pressed, when the script terminates as there are no more commands. In Windows, if the script is run within a Command Prompt window, the window remains open at the prompt as in MS-DOS, otherwise the command prompt windows closes on termination (unless the batch file has a command to prevent this).
Advanced window batch file example for conditional shutdown—
@echo off
color 0A
title Conditional Shutdown.
:start
echo Welcome, %USERNAME%
echo What would you like to do?
echo.
echo 1. Shutdown in specified time
echo 2. Shutdown at a specified time
echo 3. Shutdown now
echo 4. Restart now
echo 5. Log off now
echo 6. Hibernate now
echo.
echo 0. Quit
echo.
set /p choice=”Enter your choice: ”
if “%choice%”==”1” goto shutdown
if “%choice%”==”2” goto shutdown-clock
if “%choice%”==”3” shutdown.exe -s -f
if “%choice%”==”4” shutdown.exe -r -f
if “%choice%”==”5” shutdown.exe -l -f
if “%choice%”==”6” shutdown.exe -h -f
if “%choice%”==”0″ exit
echo Invalid choice: %choice%
echo.
pause
cls
goto start
:shutdown
cls
set /p min=”Minutes until shutdown: ”
set /a sec=60*%min%
shutdown.exe -s -f -t %sec%
echo Shutdown initiated at %time%
echo.
goto cancel
:shutdown-clock
echo.
echo the time format is HH:MM:SS (24 hour time)
echo example: 14:30:00 for 2:30 PM
echo.
set /p tmg=enter the time that you wish the computer to shutdown on:
schtasks.exe /create /sc ONCE /tn shutdown /st %tmg% /tr “shutdown.exe -s -t 00″
echo shutdown initiated at %tmg%
echo.
:cancel
set /p cancel=”Type cancel to stop shutdown: ”
if not “%cancel%”==”cancel” exit
shutdown.exe -a
cls
schtasks.exe /end /tn shutdown
cls
schtasks.exe /delete /tn shutdown
cls
echo Shutdown is cancelled.
echo.
pause
exit
Save this as anoop.bat in notepad.Now open this file you will find like this—
welcome username
What would you like to do?
1. shutdown in specific time
2. shutdown at a specific time
3. shutdown now
4. Restart now
5. Log off now
6. Hibernate now
0. quit
enter your choice
Now chose your choice & press enter and get the result.
I think it contains enjoyable content. Now please rate this and like this on facebook.