Lab 11 - Batch files Exercise 1 – Create Crea te your first batch file..................................................... file.....................................................................................2 ................................2 Exercise 2 – Create a directory structure...................................................................................2 Exercise 3 – Wildcards example..................................................... example................................................................................................ ........................................... 3 Exercise 4 – Wildcards example..................................................... example................................................................................................ ........................................... 3 Exercise 5 – Wildcards example..................................................... example................................................................................................ ........................................... 3 Batch commands...................................... commands............................................................. .............................................. ........................................................... .................................... 4 Exercise ! Wildcards Example..................................................... Example................................................................................................ ........................................... 5 Exercise " – Copy from one dri#e to another............................................................................5 Exercise $ – Creatin% a &irectory structure...............................................................................5 Exercise ' – Copyin% and mo#in% files..................... f iles............................................ ............................................................. ...................................... ... Exercise 1( – )emo#in% directories.......................................................................................... Exercise 11 – *sin% the EC+, C,--/&.................................. C,--/&......................................................... .......................................... ................... Exercise 12 – *sin% the call command.................................... command........................................................... .................................................. ........................... " Exercise 13 – )eplaceable parameters................................................................. parameters......................................................................................." ......................" Exercise 14 – )eplaceable parameters................................................................. parameters......................................................................................." ......................" Exercise 15 – )eplaceable parameters................................................................. parameters.......................................................................................$ ......................$ Exercise 1 – )eplaceable parameters................................................................. parameters.......................................................................................$ ......................$ Exercise 1" – 0aths........................................................ 0aths............................................................................... .................................................. ........................... ........1( ........ 1( )e#ision from past exams............................................ exams.................................................................... ........................................................1( ................................1(
)ef .chebucto.ns.caa21&,6BatBasic.html Batch files are files that contain a list of commands. 7t executes the commands ithin the file hen you execute the batch file. 8he commands execute one by one. command ithin a batch file can be used to startrun another application9 for example you could start -6 Word. Why batch files are useful?
batch file could be used to run fre:uently run commands9 deletin% a series of files9 mo#in% files9 copyin% files etc. simple batch file does not re:uire any special pro%rammin% sills and can be done by users ho only no &,6 commands. n example of a ell non batch file is the autoexec.bat9 hich is a simple batch file loaded each time the computer is loaded on -6!&,6 and early Windos computers. 8his batch file contained all the necessary commands and pro%rams used to run -6!&,6 and Windos Windos each time the computer booted. *se your VMware accounts to do this lab sheet because you ill need to sa#e your or.
Important: ;ou should practice the &,6 commands at the command line to ensure you no ho they or before puttin% them into the batch file.
1
Exercise 1 !reate your first batch file 1. ,pen an -6!&,6 command indo. 2. Chan%e to the root directory of the C dri#e and create a directory called Batch"iles . ;ou can use this directory to sa#e your batch files. 3. Chan%e into Batchfiles folder. Create a ne file called Ex1#bat usin% notepa$ . 4. 8o do this type notepa$ ex1#bat at the command line. 5. 8ype in the folloin% lines into the file and 6a#e. Each of the lines belo is a command. cls ver pause dir c:\windows
. Before continuin%9 can you remember hat the effect of each of the 4 commands that you ha#e typed into the file< 7f not – loo them up in the help. ". -ae sure you are in the Batchfiles folder. $. =ey in &7) and M%&E '()E you see a file called E>1.B8 '. t the prompt9 type in the name of the batch file i.e. Ex1 and press Enter. 1(. *nderstand the outcome. 1(. 0ut the command *E!+, ,"" as the first line into Ex1.bat. )un the batch file a%ain. What is effect of this statement< ................................................................................................................................... ..................................................................................................................................
Important: 'a0e your batch files to the Batch"iles fol$er Exercise !reate a $irectory structure Write a batch pro%ram called E.#B%/ that creates the folloin% directory structure on dri#e C. C?
CC,*/86
@etters
Email
C,@@EAE
a#a
pps
Chec either in Explorer or at the prompt that the directory structure shon abo#e has been created.
2
dd comments to all your batch files usin% )em command at start of the line. 8he comments should indicate the folloin% 0ro%ram purpose uthor ! &ate ritten )un the batch file E.#B%/. re you no in a different folder to the one you ere in before you ran the batch file. ;ou should not mo0e $irectory as a result of runnin% a batch file unless that is part of the batch file.
Exercise Wil$car$s example Write a batch pro%ram called E.#B%/ that lists all files hich start ith the letter - and ha#e the extension of .E>E on dri#e C. 7t should search from the root don throu%h all the subdirectories. If you want to terminate a batch file $urin2 execution3 press !trl 4 !#
Exercise 5 Wil$car$s example Write a batch pro%ram called E.5#B%/ that lists all files that match any of the folloin% criteria ithin the root of the C dri#e and don throu%h its subdirectories a
Diles ith an extension of C,- and ha#e 4 letters in the filename. e.%.9 chcp.com mode.com etc.
b
E>E files hose 2nd letter is 7
e.%.9 W I /
[email protected]>E & I A6.E>E etc.
-ae sure the output does not scroll up the screen too :uicly. 0ut a pause command in beteen parts a and b
Exercise 6 Wil$car$s example Write a batch pro%ram called E.6#B%/ that lists all files that match any of the folloin% criteria ithin the Win$ows folder and don throu%h its sub directories a Diles that ha#e an extension that starts ith the letter C. b @ist folders and files that dont ha#e any extension
3
Batch comman$s ust lie all -6!&,6 commands9 all batch file commands are not case sensiti#e. +oe#er9 in the belo listin% e ha#e listed all commands in all caps to help you identify hat is a command and hat is not. *
&oes not echo bac the text after the F symbol. 8his is most commonly used as *E!+, ,"" to pre#ent any of the commands in the batch file from bein% displayed9 Gust the information needed.
71
8he percent folloed by a numeric #alue9 be%innin% ith one9 allos users to add #ariables ithin a batch file. Example of hat can be used in a batch file. echo +ello 71
When the abo#e one!line batch file is created9 add your name after the batch file. Dor example9 typin% myname Hbein% the name of the bat file and then your name myname bob
ould output
+ello bob
8ote: 8his can be extended to I29 I39 and so on. :L%BEL By addin% a colon in front of a ord9 such as L%BEL9 you create a cate%ory9 more commonly non as a label. 8his allos you to sip to certain sections of a batch file such as the end of the batch file. lso see 9,/,. !%LL
8his is used to run another batch file ithin a batch file. When the batch file that is called is completed9 the remainder of the ori%inal batch file is completed. /ote if the batch file does not exist it ill %i#e an error messa%e.
!L'
Clear 6creen
E!+,
Will echo a messa%e in the batch file. E!+, +ello Worl$; ill print +ello World on the screen hen executed. /ote if you ha#e not typed *E!+, ,"" at the be%innin% of the file this ill also print JEC+, +ello WorldJ and J+ello WorldJ. 7f you ould Gust lie to create a blan line9 type E!+,# Haddin% the full stop creates an empty line.
E.I/
Exits out of the &,6 indo if the batch file is runnin% from Windos.
9,/, *sed to %o to a certain label9 such as @BE@. n example of A,8, ould be to A,8, L%BEL E/&. Dor an example of this see runnin% different pro%rams. . I"
*sed to chec for a certain condition if the condition exists. 7f that condition exists it ill perform that function.
<%('E
0rompt the user to press any ey to continue.
)EM
llos you to place comments into the batch file ithout displayin% or executin% that line hen the batch file is run.
4
'+I"/
Chan%es the position of replaceable parameters in a batch pro%ram. .
Exercise = - Wil$car$s Example Write a batch pro%ram called E.=#B%/ ;our batch file should do the folloin% Clear the screen dd a line to chan%e directory to the C?Windos directory dd a line to list all files ith an extension of 8>8 in that directory. H-ae sure you put some text files in here if there are none already.
Exercise > !opy from one $ri0e to another ;ou ill be usin% the C dri#e and a different partition on the dri#e in this batch file. -y example uses partition E. Create another partition on your Kmare computer if you ha#e not already done so. Write a batch pro%ram called E.>#B%/. 7t should carry out the folloin% tass
Copy all files ith an extension of ps1 from ri0e ! to ri0e E . H*se either the copy command or the xcopy command. 8he xcopy command is the better one to use here. Dor example files lie profile.ps1 or types .ps1 Before the files are copied across use the echo command to tell the user you are doin% so.
)un the batch pro%ram. Mo$ifications
dd the pause command to your batch file. 8his line should be inserted before the copy is carried out. What effect does this ha#e on the pro%ram<
Exercise @ !reatin2 a irectory structure Write a batch pro%ram called E.@#B%/ that creates the folloin% directory structure on dri#e C. ;ou ha#e already created all folders except the folder called ,p 6ys. ;ou should not create the files. C? C,@@EAE
CC,*/86 'ample#txt
Example#txt
@etters
Email
,p 6ys
a#a
pps
report#$oc
5
Chec either in Explorer or at the prompt that the directory structure shon abo#e has been created. Create the three files shon in the dia%ram abo#e usin% the LLLLL LL. command.
Exercise A !opyin2 an$ mo0in2 files 8ae it that you ill be in the &,6 prompt at the root directory of &ri#e C hen you run the bat ch file.
Write a batch pro%ram called E.A#B%/ that ill a.
Chan%e directory to the %ccounts folder.
b.
8here are to files Example.txt and 6ample.txt in the directory %ccounts. Write the &,6 commandHs to copy the to files to the directory called a0a#
c.
&elete the file Example#txt from the %ccounts folder. 0rompt the user to confirm before deletin% the file.
d.
Write the &,6 command to mo#e the file )eport#$oc to the folder %pps and call it a different name )eport,l$#$oc#
Exercise 1C )emo0in2 $irectories Write a batch pro%ram called E.1C#B%/ that remo0es the %ccounts directory and all its subdirectories in the abo#e structure. Chec that it successfully remo#es all the directories shon abo#e.
Exercise 11 (sin2 the E!+, !,MM%8 Write a batch pro%ram called E.11#B%/ that produces the folloin% output to the screen. /o snoflae feels %uilty in an a#alanche. /ot e#erythin% that is important can be measured. /ot e#erythin% that can be measured is important.
Ensure the screen is cleared and that there is a blan line under each line of output. E!+, ,"" Hturns echoin% of commands off. Hprints a blan line to the screen. E!+,# !L' Hclears the screen.
Exercise 1 (sin2 the call comman$ Write a batch pro%ram called E.1a#B%/ that contains the folloin% statements c$## $ir 6a#e this pro%ram. What ill be the effect of these commands<......................................................................... Create another pro%ram called Ex1#bat hich ill a Chan%e to the pps folder b Calls pro%ram Ex12a.bat c Echo your name to the screen What ill be the effect of this pro%ram<......................................................................... &id it execute the &,6 commands in both batch files< ................................................. What directory are you in no < ................... +o did you %et there<....................... .....................................................................................................................................
(sin2 replaceable parameters
Exercise 1 )eplaceable parameters 0ro%rams can accept replaceable parameters at the prompt. H8he replaceable parameters are I1 throu%h to I'. Write a batch file called Ex1#Bat hich accepts one parameterM the parameter is a persons name. 8he pro%ram ill then display the ord N+elloO folloed by the persons name.
Dor example you should be able to run the pro%ram as follos from the &,6 prompt E.1 a0i$ What ill be the effect of this command<.........................................................................
Exercise 15 )eplaceable parameters Create a batch file called Ex15#bat that accepts one parameterM a directory name. 8he pro%ram ill then create that directory on the C dri#e and chan%e into it.
Exercise 15a dd error checin%. Chec hether the user has supplied the parameter. 7t should print an error messa%e9 if it has not been supplied.
"
Fecho off 7f JI1J PP JJ A,8, /o!&irectory Echo. Echo &irectory 6pecified Echo. A,8, End /o!&irectory Echo. Echo /o &irectory 6pecified Echo. End *se this ith your code from Exercise 14. 7f parameter is specified9 it ill create a directory ith that name.
Exercise 16 )eplaceable parameters Create a batch file called Ex16#bat that accepts to parameters9 hich represents 2 directory names. 8he pro%ram ill create a directory represented by the first parameter on the C dri#e. 7t ill then create a subdirectory ithin this directory9 hose name is represented by the second parameter. Dor example you should be able to run the pro%ram as follos from the &,6 prompt EX15 Reports IBM
What ill be the effect of this command<.........................................................................
%$$in2 error checDin2 to Exercise 16#
What error checs ill you include< .........................................................................
Exercise 1= )eplaceable parameters Write a batch pro%ram called Ex1=#Bat that accepts to parameters – first is the directory name and the second is a file name o Creates a directory ith the same name as the first parameter at the root of the C dri#e. o Copies a file ith the same name as the second parameter to this folder. 8ote: ;ou must create the file first on the C &ri#e.
Dor example you should be able to run the pro%ram as follos from the &,6 prompt EX16 IBM report.txt
Durther modify this pro%ram to perform error checs.
$
<%/+ in M'-,' What error checs ill be included< ......................................................................... 6pecifies directories here executable pro%rams are located. 0ath specifies the search path. *se the 08+ command to tell -6!&,6 hich directories to loo into hen an executable file is not in the current directory. When you enter a line of text at the -6!&,6 prompt that is neither a reco%niQed command nor an executable filename in the current directory9 -6!&,6 ill search throu%h the directories in your 08+ for a filename that matches the text you entered. -any paca%es9 such as -icrosoft Word9 automatically add or modify the 08+ line durin% the installation process. Each path cannot exceed 12" characters9 and may be further limited by your -6!&,6 en#ironment space. 'ee more information at: http.la%monster.or%docs&,6"Q!path.html
8here are to ays to add a directory to the path in Windos " 1
%t the !omman$ line FM' ,'
t the -6!&,6 prompt type path n example of hat could be shon
BE !%)E"(L hen you attempt to chan%e the path that you do it correctly. +ow to a$$ a $irectory to the system path: PATH=%PATH%;C:\BLASTER
I08+I represents the existin% path. 8his example ould add C?B@68E) to the path.
2
/hrou2h the 9(I in Win$ows >
8he path is mana%ed in Windos " as En#ironment #ariables. 7n the 6ystem 0roperties9 d#anced tabM clic the En#ironment Kariables button. +i%hli%ht the
'
Exercise 1>
dd the directory to the path to allo your batch files to be run from any directory at the command prompt. Chan%e to a different directory to the one hich contains your batch files. /o try to run the batch file. Were you able to run the batch file from a different directory to the one the batch file is stored in< .........................................
)e0ision Guestions: /aDen from past exam papers R1.
Create a batch file pro%ram called G1#bat that a. Clears the screen b.
lists all files from !:H(sers directory and its subdirectories on the C dri#e hich match the folloin% criteria Diles hose filename starts ith Ss and the extension of the file starts ith the letter Sl and is 3 letters lon%. "or example: setupact.lo%9 setuperr.lo%
c.
Copy the file setupact#lo2 to c:Haccounts
d.
8he batchfile should also ha#e a comment line hich contains your name on the top of the pro%ram.
R2.
Write a batch file called G#bat that reads in a directory name and creates that directory on the !:H $ri0e# 7f no directory name is specified an error messa%e is displayed to the user indicatin% so.
R3.
Write a batch pro%ram called G#bat that accepts one parameter hich contains the name of a directory. a.
then create a directory in the letters folder hich has the same name as the parameter supplied.
b.
Write the &,6 command to copy the file setupact#lo2 from the c?accounts directory to this ne directory and %i#e the file a ne name called setupact#tmp
R4. a. What is the purpose of the
1(