Batch analysis of each parameter

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

I'm just wondering if I can just increment the index %1 ...

+6
source share
3 answers

You can use SHIFT to shift the remaining parameters. In other words, calling shift will add 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 

It just prints the arguments in order, but you can do whatever you want inside the loop.

+8
source

You can do something similar and just add the complexity you want:

 for %%x in (%*) do ( echo %%x ) 
+2
source

What I finished was as follows. As a rule, I do something, I thought that I would share ...

At the top of my batch file is the following code ...

Application:

 ::-------------------------------------------------------- :: Handle parameters ::-------------------------------------------------------- CALL:ChkSwitch bOverwrite "/OVERWRITE" %* CALL:ChkSwitch bMerge "/MERGED" %* 

Then below (where I usually place all my functions) ...

Function:

 ::-------------------------------------------------------- :: ChkSwitch Function ::-------------------------------------------------------- :ChkSwitch <bRet> <sSwitch> <sParams> ( SETLOCAL EnableDelayedExpansion SET "switched=0" :ChkSwitchLoop IF "%~3"=="" GOTO ChkSwitchDone IF %~3==%~2 ( SET "switched=1" GOTO ChkSwitchDone ) SHIFT /3 GOTO ChkSwitchLoop :ChkSwitchDone ) ( ENDLOCAL SET "%~1=%switched%" EXIT /B ) 

Using it is simple. You simply call the function passed in the variable you want to change, or create, than the next pass you are looking for, and the last pass all the parameters from the script.

+1
source

Source: https://habr.com/ru/post/909411/


All Articles