Batch script file

advertisement
Batch script file
Additional information
Important DOS Concepts
Batch script file
Objectives…
What is a Batch File?
Stopping a Runaway Batch File
Creating a Batch File
Editing a Batch File
Passing Parameters to a Batch File
Commonly used Batch File Commands
Controlling other applications in Batch File
Chapter 5 adds (37 slides)
CTEC 110
2
Important DOS Commands
Batch script file
copy con
rem
pause
echo
call
choice
goto
if
shutdown
taskmgr
Chapter 5 adds (37 slides)
source of copy is the console
comment
program stops until a key is hit
displays back information to the console
passes control to another batch file
offers user options for execution
transfers control to different part of batch file
checks conditions of two or more variables
powers down Windows
Brings up Task Manager
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 program
Sequence of commands
Time saver
Often 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.bat
CON 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 default
Type in the following commands:
COPY CON o:\look.bat
dir
tree
Ctrl + Z
<enter>
TYPE look.bat
look.bat
Chapter 5 adds (37 slides)
CTEC 110
13
Editing a Batch File
notepad
FileOpen
Dropdown boxchoose “All Files”
Select look.bat
Add these commands at the end
date /t
time /t
Close and save
look.bat
Chapter 5 adds (37 slides)
CTEC 110
14
Editing a Batch File
REM used for comments
Comments are not acted upon
notepad look.bat
Add 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 user
 ECHO string of words
 ECHO 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 file
Change all the REMs to ECHOs
Add PAUSE on the line between the last
ECHO and before DIR
Run the batch program again
LOOK.BAT
Chapter 5 adds (37 slides)
CTEC 110
17
Editing a Batch File
Go back to editing the look.bat file
Add a PAUSE between DIR and Tree
Run the batch program again
LOOK.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.BAT
type 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.bat
ECHO Your first name is %1
ECHO Your last name is %2
Ctrl + Z
name.bat David Sims
Chapter 5 adds (37 slides)
CTEC 110
20
Passing Parameters
Create NAME.BAT on the root of O Drive
COPY 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.bat
REM 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.bat
ECHO OFF
%1 /t
%2 /t
Add
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.bat
ECHO OFF
%1 /t
PAUSE
CALL o:\look.bat
%2 /t
Save and close
Add
Add
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 choice
 ERRORLEVEL variable set to choice 1,2,3, etc
 ERRORLEVEL is 0 on CTRL + C
 ERRORLEVEL 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 y
CHOICE /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 interactive
 The /C switch followed by set of keys and a question mark
•
Default are Y and N keys
 The /S switch keys must match the case of letters
 The /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=None
ECHO 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 line
 The LabeledLine needs to have a “:” as the first
character and no spaces
 Example:
echo off
:START
LabeledLine
echo This is an infinite loop
goto START
Chapter 5 adds (37 slides)
CTEC 110
29
The GOTO command
Create a batch file called infinite.bat
ECHO OFF
:START
ECHO This is an infinite loop
GOTO START
Save and Close infinite.bat
Infinite.bat (execute the batch file)
Chapter 5 adds (37 slides)
CTEC 110
30
The IF command
COPY CON SecretWord.bat
ECHO off
IF not linux==%1 GOTO MESSAGE
ECHO you guessed the right word
GOTO END
:MESSAGE
ECHO 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 Interface
Logoff
Shutdown Computer
Shutdown and Restart Computer
Force Running Applications to close
 Taskmgr (Starts up the Windows Task Manager)
Will start up the Task Manager
Good 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
complete
It is best to understand what they do before
attempting to execute them!!!
Chapter 5 adds (37 slides)
CTEC 110
34
calc
cleanmgr.exe
compmgmt.msc
desk.cpl
devmgmt.msc
diskmgmt.msc
eventvwr
explorer .
firewall.cpl
lusrmgr.msc
mmsys.cpl
mstsc.exe
perfmon
powercfg.cpl
regedit
Chapter 5 adds (37 slides)
Calculator
Disk Cleanup
Computer Management
opens the display properties
Device Manager
Disk Management
Windows Event Log (Event Viewer)
Open explorer with the current folder
selected.
Opens the Windows Firewall settings
Local Users and Groups Administrator
Sound/Recording/Playback properties
Remote Desktop Connection
Opens Reliability and Performance
Monitor
Power management control panel applet
Registry Editor
CTEC 110
35
runas
schtasks
secpol.msc
services.msc
systeminfo
tasklist.exe
whoami /all
winver.exe
wscui.cpl
Chapter 5 adds (37 slides)
Run specific tools and programs
with different permissions than the
user's current logon provides
Enables an administrator to create,
delete, query, change, run and end
scheduled tasks on a local or remote
system.
Local Security Settings
Services control panel
Displays a comprehensive information
about the system
List Processes on local or a remote
machine
Display Current User/Group/Privilege
Information
Find Windows Version
Windows Security Center
CTEC 110
36
END of Additional Information
HOMEWORK
Lab 4
DOS Quiz 4
Chapter 5 adds (37 slides)
CTEC 110
37
Download