Windows batch files are great if you want to perform several tasks in quick succession, or if you want to do things to several files in a row, or if you simply don't want to use the GUI interface to perform some tasks (e.g., performing unattended tasks). This tip builds a bit on the tip titled Creating a Simple Batch File, so you might want to check that out before proceeding.
If you're going to be using batch files much, it's a good idea to create a folder to hold them all. I have such a folder on my system called "C:\Bat". I also have a "C:\Temp" folder, which serves as a temporary location for various files I deal with on an ongoing basis. This tip makes use of both of these folders, so if you don't have them already, I suggest that you create them.
The main purpose of the batch file we're going to create is to introduce some command-line features more than to do anything very useful, so let's give our batch file the name "C:\Bat\Fun.bat". Create the empty file, and launch Notepad to edit it. You should also display the command prompt window so that you can run the batch file from the command line instead of Windows Explorer. (Displaying the command prompt window is covered in another tip.)
As you develop batch files you may wish to add comments to them to remind yourself of what they do and how they do it. To add a comment, use the REM statement (short for "REMark") or put a double colon ("::") at the beginning of a line. Comment lines aren't processed when the batch file is invoked; they're simply there as documentation for whoever is looking at the file.
Another simple command is the ECHO command, which does get processed and displays on the screen whatever text follows ECHO. We can use these two commands in our batch file as follows:
REM This file demonstrates the use of a few command-line statements ECHO Hello. This is my Fun.bat file.
Run this file from the Command Prompt window simply by typing its name:
C:\> \Bat\Fun.bat
You'll notice when you run this file that the lines are displayed on the screen. This is because the default behavior for batch files is to display the file's contents as it runs. To prevent this from happening you can precede each statement with the "@" symbol like so:
@REM This file demonstrates the use of a few command-line statements @ECHO Hello. This is my Fun.bat file.
Now when you run it, you'll only see what the ECHO statement says to display. Instead of preceding each statement with the "@" symbol, you can turn off displaying the file's contents by placing an "@ECHO OFF" statement at the beginning of your file. So now the file looks like this:
@ECHO OFF REM This file demonstrates the use of a few command-line statements ECHO Hello. This is my Fun.bat file.
You can pass parameters to a batch file when you invoke it so that it can work on variable information at run time. You pass parameters simply by specifying them on the command line after the name of the batch file you want to run. You then use the passed parameters in your file via the special symbols "%1", "%2", etc., up to "%9" (you can pass up to nine parameters to a batch file). Let's say, for example, that you want your file to display a directory listing of whatever folder you specify at run time. Edit your batch file to include the DIR command, like so:
@ECHO OFF REM This file demonstrates the use of a few command-line statements ECHO Hello. This is my Fun.bat file. DIR %1
Then pass the parameter on the command when it's invoked:
C:> \Bat\Fun.bat C:\Temp
This produces a directory listing of your C:\Temp folder.
This tip has introduced a few new concepts about batch files. If you want to go a bit deeper, just search on the WindowsTips site for additional clues on how you can use batch files.
This tip (9984) applies to Windows 7, 8, and 10.
Sometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about ...
Discover MoreThis tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...
Discover MoreOne of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...
Discover More2021-10-19 04:17:34
sandeep
Great post on batch files. Keep going.
Copyright © 2024 Sharon Parq Associates, Inc.
Comments