Batch script file
Additional information
Objectives…What is a Batch File?Stopping a Runaway Batch FileCreating a Batch FileEditing a Batch FilePassing Parameters to a Batch FileCommonly used Batch File CommandsControlling other applications in Batch File
Chapter 5 adds (37 slides) CTEC 110 2
Important DOS ConceptsBatch script file
Important DOS CommandsBatch script file
copy consource of copy is the console
rem comment
pause program stops until a key is hit
echo displays back information to the console
call passes control to another batch file
choice offers user options for execution
goto transfers control to different part of batch file
if checks conditions of two or more variables
shutdown powers down Windows
taskmgr Brings up Task Manager
Chapter 5 adds (37 slides) CTEC 110 3
Objectives Create useful batch files Define the role of batch files in the DOS environment Identify different batch file commands and their use Create batch files using the DOS COPY command
List those steps required to create a file from the CONSOLE (keyboard)
Understanding how those commands work Control the execution of those commands What else can we do with batch files? Be able to reference the DOS Command Index:http://www.easydos.com/dosindex.html
Chapter 5 adds (37 slides) CTEC 110 4
What is a Batch File?
Executable programSequence of commandsTime saverOften used for automation at startup
Chapter 5 adds (37 slides) CTEC 110 5
Simple Example
Chapter 5 adds (37 slides) CTEC 110 6
Other Examples
Can be long and tedious!!Take a look at GO.BAT
On the root of the N Drive
Chapter 5 adds (37 slides) CTEC 110 7
Chapter 5 adds (37 slides) CTEC 110 8
Chapter 5 adds (37 slides) CTEC 110 9
Chapter 5 adds (37 slides) CTEC 110 10
Stopping a Runaway Batch File
Ctrl + C Stops a batch file“Terminate batch job (Y/N)?”
Answer this question with a “Y” (Yes)…
Chapter 5 adds (37 slides) CTEC 110 11
Creating a Batch File
COPY CON example.batCON stands for console (the keyboard)
whereby the commands entered into keyboard will automatically be put into the file example.bat
Ctrl + Z places an end-of-file character in the file and stores it on disk.
Can also use notepad in Windows
Chapter 5 adds (37 slides) CTEC 110 12
Creating a Simple Batch File
Make the root of the O drive your defaultType in the following commands:
COPY CON o:\look.batdirtreeCtrl + Z<enter>TYPE look.batlook.bat
Chapter 5 adds (37 slides) CTEC 110 13
Editing a Batch File
notepadFileOpenDropdown boxchoose “All Files”Select look.batAdd these commands at the end
date /ttime /t
Close and savelook.bat
Chapter 5 adds (37 slides) CTEC 110 14
Editing a Batch File
REM used for comments Comments are not acted upon
notepad look.batAdd at the beginning of the file:
REM This file was created by <Your Name>
REM On <Today’s Date>
Close and save
look.bat
Chapter 5 adds (37 slides) CTEC 110 15
Editing a Batch File
ECHO displays information back to the userECHO string of wordsECHO by itself displays a blank line
Usually displays the ECHO setting (ON or OFF)Try using a period (.) instead (put a space before)
PAUSE is for stopping the program until the user hits a key
Chapter 5 adds (37 slides) CTEC 110 16
Editing a Batch File
Go back to editing the look.bat fileChange all the REMs to ECHOsAdd PAUSE on the line between the last
ECHO and before DIR
Run the batch program againLOOK.BAT
Chapter 5 adds (37 slides) CTEC 110 17
Editing a Batch File
Go back to editing the look.bat fileAdd a PAUSE between DIR and Tree
Run the batch program againLOOK.BAT
Chapter 5 adds (37 slides) CTEC 110 18
Looking at a Real Batch File
Check out the WUGXP folder on the G drive for the batch file called GO.BATtype WUGXP\go.bat
Chapter 5 adds (37 slides) CTEC 110 19
Passing Parameters
BatchFileName 1stPar 2ndPar 3rdPar …
Parameters in file: %1 %2 %3 . . .Create NAME.BAT on the root of O Drive
COPY CON name.batECHO Your first name is %1ECHO Your last name is %2Ctrl + Z
name.bat David Sims
Chapter 5 adds (37 slides) CTEC 110 20
Passing Parameters
Create NAME.BAT on the root of O DriveCOPY CON times.bat
%1 /t
%2 /t
Ctrl + Z
times.bat date time
Chapter 5 adds (37 slides) CTEC 110 21
The ECHO command The ECHO command syntax:
ECHO [ON | OFF]ECHO [message]
Displays messages, or turns command-echoing on or off.Type ECHO without parameters to display the current echo setting
Examples:ECHO
• ECHO is on.ECHO This is a message!
• This is a message
Chapter 5 adds (37 slides) CTEC 110 22
The ECHO command
ECHO OFF - turns off the display of commands
COPY CON rems.batREM This is a comment line
ECHO OFF
REM This comment line will not be seen
CTRL + Z
rems.bat
Chapter 5 adds (37 slides) CTEC 110 23
The ECHO command
Use notepad to adjust times.batECHO OFF Add
%1 /t
%2 /t
Save and close
times.bat date time
Chapter 5 adds (37 slides) CTEC 110 24
The CALL command
The CALL command syntax:CALL [d:][path]batchfilename [options]
Calls another batch file, then returns to current batch file to continue
Example:CALL go.bat
Chapter 5 adds (37 slides) CTEC 110 25
The CALL command
Use notepad to adjust times.batECHO OFF
%1 /t
PAUSE Add
CALL o:\look.batAdd
%2 /tSave and close
times.bat date time
Chapter 5 adds (37 slides) CTEC 110 26
The CHOICE command The CHOICE command syntax:
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
This tool allows users to select one item from a list of choices and returns the index of the selected choiceERRORLEVEL variable set to choice 1,2,3, etcERRORLEVEL is 0 on CTRL + CERRORLEVEL is 255 when invalid choice is made
EXAMPLES:CHOICE /?CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."CHOICE /T 10 /C ync /CS /D yCHOICE /C ab /M "Select a for option 1 and b for option 2."CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Chapter 5 adds (37 slides) CTEC 110 27
The CHOICE command(Second look)
The CHOICE command syntax (second look…):CHOICE [/C:[:]list of keys] [/N] [/S] [/T[:]choice,number] [text]
CHOICE is used to make batch files interactiveThe /C switch followed by set of keys and a question mark
• Default are Y and N keysThe /S switch keys must match the case of lettersThe /T switch is the number of seconds a user has to press keys
Examples:CHOICE /C:ABCDN /N /T:N,10 Format drive A:, B:, C:, D: or None?IF ERRORLEVEL 1 SET DRIVE=drive A:IF ERRORLEVEL 2 SET DRIVE=drive B:IF ERRORLEVEL 3 SET DRIVE=drive C:IF ERRORLEVEL 4 SET DRIVE=drive D:IF ERRORLEVEL 5 SET DRIVE=NoneECHO You chose to format %DRIVE%
Format drive A:, B:, C:, D: or None?
Chapter 5 adds (37 slides) CTEC 110 28
The GOTO command
The GOTO command syntax:GOTO (label)
Transfer control to a labeled lineThe LabeledLine needs to have a “:” as the first character and no spaces
Example:echo off:START LabeledLineecho This is an infinite loop goto START
Chapter 5 adds (37 slides) CTEC 110 29
The GOTO command
Create a batch file called infinite.batECHO OFF
:START
ECHO This is an infinite loop
GOTO STARTSave and Close infinite.bat
Infinite.bat (execute the batch file)
Chapter 5 adds (37 slides) CTEC 110 30
The IF command
COPY CON SecretWord.batECHO offIF not linux==%1 GOTO MESSAGEECHO you guessed the right wordGOTO END:MESSAGEECHO you made a bad guess:END
SecretWord.bat linux
Chapter 5 adds (37 slides) CTEC 110 31
Calling Other Programs
Shutdown (Has a variety of options)
Display a Graphical User InterfaceLogoffShutdown ComputerShutdown and Restart ComputerForce Running Applications to close
Taskmgr (Starts up the Windows Task Manager)
Will start up the Task ManagerGood for troubleshooting Windows
Chapter 5 adds (37 slides) CTEC 110 32
shutdown -l
Chapter 5 adds (37 slides) CTEC 110 33
Calling Other Programs
Try to run some of the following commands on your own…Some of these may take some time to
completeIt is best to understand what they do before
attempting to execute them!!!
Chapter 5 adds (37 slides) CTEC 110 34
Chapter 5 adds (37 slides) CTEC 110 35
calc Calculatorcleanmgr.exe Disk Cleanupcompmgmt.msc Computer Managementdesk.cpl opens the display propertiesdevmgmt.msc Device Managerdiskmgmt.msc Disk Managementeventvwr Windows Event Log (Event Viewer)explorer . Open explorer with the current folder
selected.firewall.cpl Opens the Windows Firewall settingslusrmgr.msc Local Users and Groups Administratormmsys.cpl Sound/Recording/Playback propertiesmstsc.exe Remote Desktop Connectionperfmon Opens Reliability and Performance
Monitorpowercfg.cpl Power management control panel appletregedit Registry Editor
Chapter 5 adds (37 slides) CTEC 110 36
runas Run specific tools and programs with different permissions than the user's current logon provides
schtasks Enables an administrator to create, delete, query, change, run and end scheduled tasks on a local or remote system.
secpol.msc Local Security Settingsservices.msc Services control panelsysteminfo Displays a comprehensive information
about the systemtasklist.exe List Processes on local or a remote
machinewhoami /all Display Current User/Group/Privilege
Informationwinver.exe Find Windows Versionwscui.cpl Windows Security Center
END of Additional Information
HOMEWORK
Lab 4DOS Quiz 4
Chapter 5 adds (37 slides) CTEC 110 37