This document provides instructions for a series of exercises to learn batch file commands. The exercises include creating basic batch files, using wildcards to match files, copying and moving files between directories, removing directories, and using replaceable parameters. The exercises build on each other to demonstrate increasingly complex uses of commands like cls, echo, pause, copy, xcopy, mkdir, rmdir, and call within batch files.
This document provides instructions for a series of exercises to learn batch file commands. The exercises include creating basic batch files, using wildcards to match files, copying and moving files between directories, removing directories, and using replaceable parameters. The exercises build on each other to demonstrate increasingly complex uses of commands like cls, echo, pause, copy, xcopy, mkdir, rmdir, and call within batch files.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10
Lab 4 - Batch files
Exercise 1 Create your first batch file.....................................................................................1
Exercise 2 Create a directory structure...................................................................................2 Exercise 3 Wildcards example................................................................................................3 Exercise 4 Wildcards example................................................................................................3 Exercise 5 Wildcards example................................................................................................3 Exercise 6 - Wildcards 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.....................................................................................6 Exercise 1' (emo"i!$ directories..........................................................................................6 Exercise 11 )si!$ the EC*+ C+,,-.%............................................................................ Exercise 12 )si!$ the call comma!d...................................................................................... Exercise 13 (eplaceable parameters....................................................................................... Exercise 14 (eplaceable parameters.......................................................................................# Exercise 15 (eplaceable parameters.......................................................................................# Exercise 16 (eplaceable parameters.......................................................................................& Exercise 1 - /aths...................................................................................................................1' (ef0 111.chebucto.!s.ca23a46212%+526at6asic.html 6atch files are files that co!tai! a list of comma!ds. 7t executes the comma!ds 1ithi! the file 1he! you execute the batch file. 8he comma!ds execute o!e by o!e. - comma!d 1ithi! a batch file ca! be used to start2ru! a!other applicatio!9 for example you could start ,5 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!$ s4ills a!d ca! be do!e by users 1ho o!ly 4!o1 %+5 comma!ds. -! example of a 1ell 4!o1! batch file is the autoexec.bat9 1hich is a simple batch file loaded each time the computer is loaded o! ,5-%+5 a!d early Wi!do1s computers. 8his batch file co!tai!ed all the !ecessary comma!ds a!d pro$rams used to ru! ,5-%+5 a!d Wi!do1s each time the computer booted. )se your VMware accounts to do this lab sheet because you 1ill !eed to sa"e your 1or4. 1 Exercise 1 Create your first batch file 1. +pe! a! ,5-%+5 comma!d 1i!do1. 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 ,5-%+5 prompt9 ope! the editor by 4eyi!$ i! E%78 a!d pressi!$ Enter. 4. 8ype the follo1i!$ li!es i!to the editor. Each of the li!es belo1 is a comma!d. cls ver pause dir c:\windows 5. 5a"e this file i! the Batchfiles folder a!d call it Ex1bat 6. 6efore co!ti!ui!$9 ca! you remember 1hat the effect of each of the 4 comma!ds that you ha"e typed i!to the file< 7f !ot loo4 them up i! the help. 7. =ea"e the editor Clic4 o! File a!d Exit. 8. ,a4e sure you are i! the directory 1here the batch file is stored >hopefully i! batchfiles? 9. Cha!$e i!to the 6atch@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.6-8 11. 7f you do!Ct see the file do this0 Aey i! C& ' Aey i! &(% )# E*1B!+ >;ou should see exactly 1here 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 #a1e 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 follo1i!$ directory structure o! dri"e C. Chec4 either i! Explorer or at the prompt that the directory structure sho1! abo"e has bee! created. Exercise 4 Wil2car2s exa/0le Write a batch pro$ram called E*4B!+ that lists all files 1hich start 1ith the letter , a!d ha"e the exte!sio! of .EBE o! dri"e C. 7t should search from the root do1! 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 follo1i!$ criteria 1ithi! the root of the C dri"e a!d do1! throu$h its subdirectories0 a? @iles 1ith a! exte!sio! of C+, b? EBE files 1hose 2!d letter is 7 e.$.9 WI.*E=/.EBE %I-D5.EBE etc. ,a4e sure the output does !ot scroll up the scree! too :uic4ly. /ut a pause comma!d i! bet1ee! 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 follo1i!$ criteria 1ithi! the Win2ows folder a!d do1! throu$h its sub directories0 a? @iles that ha"e a! exte!sio! that starts 1ith the letter C. b? @iles that do!Ct ha"e a!y exte!sio! 3 C0E -CC+).85 C+==EDE =etters Email Fa"a -pps Batch co//an2s Fust li4e all ,5-%+5 comma!ds9 all batch file comma!ds are !ot case se!siti"e. *o1e"er9 i! the belo1 listi!$ 1e ha"e listed all comma!ds i! all caps to help you ide!tify 1hat is a comma!d a!d 1hat is !ot. , %oes !ot echo bac4 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 follo1ed by a !umeric "alue9 be$i!!i!$ 1ith o!e9 allo1s users to add "ariables 1ithi! a batch file. Example of 1hat 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 1ould output0 *ello bob 7ote8 8his ca! be exte!ded to I29 I39 a!d so o!. 8L!BEL 6y addi!$ a colo! i! fro!t of a 1ord9 such as L!BEL9 you create a cate$ory9 more commo!ly 4!o1! as a label. 8his allo1s you to s4ip 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 1ithi! 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 1ill $i"e a! error messa$e. CL# Clear 5cree! EC-. Will echo a messa$e i! the batch file. EC-. :-ello Worl2; 1ill pri!t *ello World o! the scree! 1he! executed. .ote if you ha"e !ot typed ,EC-. .FF at the be$i!!i!$ of the file this 1ill also pri!t JEC*+ *ello WorldJ a!d J*ello WorldJ. 7f you 1ould Hust li4e to create a bla!4 li!e9 type EC-. >addi!$ the full stop creates a! empty li!e.? E*(+ Exits out of the %+5 1i!do1 if the batch file is ru!!i!$ from Wi!do1s. 9.+. L!BEL )sed to $o to a certai! label9 such as =-6E=. -! example of D+8+ 1ould be to D+8+ E.%. @or a! example of this see ru!!i!$ differe!t pro$rams. . (F )sed to chec4 for a certai! co!ditio! if the co!ditio! exists. 7f that co!ditio! exists it 1ill perform that fu!ctio!. <!$#E /rompt the user to press a!y 4ey to co!ti!ue. %EM -llo1s you to place comme!ts i!to the batch file 1ithout displayi!$ or executi!$ that li!e 1he! 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 follo1i!$0 Clear the scree! -dd a li!e to cha!$e directory to the C0EWi!do1s directory -dd a li!e to list all files 1ith a! exte!sio! of 8B8 i! that directory. >,a4e sure you put some text files i! here if there are !o!e already?. Exercise > Co0y fro/ one 2ri1e to another carry this exercise out o! your =+C-= machi!e Write a batch pro$ram called E*>B!+. 7t 1ill carry out the follo1i!$ tas4s0 Copy all files 1ith a! exte!sio! of F-K- 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 1ritte!9 author9 pro$ram purpose etc. -dd co//ents to all your batch files. 8he comme!ts should i!dicate the follo1i!$0 - /ro$ram purpose - -uthor - %ate 1ritte! 5 Exercise ? Creatin@ a &irectory structure Write a batch pro$ram called E*?B!+ that creates the follo1i!$ directory structure o! dri"e C. ;ou ha"e already created all folders except the folder called +p 5ys. ;ou should not create the files. Chec4 either i! Explorer or at the prompt that the directory structure sho1! abo"e has bee! created. Exercise A Co0yin@ an2 /o1in@ files Create the three files sho1! i! the dia$ram abo"e. 8a4e it that you are curre!tly i! the %+5 prompt at the root directory of %ri"e C 1he! you ru! the batch file. Write a batch pro$ram called E*AB!+ that 1ill a. Cha!$e directory to the !ccounts folder. b. 8here are t1o files Example.txt a!d 5ample.txt i! the directory !ccounts. Write the %+5 comma!d to copy the t1o files to the directory called Ba1a c. %elete the file Exa/0letxt from the !ccounts folder. /rompt the user to co!firm before deleti!$ the file. d. Write the %+5 comma!d to mo"e the file %e0ort2oc to the folder !00s a!d call it a differe!t !ame %e0ort.l22oc Exercise 1C %e/o1in@ 2irectories Write a batch pro$ram called E*1CB!+ that re/o1es the !ccounts directory a!d all its subdirectories i! the abo"e structure. Chec4 that it successfully remo"es all the directories sho1! abo"e. 6 #a/0letxt Exa/0letxt re0ort2oc C0E -CC+).85 C+==EDE =etters Email +p 5ys Fa"a -pps Exercise 11 $sin@ the EC-. C.MM!7& Write a batch pro$ram called E*11B!+ that produces the follo1i!$ output to the scree!. .o s!o1fla4e 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!4 li!e u!der each li!e of output. EC-. .FF >tur!s echoi!$ of comma!ds off?. EC-. >pri!ts a bla!4 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 follo1i!$ stateme!ts0 c2 cls 2ir 5a"e this pro$ram. What 1ill be the effect of these comma!ds<......................................................................... Create a!other pro$ram called Ex13bat 1hich 1ill a? Cha!$e to the apps folder b? Calls pro$ram Ex12a.bat What 1ill be the effect of this pro$ram<......................................................................... What directory are you i! !o1 < ................... *o1 did you $et there<....................... ..................................................................................................................................... Exercise 14 %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 Ex14Bat 1hich accepts o!e parameterL the parameter is a perso!s !ame. 8he pro$ram 1ill the! display the 1ord M*elloN follo1ed by the perso!Cs !ame. @or example you should be able to ru! the pro$ram as follo1s from the %+5 prompt0 E*14 &a1i2 What 1ill be the effect of this comma!d<.........................................................................
$sin@ re0laceable 0ara/eters
Exercise 14 %e0laceable 0ara/eters Create a batch file called Ex14bat that accepts o!e parameterL a directory !ame. 8he pro$ram 1ill 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@eE if it has not been su00lie2 Gecho off 7f JI1J OO JJ D+8+ .o-%irectory Echo. Echo %irectory 5pecified Echo. D+8+ E!d 0.o-%irectory Echo. Echo .o %irectory 5pecified Echo. 0E!d )se this 1ith your code from Exercise 14. 7f parameter is specified9 it 1ill create a directory 1ith that !ame. Exercise 15 %e0laceable 0ara/eters Create a batch file called Ex15bat that accepts t1o parameters9 1hich represe!ts 2 directory !ames. 8he pro$ram 1ill create a directory represe!ted by the first parameter o! the C0 dri"e. 7t 1ill the! create a subdirectory 1ithi! this directory9 1hose !ame is represe!ted by the seco!d parameter. @or example you should be able to ru! the pro$ram as follo1s from the %+5 prompt0 EX15 Reports IBM What 1ill be the effect of this comma!d<......................................................................... !22in@ error checDin@ to Exercise 15 What error chec4s 1ill you i!clude< ......................................................................... # Exercise 1= %e0laceable 0ara/eters Write a batch pro$ram called Ex1=Bat that accepts t1o parameters first is the directory !ame a!d the seco!d is a file !ame0 o Creates a directory 1ith the same !ame as the first parameter at the root of the C dri"e. o Copies a file 1ith 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 follo1s from the %+5 prompt0 EX16 IBM report.txt @urther modify this pro$ram to perform error chec4s. What error chec4s 1ill be i!cluded< ......................................................................... 5pecifies directories 1here executable pro$rams are located. /ath specifies the search path. )se the /-8* comma!d to tell ,5-%+5 1hich directories to loo4 i!to 1he! a! executable file is !ot i! the curre!t directory. Whe! you e!ter a li!e of text at the ,5-%+5 prompt that is !either a reco$!iPed comma!d !or a! executable file!ame i! the curre!t directory9 ,5-%+5 1ill search throu$h the directories i! your /-8* for a file!ame that matches the text you e!tered. ,a!y pac4a$es9 such as Word/erfect9 automatically add or modify the /-8* li!e duri!$ the i!stallatio! process. 8he path ca!!ot exceed 12 characters9 a!d may be further limited by your ,5-%+5 e!"iro!me!t space. 8here are t1o 1ays to add a directory to the path i! Wi!do1s B/ 1F !t the Co//an2 line GM# &.#F -t the ,5-%+5 prompt type0 0ath -! example of 1hat could be sho1!0 ,icrosoft Wi!do1s B/ QKersio! 5.1.26''R >C? Copyri$ht 1-2''1 ,icrosoft Corp. D0ESpath /-8*OC0EW7.%+W5Esystem32LC0EW7.%+W5LC0EW7.%+W5E5ystem32EWbemLc0E/ro$ra m @ilesE,i crosoft 5T= 5er"erE&'E8oolsEbi!!E D0ES -ow to a22 a 2irectory to the syste/ 0ath8 PAT!"PAT"#$:\%&A'T() I/-8*I represe!ts the existi!$ path. 8his example 1ould add C0E6=-58E( to the path. & <!+- in M#-&.#
3F +hrou@h the 9$( in Win2ows *< 8he path is ma!a$ed i! Wi!do1s B/ as E!"iro!me!t "ariables. @rom the des4top9 ,y ComputerL properties. 7! the 5ystem /roperties9 -d"a!ced tabL clic4 the E!"iro!me!t Kariables butto!. *i$hli$ht the path "ariable i! the 5ystems Kariable sectio!>lo1er sectio!? a!d clic4 edit. -dd or modify the path li!es 1ith the paths you 1ish the computer to access. Each differe!t directory is separated 1ith a semicolo! as sho1! belo1. C0E/ro$ram @ilesLC0EWi!do1sLC0EWi!do1sE5ystem32 What is the path that you see< .................................................................................................... Exercise 1> - <aths -dd to the path to allo1 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 1hich co!tai!s your batch files. .o1 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' W!%7(798 Be careful when chan@in@ the 0aths on the co/0uter syste/