3

I have this single line CMD file TEST.CMD:

for %%f in (%1 %2 %3 %4 %5 %6 %7 %8) DO ECHO %%f

If I run this:

TEST this is a test

it correctly echos each parameter on a separate line, i.e.,

this
is
a
test

However if a parameter contains asterisk it skips it. E.g.,

TEST this is a* test

Results in:

this
is
test

How do I get the parameter with an asterisk to be treated like a normal token?

Thanks.

Neil Weicher
  • 2,370
  • 6
  • 34
  • 56

3 Answers3

1

The simplest method that works for most parameters is to transfer the parameters to an "array" of variables, and then use FOR /L to loop through the array. This is best achieved with delayed expansion.

This technique can process an arbitrary number of parameters - it is not limited to 9.

@echo off
setlocal

:: Transfer parameters to an "array"
set arg.cnt=1
:getArgs
(set arg.%arg.cnt%=%1)
if defined arg.%arg.cnt% (
  set /a arg.cnt+=1
  shift /1
  goto :getArgs
)
set /a arg.cnt-=1

:: Process the "array"
setlocal enableDelayedExpansion
for /l %%N in (1 1 %arg.cnt%) do echo arg %%N = !arg.%%N!
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

The only way I have found without knowing the parameters beforehand is to echo the parameters in the for loop

for /f %%f in ('"echo %1 && echo %2 && echo %3 && etc"') DO ECHO %%f
Bali C
  • 30,582
  • 35
  • 123
  • 152
0

You can't print that, the asterisk is a dynamic operator that matches "1 or more characters" in some commands, like the FOR command, the only way is to use the /F parameter that gets the output of a command.

See what happens if you use this:

@Echo OFF

Pushd "C:\"

Call :sub a b c * d e

:sub
for %%f in (%1 %2 %3 %4 %5 %6 %7 %8) DO ECHO %%f
Pause&Exit

(The FOR prints all the files in current directory)

Then you need to do... :

@Echo OFF

Call :sub a b c* d e

:sub
FOR /F "tokens=*" %%a in ('Echo %*') DO (ECHO %%a)
Pause&Exit
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417