Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch parse each parameter

I am trying to create a batch script which would perform the same action for each parameter given. For example, giving X files as parameters:
script.bat "file1.txt" "file2.txt" "file3.txt" "file4.txt" ... "fileX.txt"
will rename them to:
"file1.bin" "file2.bin" "file3.bin" "file4.bin" ... "fileX.bin"
Rename is just an example, I will need it for more complex operations too.
I guess it should be something like for each but I'm new in batch scripts.

I just wonder if I could just increment %1 index...

like image 881
Ion Bazan Avatar asked Feb 26 '12 21:02

Ion Bazan


1 Answers

You can use SHIFT to shift the parameters left. In other words calling shift will put the second parameter to %1, the third to %2 etc.

So you need something like:

@ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
   ECHO %1
SHIFT
GOTO Loop
:Continue

This will just print the arguments in order, but you can do whatever you want inside the loop.

like image 154
Petar Ivanov Avatar answered Oct 01 '22 10:10

Petar Ivanov