Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since the values %1, %2, %3...etc in a batch can't be modify, I need to create a copy of it.

Question:

Q1: How to create a copy in one go?  
    Not like SET cp1=%1, SET cp2=%2, ...etc.

Q2: Can one access the variables in this copy by a for loop, which is similar to
    "FOR %%A IN (%*) DO (...)"
share|improve this question
    
In the for loop why can't you set a variable to %%A, then do what you want with it? – Karan Jan 23 '13 at 16:04
    
Because I need the modified value to pass them to another bat. – user565739 Jan 23 '13 at 17:13
    
However, just create var1~var9, assign them to %1~%9, using FOR %%A IN (var1 var2....var9) DO works for me. – user565739 Jan 23 '13 at 17:14
    
Ah, so you need to pass along multiple modified values to a second batch file, and thus cannot copy them one by one to the same variable in a loop and modify them? – Karan Jan 23 '13 at 17:17
1  
Yes, I got it, you need multiple modified values together. A for loop with a single variable to which each value is copied in turn won't help in this case. So what's wrong with your solution? Edit: You know, you've asked 3 batch-related questions, and I think they're all part of the same problem. If so, why not simply create a single question stating clearly what it is you're actually trying to do, rather than ask piecemeal? – Karan Jan 23 '13 at 17:26
up vote 1 down vote accepted

I can’t figure out a really clean way to run the second program (or batch file, or whatever) with the modified argument list, but I believe I have a good 88% of what you want:

@echo off
setlocal enabledelayedexpansion
set i=0
for %%A in (%*) do (
    set /a i+=1
    echo arg !i! is %%A
    set var!i!=%%A      // This sets var1=%1, var2=%2, …
                        // and, of course, you could modify the values at this point.
)
                        // And at this point, %i% is the number of arguments.
 (Other stuff)
          ︙
for /L %%J in (1, 1, %i%) do (
    call :kludge var%%J           // var%%J is “var1” or “var2” or …
    echo arg %%J is !this_value!  // This iterates through the original args,
                                  // and, of course, you could modify them here, too.
)
 (Other stuff)
          ︙
exit /b

:kludge
set this_value=!%1!
exit /b

share|improve this answer
    
Thanks for the trick set var!i!=%%A , I will try it. – user565739 Jan 25 '13 at 8:48
    
100% what I want. Make a copy of all argument in one go! – user565739 Jan 25 '13 at 8:59

As someone mention on SO

%* seems to hold all of the arguments passed to the script.

it was here

Update 1:

:loop
@echo %1
shift
if not "%~1"=="" goto loop

You can save them all to variable e.g

SET ARGS_LIST=%*
echo %ARGS_LIST%
share|improve this answer
    
But I can't not modify these values, so I create a copy. This is what I asked here. – user565739 Jan 23 '13 at 15:00
    
u can loop through all arguments by the upper code (update 1) – Ishikawa Yoshi Jan 23 '13 at 15:07

I'm not sure why you need to do this, but you can make a copy of the parameters using:

set parms=%*

The above will work for most situations, though there are some cases where it will fail. For example, it will fail if a parameter includes unquoted special characters.

The parameters can safely be iterated using a FOR loop if and only if none of the parameters contain * or ?, and all parameters containing space or special characters are quoted.

for %%P in (%parms%) do ...
share|improve this answer
    
The problem is that I can't still modify the value of %%P, because %%P will be %1, %2, ...etc – user565739 Jan 25 '13 at 8:46

You must log in to answer this question.

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