16
Coding Snacks / Re: Script/batch file that copies two files from a drive(usb) to a location
« on: November 16, 2007, 06:56 PM »
A note on writing comments inside batch files: (sorry, I am getting carried away)
the keyword "rem" is not universally useful for commenting out lines inside batch files. It really is a "do-nothing" command and it will be executed. This can lead to strange behavior to say the least. Take for example the following line inside a batch file:
Now let us assume that I want that line to be ignored (commented out), but I don't want to delete it, to save me remembering, with the option to include the line later again ... If I added the command rem in front of the line like so:
I will get this behavior: the command "rem" with the parameters "echo Some text that will appear in the file test.txt" will be executed and do nothing, that is: it will produce no output. But that empty output will still be redirected to the file test.txt. It is the redirection (">") that will now create an empty file test.txt. The danger here is that redirection does not ask for confirmation when overwriting an existing file. I repeat: rem is a command, that will be executed and, when used for commenting out a line that contains redirection, the redirection will be carried out as well.
Solution:
I use the double colon ("::") to comment out lines that I want to temporary be ignored, or for writing comments inside batch files. The behaviour of that can be regarded as such: Any line beginning with a colon is treated as a label. If the second character (first character of the label name) is an illegal character for a label name, then the entire line will be ignored. The following line inside a batch file will not produce an empty file called test.txt, and it will not overwrite an existing file:
Greetings
Wolf
the keyword "rem" is not universally useful for commenting out lines inside batch files. It really is a "do-nothing" command and it will be executed. This can lead to strange behavior to say the least. Take for example the following line inside a batch file:
...
echo Some text that will appear in the file test.txt > test.txt
...
echo Some text that will appear in the file test.txt > test.txt
...
Now let us assume that I want that line to be ignored (commented out), but I don't want to delete it, to save me remembering, with the option to include the line later again ... If I added the command rem in front of the line like so:
...
rem echo Some text that will appear in the file test.txt > test.txt
...
rem echo Some text that will appear in the file test.txt > test.txt
...
I will get this behavior: the command "rem" with the parameters "echo Some text that will appear in the file test.txt" will be executed and do nothing, that is: it will produce no output. But that empty output will still be redirected to the file test.txt. It is the redirection (">") that will now create an empty file test.txt. The danger here is that redirection does not ask for confirmation when overwriting an existing file. I repeat: rem is a command, that will be executed and, when used for commenting out a line that contains redirection, the redirection will be carried out as well.
Solution:
I use the double colon ("::") to comment out lines that I want to temporary be ignored, or for writing comments inside batch files. The behaviour of that can be regarded as such: Any line beginning with a colon is treated as a label. If the second character (first character of the label name) is an illegal character for a label name, then the entire line will be ignored. The following line inside a batch file will not produce an empty file called test.txt, and it will not overwrite an existing file:
...
:: echo Some text that will appear in the file test.txt > test.txt
...
:: echo Some text that will appear in the file test.txt > test.txt
...
Greetings
Wolf