Lab 4 - Batch files Exercise 1 – Create Crea te your first batch file..................................................... file.....................................................................................1 ................................1 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 Exercise 6 - Wildcards Example..................................................... Example................................................................................................ ........................................... 4 Exercise – Copy from o!e dri"e to a!other............................................................................4 Exercise # – Creati!$ a %irectory structure...............................................................................6 Exercise & – Copyi!$ a!d mo"i!$ files..................... fi les............................................ ............................................................. ...................................... ...6 Exercise 1' – (emo"i!$ directories..........................................................................................6 Exercise 11 – )si!$ the EC*+ C+,,%.................................. C+,,%......................................................... .......................................... ................... Exercise 12 – )si!$ the call comma!d.................................... comma!d........................................................... .................................................. ........................... Exercise 13 – (eplaceable parameters................................................................. parameters....................................................................................... ...................... Exercise 14 – (eplaceable parameters................................................................. parameters.......................................................................................# ......................# Exercise 15 – (eplaceable parameters................................................................. parameters.......................................................................................# ......................# Exercise 16 – (eplaceable parameters................................................................. parameters.......................................................................................& ......................& Exercise 1 - /aths........................................ /aths............................................................... .............................................. .................................................... ............................. 1'
(ef0 .chebucto.!s.caa621%+atasic.html atch files are files that co!tai! a list of comma!ds. 7t executes the comma!ds ithi! the file he! you execute the batch file. 8he comma!ds execute o!e by o!e. comma!d ithi! a batch file ca! be used to startru! a!other applicatio!9 for example you could start , Word. Word. Why batch files are useful?
batch file could be used to ru! fre:ue!tly ru! comma!ds9 deleti!$ a series of files9 mo"i!$ files9 copyi!$ files etc. simple batch file does !ot re:uire a!y special pro$rammi!$ sills a!d ca! be do!e by users ho o!ly !o %+ comma!ds. ! example of a ell !o! batch file is the autoexec.bat9 hich is a simple batch file loaded each time the computer is loaded o! ,-%+ a!d early Wi!dos computers. 8his batch file co!tai!ed all the !ecessary comma!ds a!d pro$rams used to ru! ,-%+ a!d Wi!dos Wi!dos each time ti me the computer booted. )se your VMware accounts to do this lab sheet because you ill !eed to sa"e your or.
1
Exercise 1 – Create your first batch file 1. +pe! a! ,-%+ comma!d i!do. 2. Cha!$e to the root directory of the C dri"e a!d create a directory called BatchFiles .
;ou ca! use this directory to sa"e your batch files. 3. t the ,-%+ prompt9 ope! the editor by eyi!$ i! E%78 a!d pressi!$ Enter. 4. 8ype the folloi!$ li!es i!to the editor. Each of the li!es belo is a comma!d. cls ver pause dir c:\windows 5. a"e this file i! the Batchfiles folder a!d call it Ex1bat 6. efore co!ti!ui!$9 ca! you remember hat the effect of each of the 4 comma!ds that
you ha"e typed i!to the file< 7f !ot – loo them up i! the help. 7. =ea"e the editor – Clic o! File a!d Exit. 8. ,ae sure you are i! the directory here the batch file is stored >hopefully i! batchfiles? 9. Cha!$e i!to the atch@iles folder a!d type i! the !ame of the batch file i.e. Ex1 a!d press Enter. 10. Aey i! %7( a!d M!"E #$%E you see a file called EB1.8 7f you do!t see the file do this0 11. Aey i! C& ' Aey i! &(% )# E*1B!+ >;ou should see exactly here your file is located?.
What happe!s< ......................................................................................................... ................................................................................................................................... /ut the comma!d ,EC. .FF as the first li!e i!to Ex1.bat. (u! the batch file a$ai!. What is effect of this stateme!t< ................................................................................................................................... ..................................................................................................................................
(/0ortant #ae your batch files to the BatchFiles fol2er
2
Exercise 3 – Create a 2irectory structure Write a batch pro$ram called E*3B!+ that creates the folloi!$ directory structure o! dri"e C.
C0
CC+)8
=etters
Email
C+==EDE
Fa"a
pps
Chec either i! Explorer or at the prompt that the directory structure sho! abo"e has bee! created.
Exercise – Wil2car2s exa/0le Write a batch pro$ram called E*B!+ that lists all files hich start ith the letter , a!d ha"e the exte!sio! of .EBE o! dri"e C. 7t should search from the root do! throu$h all the subdirectories.
Exercise 4 – Wil2car2s exa/0le Write a batch pro$ram called E*4B!+ that lists all files that match a!y of the folloi!$ criteria ithi! the root of the C dri"e a!d do! throu$h its subdirectories0 a? b?
@iles ith a! exte!sio! of C+, EBE files hose 2!d letter is 7 e.$.9 W I *E=/.EBE % I D.EBE etc.
,ae sure the output does !ot scroll up the scree! too :uicly. /ut a pause comma!d i! betee! parts a? a!d b?
Exercise 5 – Wil2car2s exa/0le Write a batch pro$ram called E*5B!+ that lists all files that match a!y of the folloi!$ criteria ithi! the Win2ows folder a!d do! throu$h its sub directories0 a? @iles that ha"e a! exte!sio! that starts ith the letter C. b? @iles that do!t ha"e a!y exte!sio!
3
Batch co//an2s Fust lie all ,-%+ comma!ds9 all batch file comma!ds are !ot case se!siti"e. *oe"er9 i! the belo listi!$ e ha"e listed all comma!ds i! all caps to help you ide!tify hat is a comma!d a!d hat is !ot. ,
%oes !ot echo bac the text after the G symbol. 8his is most commo!ly used as ,EC. .FF to pre"e!t a!y of the comma!ds i! the batch file from bei!$ displayed9 Hust the i!formatio! !eeded.
61
8he perce!t folloed by a !umeric "alue9 be$i!!i!$ ith o!e9 allos users to add "ariables ithi! a batch file. Example of hat ca! be used i! a batch file. echo ello 61
Whe! the abo"e o!e-li!e batch file is created9 add your !ame after the batch file. @or example9 typi!$ my!ame >bei!$ the !ame of the bat file? a!d the! your !ame0 /yna/e bob
ould output0
*ello bob
7ote8 8his ca! be exte!ded to I29 I39 a!d so o!. 8L!BEL y addi!$ a colo! i! fro!t of a ord9 such as L!BEL9 you create a cate$ory9 more commo!ly !o! as a label. 8his allos you to sip to certai! sectio!s of a batch file such as the e!d of the batch file. lso see 9.+.. C!LL
8his is used to ru! a!other batch file ithi! a batch file. Whe! the batch file that is called is completed9 the remai!der of the ori$i!al batch file is completed. ote if the batch file does !ot exist it ill $i"e a! error messa$e.
CL#
Clear cree!
EC.
Will echo a messa$e i! the batch file. EC. :ello Worl2; ill pri!t *ello World o! the scree! he! executed. ote if you ha"e !ot typed ,EC. .FF at the be$i!!i!$ of the file this ill also pri!t JEC*+ *ello WorldJ a!d J*ello WorldJ. 7f you ould Hust lie to create a bla! li!e9 type EC. >addi!$ the full stop creates a! empty li!e.?
E*(+
Exits out of the %+ i!do if the batch file is ru!!i!$ from Wi!dos.
9.+. )sed to $o to a certai! label9 such as =E=. ! example of D+8+ ould be to D+8+ L!BEL E%. @or a! example of this see ru!!i!$ differe!t pro$rams. . (F
)sed to chec for a certai! co!ditio! if the co!ditio! exists. 7f that co!ditio! exists it ill perform that fu!ctio!.
/rompt the user to press a!y ey to co!ti!ue.
%EM
llos you to place comme!ts i!to the batch file ithout displayi!$ or executi!$ that li!e he! the batch file is ru!.
#(F+
Cha!$es the positio! of replaceable parameters i! a batch pro$ram. .
4
Exercise = - Wil2car2s Exa/0le Write a batch pro$ram called E*=B!+ ;our batch file should do the folloi!$0 Clear the scree! • dd a li!e to cha!$e directory to the C0Wi!dos directory • dd a li!e to list all files ith a! exte!sio! of 8B8 i! that directory. >,ae sure you put • some text files i! here if there are !o!e already?.
Exercise > – Co0y fro/ one 2rie to another carry this exercise out o! your =+C= machi!e Write a batch pro$ram called E*>B!+. 7t ill carry out the folloi!$ tass0 Copy all files ith a! exte!sio! of FK from %ri"e C to %ri"e D. >)se either the co0y comma!d or the xco0y comma!d. 8he xcopy comma!d is the better o!e to use here.? • Before the files are copied across use the echo comma!d to tell the user you are doi!$ so. (u! the batch pro$ram. •
Mo2ifications
dd the 0ause comma!d to your batch file. 8his li!e should be i!serted before the copy is carried out. What effect does this ha"e o! the pro$ram< •
•
dd comme!ts to the batch pro$ram that i!dicate the date ritte!9 author9 pro$ram purpose etc.
dd co//ents to all your batch files. 8he comme!ts should i!dicate the folloi!$0 - /ro$ram purpose - uthor - %ate ritte!
5
Exercise – Creatin@ a &irectory structure Write a batch pro$ram called E*B!+ that creates the folloi!$ directory structure o! dri"e C. ;ou ha"e already created all folders except the folder called +p ys. ;ou should not create the files. C0 C+==EDE
CC+)8 #a/0letxt
Exa/0letxt
=etters
Email
+p ys
Fa"a
pps
re0ort2oc
Chec either i! Explorer or at the prompt that the directory structure sho! abo"e has bee! created.
Exercise A – Co0yin@ an2 /oin@ files Create the three files sho! i! the dia$ram abo"e. 8ae it that you are curre!tly i! the %+ prompt at the root directory of %ri"e C he! you ru! the batch file.
Write a batch pro$ram called E*AB!+ that ill a.
Cha!$e directory to the !ccounts folder.
b.
8here are to files Example.txt a!d ample.txt i! the directory !ccounts. Write the %+ comma!d to copy the to files to the directory called aa
c.
%elete the file Exa/0letxt from the !ccounts folder. /rompt the user to co!firm before deleti!$ the file.
d.
Write the %+ comma!d to mo"e the file %e0ort2oc to the folder !00s a!d call it a differe!t !ame %e0ort.l22oc
Exercise 1 – %e/oin@ 2irectories Write a batch pro$ram called E*1B!+ that re/oes the !ccounts directory a!d all its subdirectories i! the abo"e structure. Chec that it successfully remo"es all the directories sho! abo"e.
6
Exercise 11 – $sin@ the EC. C.MM!7& Write a batch pro$ram called E*11B!+ that produces the folloi!$ output to the scree!. o s!oflae feels $uilty i! a! a"ala!che. ot e"erythi!$ that is importa!t ca! be measured. ot e"erythi!$ that ca! be measured is importa!t.
E!sure the scree! is cleared a!d that there is a bla! li!e u!der each li!e of output. EC. .FF >tur!s echoi!$ of comma!ds off?. EC. >pri!ts a bla! li!e to the scree!?. CL# >clears the scree!?.
Exercise 13 – $sin@ the call co//an2 Write a batch pro$ram called E*13aB!+ that co!tai!s the folloi!$ stateme!ts0 c2 cls 2ir a"e this pro$ram. What ill be the effect of these comma!ds<......................................................................... Create a!other pro$ram called Ex13bat hich ill a? Cha!$e to the apps folder b? Calls pro$ram Ex12a.bat What ill be the effect of this pro$ram<......................................................................... What directory are you i! !o < ................... *o did you $et there<....................... .....................................................................................................................................
$sin@ re0laceable 0ara/eters Exercise 1 – %e0laceable 0ara/eters /ro$rams ca! accept re0laceable 0ara/eters at the prompt. >8he replaceable parameters are I1 throu$h to I&?. Write a batch file called Ex1Bat hich accepts o!e parameterL the parameter is a perso!s !ame. 8he pro$ram ill the! display the ord M*elloN folloed by the perso!s !ame.
@or example you should be able to ru! the pro$ram as follos from the %+ prompt0 E*1 &ai2 What ill be the effect of this comma!d<.........................................................................
Exercise 14 – %e0laceable 0ara/eters Create a batch file called Ex14bat that accepts o!e parameterL a directory !ame. 8he pro$ram ill the! create that directory o! the C0 dri"e a!d cha!$e i!to it.
Exercise 14a !22in@ error checDin@ Will checD that the user has su00lie2 the 0ara/eter Will 0rint an error /essa@e if it has not been su00lie2
Gecho off 7f JI1J OO JJ D+8+ o-%irectory Echo. Echo %irectory pecified Echo. D+8+ E!d 0o-%irectory Echo. Echo o %irectory pecified Echo. 0E!d )se this ith your code from Exercise 14. 7f parameter is specified9 it ill create a directory ith that !ame.
Exercise 15 – %e0laceable 0ara/eters Create a batch file called Ex15bat that accepts to parameters9 hich represe!ts 2 directory !ames. 8he pro$ram ill create a directory represe!ted by the first parameter o! the C0 dri"e. 7t ill the! create a subdirectory ithi! this directory9 hose !ame is represe!ted by the seco!d parameter. @or example you should be able to ru! the pro$ram as follos from the %+ prompt0 EX15 Reports IBM
What ill be the effect of this comma!d<.........................................................................
!22in@ error checDin@ to Exercise 15
What error checs ill you i!clude< .........................................................................
#
Exercise 1= – %e0laceable 0ara/eters Write a batch pro$ram called Ex1=Bat that accepts to parameters – first is the directory !ame a!d the seco!d is a file !ame0 o Creates a directory ith the same !ame as the first parameter at the root of the C dri"e. o Copies a file ith the same !ame as the seco!d parameter to this folder. 7ote8 ;ou must create the file first o! the C0 %ri"e.
@or example you should be able to ru! the pro$ram as follos from the %+ prompt0 EX16 IBM report.txt
@urther modify this pro$ram to perform error checs. What error checs ill be i!cluded< .........................................................................
!t the Co//an2 line GM# &.#
t the ,-%+ prompt type0 0ath ! example of hat could be sho!0 ,icrosoft Wi!dos B/ QKersio! 5.1.26''R >C? Copyri$ht 1-2''1 ,icrosoft Corp. D0Spath /8*OC0W7%+Wsystem32LC0W7%+WLC0W7%+Wystem32WbemLc0/ro$ra m @iles,i crosoft T= er"er&'8oolsbi!! D0S ow to a22 a 2irectory to the syste/ 0ath8 PAT!"PAT"#$:\%&A'T() I/8*I represe!ts the existi!$ path. 8his example ould add C0=8E( to the path.
&
3
+hrou@h the 9$( in Win2ows *<
8he path is ma!a$ed i! Wi!dos B/ as E!"iro!me!t "ariables. @rom the destop9 ,y ComputerL properties. 7! the ystem /roperties9 d"a!ced tabL clic the E!"iro!me!t Kariables butto!. *i$hli$ht the path "ariable i! the ystems Kariable sectio!>loer sectio!? a!d clic edit. dd or modify the path li!es ith the paths you ish the computer to access. Each differe!t directory is separated ith a semicolo! as sho! belo. C0/ro$ram @ilesLC0Wi!dosLC0Wi!dosystem32
What is the path that you see< ....................................................................................................
Exercise 1> -
•
• •
dd to the path to allo your batch files to be ru! from a!y directory at the comma!d prompt. Cha!$e to a differe!t directory to the o!e hich co!tai!s your batch files. o try to ru! the batch file. Were you able to ru! the batch file from a differe!t directory to the o!e the batch file is stored i!<
------------------------------------------------------------------------------
1'