1

Alright, so, I've searched quite a bit on how to do this but I've given up as I simply couldn't find anything.

So, I have a code (below); it's main purpose is to get 9 arguments and assign them as a variable-- ignore the echo's and pipping.

My question is: How can I simplfy this with a for loop or such so it doesn't take as much code, and if possible, have more than 9 arguments aswell

set pkg1=%1
set pkg2=%2
set pkg3=%3
set pkg4=%4
set pkg5=%5
set pkg6=%6
set pkg7=%7
set pkg8=%8
set pkg9=%9

IF DEFINED pkg1 (echo %1.ini 1> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg2 (echo %2.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg3 (echo %3.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg4 (echo %4.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg5 (echo %5.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg6 (echo %6.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg7 (echo %7.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg8 (echo %8.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
IF DEFINED pkg9 (echo %9.ini 1>> %WINGET_TEMP%\args.rdc 2>nul)
2

3 Answers 3

6

Without any ugly goto statements:

@echo off
setlocal enabledelayedexpansion
set cnt=0
for %%a in (%*) do (
  set /a cnt+=1
  set pkg!cnt!=%%a
)
for /l %%a in (1,1,!cnt!) do (
  echo pkg%%a=!pkg%%a!
)
endlocal

Output

C:\>loopbat a b c d e f g h i j k l "m n" o p q r s t u v w x y z
pkg1=a
pkg2=b
pkg3=c
pkg4=d
pkg5=e
pkg6=f
pkg7=g
pkg8=h
pkg9=i
pkg10=j
pkg11=k
pkg12=l
pkg13="m n"
pkg14=o
pkg15=p
pkg16=q
pkg17=r
pkg18=s
pkg19=t
pkg20=u
pkg21=v
pkg22=w
pkg23=x
pkg24=y
pkg25=z

If you want the quotes removed from arguments that contain spaces use set pkg!cnt!=%%~a instead of set pkg!cnt!=%%a (there's an extra tilde in the first one in case you didn't spot the difference).

2
  • 1
    This works as long as none of the arguments contain any of *, ?, or !. But there will be problems if any of those characters exist in the arguments. A robust solution that supports those chars requires GOTO.
    – dbenham
    Jun 27, 2013 at 14:03
  • 1
    Let me restate that. It may be possible to construct a robust script without GOTO that executes itself without CALL. But GOTO with SHIFT is the best option for a reasonable robust solution.
    – dbenham
    Jun 27, 2013 at 14:13
2

This should help you achieve what you want:

@echo off
set i=1

:again
rem use %~1 to remove any potential surrounding quotes (")
if "%~1"=="" goto end
set pkg%i%=%~1

set /a i+=1
shift
goto again

:end
set i=

Here's a test of this in a batch file called doit.bat:

C:\Users\johnma>doit a b c d "e f" g hi j k l mn o p

C:\Users\johnma>set
.
.
.
pkg1=a
pkg10=l
pkg11=mn
pkg12=o
pkg13=p
pkg2=b
pkg3=c 
pkg4=d
pkg5=e f
pkg6=g
pkg7=hi
pkg8=j
pkg9=k
.
.    
C:\Users\johnma>
1
  • 1
    +1, But the code would be more robust if your IF comparison used "%~1" instead of "%1". Your increment statement can be simplified to set /a i+=1
    – dbenham
    Jun 27, 2013 at 15:04
0

GetAll.cmd:

@set AllArg=%*
@echo %AllArg%

Split variable AllArg any method.

2
  • 2
    That would work, except I'm asking moreof how to split it, or work around that. Insteaof just getting it all in one big chunk like that is. Jun 27, 2013 at 3:36
  • 2
    This is a good idea, but not very helpful since you did not show how. It is tricky because you don't want to split on quoted delimiters. Perhaps more importantly, it is nearly impossible to implement properly because an unquoted = is a token delimiter and there is no practical method to search and replace = within batch.
    – dbenham
    Jun 27, 2013 at 14:59

Not the answer you're looking for? Browse other questions tagged .