ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Coding Snacks

Script/batch file that copies two files from a drive(usb) to a location

<< < (5/6) > >>

wolf.b:
A note on using two percent signs inside batch files, when using the for command. There are other similar cases as well:

The command line interpretor reads the batch file line by line. When it comes across a line that reads for example:

--- ---set PATH=%PATH%;C:\TOOLS
then it will append the directory C:\TOOLS to the current path. This is achieved by doing a replacement before execution. When the line is read, the CLI will replace the expression %PATH% with the string that is stored inside the environment variable called PATH (usually a list of folders, separated by semicolons). Lets assume for the sake of easy examples that the PATH would be C:\WINDOWS;C:\WINDOWS\COMMAND at the beginning. Replacement leads to the following command being executed:

--- ---set PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\TOOLS
Relacement (also called expansion of variables) is done first, execution is done second.

Now the for command is special, as you don't want the variable %f to be expanded, you want to assign something to it. If you were to write on the command line (DOS prompt) this:

--- ---for %x in (1 2 3) do echo %x
you would get what you want, because it is not inside a batch file and is correct syntax.The same line inside a batch file would produce a syntax error, secondary to expansion of %x to an empty string, resulting in the execution of:

--- ---for in (1 2 3) do echo
and that is missing the necessary name for the variable.

The solution is the special expansion of double percent signs: "%%" expands to (the shorter) "%", and the line:

--- ---for %%x in (1 2 3) do echo %%x
will expand to the correct line:

--- ---for %x in (1 2 3) do echo %x

Note that the name of the variable (here: x) is just added on after the expansion ot "%%", just like all the other letters.

Short: at the prompt use one, in batch files use two percent signs. (Applies only to special commands like for)


Greetings
Wolf

wolf.b:
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:


--- ---...
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
...

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
...


Greetings
Wolf

wolf.b:
Only today I noticed that there is another question unanswered:

and the line: @echo off, that removes extra spaces right?
-reply#4
--- End quote ---

It toggles the "default batch echo" state OFF, which is otherwise ON. Now to understand what the default batch echo does, take the following code:


--- ---echo on
echo 1
echo.
echo 2

echo off
echo 3
echo
echo 4

What happens here is this:
echo on     : this line will switch the echo state ON (unnecessary, it is ON anyway), and put out a message about the new state: ECHO is on
echo 1      : this will put out the string "1"
echo.        : this will put out a empty line
echo 2      : this will put out the string "2"
               : this will put out a prompt (C:\TestCopy\>) or similar
echo off    : this will switch the default batch echo off, and put out a message about the new state: ECHO is off
echo 3      : this will put out the string "3"
echo         : this will put out a message about the state of the default batch echo: ECHO is off
echo 4      : this will put out the string "4"

The output of that batch is very messy. Quite often I want to have empty lines in my code (for structuring) and I don't want to see the prompt echoed to the screen. That is why I have put "echo off" in the first line of the batch file in reply #3. If you remove the "@" sign from that batch you will still see the message "ECHO is off", because when the batch starts, the echo is on, and the first command will create some output. It switches the echo off for all the following lines, not for itself.

The "@" sign in front of any line switches the echo off for that particular line only. So in a batch with only one line you could write:


--- ---@for %%f in (myicon.ico, myieshortcut.url) do copy "C:\TestCopy\%%f" "C:\Documents and Settings\All Users\Desktop\TestCopy\datafolder"

and get the same effect (avoid messy output).


Greetings
Wolf

TheFormatter:
foolios

Sorry, I'm a little late getting into this thread, but there is some help I can provide:

As for the question

How do I represent the current directory of the batch file I am using along with the files to be copied?
--- End quote ---

the lightly documented %CD% variable that has been in the cmd environment for the last few revs will do what you need.

If you start the batch or cmd file in the directory f:\data, then %CD% will equal f:\data, so instead of looking like this

copy f:\data\data.exe c:\windows\system32\data
copy f:\data\pic.jpg C:\Documents and Settings\Administrator\Desktop

--- End quote ---

your copy lines would look something like this:

copy %CD%\data.exe c:\windows\system32\data
copy %CD%\pic.jpg C:\Documents and Settings\Administrator\Desktop

So it won't matter what drive or directory the batch file is executed from.  Please keep in mind that the %CD% represents the current command line directory - you can call the batch/cmd file from anywhere using the full path to it, but %CD% will contain the current drive and directory.

The above phrase hurts my eyes, so an example might be more in order.

CopyMyFiles.cmd is stored in C:\Scripts
At the command prompt, you change directories to F:\Data
From there you type in the command
       C:\Scripts\CopyMyFiles.cmd
The %CD% variable will contain the value F:\Data.

I hope that hasn't muddied the waters.


Jim

If you think the problem is bad now, wait until I solve it!

TheFormatter:
wolf.b:

In regards to handling the command line arguments from the "regular" environment as opposed to the 4DOS environment, you can get the whole command line with this:

ECHO Command Line:  %0 %*

And the parameter list with this:

ECHO Command Line Parameters:  %*

Jim

Batches? We don't need no stinkin' batches!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version