简体   繁体   中英

Generating a batch file with %* as a parameter

I have a tool written in dos/batch that generates other batch files as need...

works fine and is very useful...
basically the inner working is the is the same as doing

echo dir /s \windows\*.log > logs.cmd

this make a script named "logs" with some functionality
(it's a simplification just to illustrate the objective)

recently I need to add %* as a parameter of the generated batch file
(to capture all the input parameters from a command line)

the problem is that I don't know how to generate the "%*" sequence

I try several escape sequences and tricks google(ed) from the net
but nothing seems to work

?any ideas??

According to this page, % marks must be escaped like this: %% (double percentage signs, so the second one will show up.

You should be able to pass in the * as a normal escape character, that is: ^% . If that doesn't work, try "*" (but the ^ escape should work).

So together, passing in the %* param to your code would look like this: %%^* .

Almost nothing to add to the jmoon's answer, but as it does not work for you (but works for me), you can try with something like

@echo off
    setlocal enableextensions disabledelayedexpansion

    echo Current script arguments
    echo --------------------------------------------
    echo %*
    echo --------------------------------------------

    >"output.cmd" (
        echo @echo off
        echo echo %%*
        echo for %%%%a in (%%*^) do echo %%%%a
    )

    echo Generated file
    echo --------------------------------------------
    type output.cmd
    echo --------------------------------------------

    echo Pass different set of arguments
    echo --------------------------------------------
    call output.cmd 1 2 3 4 
    echo --------------------------------------------

    echo Pass our arguments
    echo --------------------------------------------
    call output.cmd %*
    echo --------------------------------------------

    echo Pass escaped string
    echo --------------------------------------------
    call output.cmd %%%%*
    echo --------------------------------------------

This will generate

W:\>input.cmd a b c d e
Current script arguments
--------------------------------------------
a b c d e
--------------------------------------------
Generated file
--------------------------------------------
@echo off
echo %*
for %%a in (%*) do echo %%a
--------------------------------------------
Pass different set of arguments
--------------------------------------------
1 2 3 4
1
2
3
4
--------------------------------------------
Pass our arguments
--------------------------------------------
a b c d e
a
b
c
d
e
--------------------------------------------
Pass escaped string
--------------------------------------------
%*
--------------------------------------------
W:\>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM