1.

Solve : Run multiple command in a batch file?

Answer»

Hi all,
i need your support cause i'm a newbie about Ms Dos batch.
My problem is that i want to run into a batch, two command.
The first one has to compress a FILE and the other has to change the name of the resulting file adding a suffix.
The problem i have encountered is that only the first one works, the second one doesn't start.
Here you are the batch i have written:

Code: [Select]RAR a XXX_ -m1 -df -ep -agYYYYMMDD C:\temp\*.txt
for /F "tokens=1-2 delims=." %i in ('dir *.rar /b') do ren "%i.%j" "%i0330.%j"
If i run the second command in the MsDos prompt it works.
Thanks in advanceOne percent sign at the prompt, two in a batch file, so change all %i to %%i and all %j to %%j
WORKS...
Thanks a lot.
Can you explain me why i have to double all %? Quote from: canislupus on FEBRUARY 03, 2011, 08:33:01 AM

WORKS...
Thanks a lot.
Can you explain me why i have to double all %?

It's to do with the way the command interpreter expands variables. Using a percent sign (%) in a batch file requires that two percent signs (%%) be specified.

For example, the command to display "5%" from a batch file would be :

ECHO 5%%

A single percent sign on a line is treated as a "nul" character in a batch file. For example:

ECHO % is processed as ECHO
ECHO a%b is processed as ECHO ab

If a command contains two percent signs, the command interpreter will treat any characters between them as an environment variable to be expanded. For example, if the SET command shows that the current environment variables are

COMSPEC=C:\COMMAND.COM
PATH=C:\DOS
PROMPT=$P$G
B=C


then

ECHO %PATH% is processed as ECHO C:\DOS
ECHO a%b% is processed as ECHO aC
ECHO a%b b%a is processed as ECHO aa


If there are no characters between the two percent signs, one percent sign is stripped off and the other will remain. This is why a FOR command that echos the name of each file with a .COM extension would be

FOR %V IN (*.COM) DO ECHO %V


but if the same command is placed in a batch file, the following is required:

FOR %%V IN (*.COM) DO ECHO %%V


Perfect i have understood all.
Now i have an other problem related to the same batch.
I need to launch the for command on a file that isn't in the same directory where the batch run.
I have TRIED to modify the batch from:

Code: [Select]for /f "tokens=1-2 delims=." %%i in ('dir *.rar /b') do ren "%%i.%%j" "%%i0330.%j"
to:

Code: [Select]for /f "tokens=1-2 delims=." %%i in ('dir C:\temp\*.rar /b') do ren "%%i.%%j" "%%i0330.%%j"

But i receive the error "The System cannot find the file specified".
This time where is the problem ?

P.S. Sorry if i asked stupid thing but i am a newbie...


Code: [Select]for /f "delims=" %%i in ('dir /b C:\temp\*.rar') do ren "%%~dpnxi" "%%~dpni0330%%~xi"I apologise, the above won't work, but this should...

Code: [Select]for %%i in ("C:\temp\*.rar") do ren "%%~dpnxi" "%%~ni0330%%~xi"
explanation

FOR %%variable IN (set) DO command.

Using FOR without the /F switch and therefore no tokens/delims block, (set) can be a set of one or more files, wildcards can be used.

There are FOR variable modifiers... in this case we use ~d, (drive) ~p, (path) ~n (name) ~x (extension) so if (set) is a file or files, then:

%%~di is the drive letter (with a colon)
%%~pi is the path
%%~ni is the file name
%%~xi is the file extension (including the dot)

They can be combined so that %%~dpnxi is the full drive letter, path, name and extension of a file. You need to supply this to REN if the file in QUESTION is not in the current directory.

Type FOR /? at the prompt for the command help.






Discussion

No Comment Found