hello there
Absolute beginner
Can anyone help me with this, the first part is no problem, its the second part "a more general version with arguments" the one in bold that i carn't seem to get the grip of, i have been trying this out quite a few times without success
I must be getting something wrong.................windows xp
It is sell explanatry i know, but if anyone can clarify it a bit more it would help me move on
Constructing a batch file
In the following discussion it is assumed that the Introductory page and the page on Commands have been read.
The first line in a batch file often consists of this command
@echo off
By default, a batch file will display its commands as it runs. The purpose of this first command is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well. This nuance isn't really all that important in the context here but I mention it because it is often seen in scripts. The scripts we will discuss are very brief and omitting this line won't make any great difference. However, as a matter of good practice, we will enter it in our scripts.
Our first batch file example is going to list all the files in a folder and put the list in a new text file . We will use the directory command "dir" that is discussed on another page. Open Notepad and enter the line "@echo off" (without quotes). Next enter another line
dir "C:\Program Files" > C:\list_of_program_files.txt
(I'm assuming that your Program Files folder is on the C: drive). This will give us the two-line file
@echo off
dir "C:\Program Files" > C:\list_of_program_files.txt
Now save this two-line file as "listprograms.bat" (without quotes) to some convenient location. Be sure that Notepad is saving as "All files" and not as a text file. See the figure below.
Three important points are illustrated in the example script. Note that complete paths are used for files including the drive letter. Also note the quotes around "C:\Program Files". Paths must be quoted whenever a file or folder name has a space in it. Finally note the redirection symbol ">" that is used to send the output to a file instead of the screen.
All that has to be done to use the file is to double-click it. A file C:\list_of_program_files.txt will then be created.
A more general version with arguments
The file that we have been discussing is limited to listing one particular folder and putting the list in one particular file. However, it is easy to make the file able to list whatever folder we want and to put the list wherever we want. Batch files can use arguments or data that is input from the user. The process makes use of placeholders of the form %1, %2, These are replaced in the script by our input data. This type of situation cannot be clicked directly but should be run in a command prompt. The new batch file would be
@echo off
dir %1 > %2
Enter in Notepad and save as "makelist.bat". To run the file , open a command prompt and enter
{path}makelist somefolder somewhere\list.txt
where somefolder is whatever folder (with complete path) that you want to list in somewhere\list.txt. Now you have a little program that will list the contents of a folder whenever you want.
using command line arguments %1 %2
Moderator: DosItHelp
-
- Posts: 36
- Joined: 17 Jul 2008 07:37
Not sure what you need here.
The last bit uses %1 and %2 to refer to the first and second parameters you use after makedir.cmd
> redirects the output of the dir command to a file, rather than just showing it on screen
In this case, dir %1 > %2 :
%1 is the folder that dir lists
%2 is the file you want the output to go to
So you use it as described
e.g. makedir c:\temp d:\templist.txt
Without a %2 (a second parameter), makedir.cmd does nothing really.
HTH,
GF
The last bit uses %1 and %2 to refer to the first and second parameters you use after makedir.cmd
> redirects the output of the dir command to a file, rather than just showing it on screen
In this case, dir %1 > %2 :
%1 is the folder that dir lists
%2 is the file you want the output to go to
So you use it as described
e.g. makedir c:\temp d:\templist.txt
Without a %2 (a second parameter), makedir.cmd does nothing really.
HTH,
GF