Saturday, 5 January 2019

How do I detect the number of parameters to a batch file and loop through them?


Is there an easy way to detect the number of parameters passed as arguments to a batch file, and then use for /L to loop through them?



Answer



How do I detect the number of parameters to a batch file and loop through them?



%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)



You can use %* to retrieve all of the command line arguments to a batch file.


To count the arguments and loop with for /L see the StackOverflow answer Batch-Script - Iterate through arguments by aacini:



@echo off
setlocal enabledelayedexpansion

set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)

echo Number of processed arguments: %argCount%

for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"



Further Reading



No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...