Please explain

  • Thread starter Thread starter Praetorian Guard
  • Start date Start date
P

Praetorian Guard

This works if I call this from another batch file but does not work when run
manually in the command prompt. Removing the one % on %%a will run manually
in the command prompt. Can someone explain why?

FOR /F "TOKENS=1 DELIMS=." %%a IN ('DIR /B /S "D:\Backup\DOC\*.DOC"') DO
(wzzip %%~fpna_Weekly_%vday%_%vdate%.zip %%a.DOC) || GOTO :EOF

What does the two %% stands for?

Thank you in advance.
 
Praetorian Guard said:
This works if I call this from another batch file but does not work when
run manually in the command prompt. Removing the one % on %%a will run
manually in the command prompt. Can someone explain why?

FOR /F "TOKENS=1 DELIMS=." %%a IN ('DIR /B /S "D:\Backup\DOC\*.DOC"') DO
(wzzip %%~fpna_Weekly_%vday%_%vdate%.zip %%a.DOC) || GOTO :EOF

What does the two %% stands for?

Thank you in advance.

This is a standard rule: Loop variables are preceded by %% when used in a
batch file and by % when used at the Command Prompt. Exampls:

for /L %a in (1,1,10) do echo %a [Command Prompt]
for /L %%a in (1,1,10) do echo %%a [Batch file]
 
This is a standard rule: Loop variables are preceded by %% when used in a
batch file and by % when used at the Command Prompt. Exampls:
for /L %a in (1,1,10) do echo %a [Command Prompt]
for /L %%a in (1,1,10) do echo %%a [Batch file]

Right. Another way of putting is that in the script the first % acts as
an escape character. More on that in
http://www.netikka.net/tsneti/info/tscmd047.htm

All the best, Timo
 
This works if I call this from another batch file but does not work when run
manually in the command prompt. Removing the one % on %%a will run manually
in the command prompt. Can someone explain why?

FOR /F "TOKENS=1 DELIMS=." %%a IN ('DIR /B /S "D:\Backup\DOC\*.DOC"') DO
(wzzip %%~fpna_Weekly_%vday%_%vdate%.zip %%a.DOC) || GOTO :EOF

What does the two %% stands for?

In a batch file two %% in a row equals one % to the command processor.

For exmaple if you run this you'll get one % on the screen:

@echo off
echo %%


It's easiest to remember that in a batch file the for-in-do command is a
special case where two %% are used to create the metavariables %%a %%b %%c
etc and only one % is used on the command line.
 
Timo Salmi said:
This is a standard rule: Loop variables are preceded by %% when used in a
batch file and by % when used at the Command Prompt. Exampls: for /L %a
in (1,1,10) do echo %a [Command Prompt]
for /L %%a in (1,1,10) do echo %%a [Batch file]

Right. Another way of putting is that in the script the first % acts as
an escape character.

I'll grant that this is something like an escape character. But the use of
an escape character is generally to prevent a special character from having
its usual effect. But the usual effect of the single "%" in a FOR loop typed
interactively is to expand the variablemm which is also what we want in a
batch file.

Percent signs are doubled in other contexts, i.e.:

set var=zzz
echo/%%var%% = %var%

But the result works as designed whether the commands are typed or run from
a batch file. If the percentage signs are doubled in either situation, the
result is not as intended.

/Al
 
Back
Top