Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)
share|improve this question
    
Are you going to pipe these in? Or type them at the start of the script? What format are you going to want arguments in? –  AthomSfere Jun 27 '13 at 1:37
    
possible duplicate of make a copy of all arguments in a batch –  Scott Jul 6 '13 at 19:32
add comment

closed as off-topic by Darth Android, teylyn, gronostaj, Scott, Breakthrough Jul 7 '13 at 19:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here, but can be asked on Stack Overflow." – Darth Android, teylyn, gronostaj
If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

up vote 6 down vote accepted

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).

share|improve this answer
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 '13 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 '13 at 14:13
add comment

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>
share|improve this answer
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 '13 at 15:04
add comment

GetAll.cmd:

@set AllArg=%*
@echo %AllArg%

Split variable AllArg any method.

share|improve this answer
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. –  RainbowDashDC Jun 27 '13 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 '13 at 14:59
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.