Write a batch program called EX3.BAT that lists all files which start with the letter M and have the extension of .EXE on drive C. It should search from the root down through all the subdirectories
Exercise 3 – Wildcards example
Write a batch program called EX3.BAT that lists all files which start with the letter M and have the extension of .EXE on drive C. It should search from the root down through all the subdirectories.
If you want to terminate a batch file during execution, press Ctrl + C.
Exercise 4 – Wildcards example
Write a batch program called EX4.BAT that lists all files that match any of the following criteria within the root of the C drive and down through its subdirectories:
- a) Files with an extension of COM and have 4 letters in the filename. g., chcp.com mode.com etc.
- b) EXE files whose 2nd letter is I g., WINHELP.EXE DIAGS.EXE etc.
Make sure the output does not scroll up the screen too quickly. Put a pause command in between parts a) and b)
Exercise 5 – Wildcards example
Write a batch program called EX5.BAT that lists all files that match any of the following criteria within the Windows folder and down through its sub directories:
- a) Files that have an extension that starts with the letter C.
- b) List folders and files that don’t have any extension
Batch commands
Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in the below listing we have listed all commands in all caps to help you identify what is a command and what is not.
@ |
Does not echo back the text after the @ symbol. This is most commonly used as @ECHO OFF to prevent any of the commands in the batch file from being displayed, just the information needed. |
%1 |
The percent followed by a numeric value, beginning with one, allows users to add variables within a batch file. Example of what can be used in a batch file. echo Hello %1 When the above one-line batch file is created, add your name after the batch file. For example, typing myname (being the name of the bat file) and then your name: myname bob would output: Hello bob Note: This can be extended to %2, %3, and so on. |
:LABEL |
By adding a colon in front of a word, such as LABEL, you create a category, more commonly known as a label. This allows you to skip to certain sections of a batch file such as the end of the batch file. Also see GOTO. |
CALL |
This is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message. |
CLS |
Clear Screen |
ECHO |
Will echo a message in the batch file. ECHO “Hello World" will print Hello World on the screen when executed.
Note if you have not typed @ECHO OFF at the beginning of the file this will also print "ECHO Hello World" and "Hello World".
If you would just like to create a blank line, type ECHO. (adding the full stop creates an empty line.) |
EXIT |
Exits out of the DOS window if the batch file is running from Windows. |
GOTO LABEL |
Used to go to a certain label, such as LABEL. An example of GOTO would be to GOTO END. For an example of this see running different programs. . |
IF |
Used to check for a certain condition if the condition exists. If that condition exists it will perform that function. |
PAUSE |
Prompt the user to press any key to continue. |
REM |
Allows you to place comments into the batch file without displaying or executing that line when the batch file is run. |
SHIFT |
Changes the position of replaceable parameters in a batch program. . |
Exercise 6 - Wildcards Example
Write a batch program called EX6.BAT
Your batch file should do the following:
- Clear the screen
- Add a line to change directory to the C:\Windows directory
- Add a line to list all files with an extension of TXT in that directory. (Make sure you put some text files in here if there are none already).
Exercise 7 – Copy from one drive to another
You will be using the C drive and a different partition on the drive in this batch file. My example uses partition E. Create another partition on your Vmware computer if you have not already done so.
Write a batch program called EX7.BAT. It should carry out the following tasks:
- Copy all files with an extension of ps1 from Drive C to Drive E. (Use either the copy command or the xcopy The xcopy command is the better one to use here.) For example files like profile.ps1 or types .ps1
- Before the files are copied across use the echo command to tell the user you are doing so.
Run the batch program.
Modifications
Add the pause command to your batch file. This line should be inserted before the copy is carried out. What effect does this have on the program?
Step by step
Solved in 4 steps with 2 images