Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何获取参数列表?_Windows_Batch File - Fatal编程技术网

Windows 如何获取参数列表?

Windows 如何获取参数列表?,windows,batch-file,Windows,Batch File,我想找到一个与Bash的$@相对应的Windows批处理,它包含传递到脚本中的所有参数的列表 或者我不得不为shift?%*似乎保存了传递给脚本的所有参数。对了,%*用于所有命令行参数(不包括脚本名称本身)。您还可能会发现以下内容很有用: %0-用于调用批处理文件的命令(可以是foo,。\foo,c:\bats\foo.bat等) %1是第一个命令行参数, %2是第二个命令行参数, 依此类推,直到%9(9号之后的可以使用SHIFT) %~nx0-批处理文件的实际名称,与调用方法无关(some b

我想找到一个与Bash的
$@
相对应的Windows批处理,它包含传递到脚本中的所有参数的列表


或者我不得不为
shift

%*
似乎保存了传递给脚本的所有参数。

对了,
%*
用于所有命令行参数(不包括脚本名称本身)。您还可能会发现以下内容很有用:

%0
-用于调用批处理文件的命令(可以是
foo
。\foo
c:\bats\foo.bat
等)
%1
是第一个命令行参数,
%2
是第二个命令行参数,
依此类推,直到
%9
(9号之后的可以使用
SHIFT

%~nx0
-批处理文件的实际名称,与调用方法无关(some batch.bat)
%~dp0
-脚本的驱动器和路径(d:\scripts)
%~dpnx0
-是脚本的完全限定路径名(d:\scripts\some batch.bat)


更多信息示例位于和

%1
<代码>%n和
%*
保存参数,但访问它们可能会很困难,因为内容将被解释。
因此,用正常的语句来处理这样的事情是不可能的

myBatch.bat "&"^&
每行都失败,因为cmd.exe尝试执行一个符号AND(%1的内容是
“&”&

但是存在一个临时文件的解决方法

@echo off
SETLOCAL DisableDelayedExpansion

SETLOCAL
for %%a in (1) do (
    set "prompt=$_"
    echo on
    for %%b in (1) do rem * #%1#
    @echo off
) > param.txt
ENDLOCAL

for /F "delims=" %%L in (param.txt) do (
  set "param1=%%L"
)
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo %%1 is '!param1!'
诀窍是启用
echo on
,并在
rem
语句后展开
%1
(也适用于%2..%*)。
但是为了能够在上重定向
echo的输出,您需要两个FOR-loop

额外的字符
*#
用于防止类似
/?
的内容(将显示REM的帮助)。
或者,行末端的插入符号可以用作多行字符

FOR/F应在延迟扩展关闭的情况下工作,否则带有“!”的内容将被销毁。
在删除
param1
中的额外字符后,您得到了它

要安全地使用
param1
,请启用延迟扩展

编辑:对%0添加一条备注

%0
包含用于调用批处理的命令,还保留了类似于
FoO.BaT

但是在调用函数后,
%0
以及在
%0
中包含函数名(或者更好的是用于调用函数的字符串)。
但是使用
%~f0
您仍然可以回忆起文件名

@echo off
echo main %0, %~0, %~f0
call :myLabel+xyz
exit /b

:MYlabel
echo func %0, %~0, %~f0
exit /b
输出

main test.bat, test.bat, C:\temp\test.bat
func :myLabel+xyz, :myLabel+xyz, C:\temp\test.bat

我发现下次当你需要查找这些信息时。您可以在cmd中键入
call/?
,而不是打开浏览器并用谷歌搜索它,您将得到:

...

%* in a batch script refers to all the arguments (e.g. %1 %2 %3
    %4 %5 ...)

Substitution of batch parameters (%n) has been enhanced.  You can
now use the following optional syntax:

    %~1         - expands %1 removing any surrounding quotes (")
    %~f1        - expands %1 to a fully qualified path name
    %~d1        - expands %1 to a drive letter only
    %~p1        - expands %1 to a path only
    %~n1        - expands %1 to a file name only
    %~x1        - expands %1 to a file extension only
    %~s1        - expanded path contains short names only
    %~a1        - expands %1 to file attributes
    %~t1        - expands %1 to date/time of file
    %~z1        - expands %1 to size of file
    %~$PATH:1   - searches the directories listed in the PATH
                   environment variable and expands %1 to the fully
                   qualified name of the first one found.  If the
                   environment variable name is not defined or the
                   file is not found by the search, then this
                   modifier expands to the empty string

The modifiers can be combined to get compound results:

    %~dp1       - expands %1 to a drive letter and path only
    %~nx1       - expands %1 to a file name and extension only
    %~dp$PATH:1 - searches the directories listed in the PATH
                   environment variable for %1 and expands to the
                   drive letter and path of the first one found.
    %~ftza1     - expands %1 to a DIR like output line

In the above examples %1 and PATH can be replaced by other
valid values.  The %~ syntax is terminated by a valid argument
number.  The %~ modifiers may not be used with %*

在脚本中检索所有参数的方法如下:

@ECHO off
ECHO The %~nx0 script args are...
for %%I IN (%*) DO ECHO %%I
pause

下面是一种获取参数并将其设置为env vars的相当简单的方法。在本例中,我将它们称为键和值

将下面的代码示例保存为“args.bat”。然后调用批处理文件 您是从命令行保存的。 示例:arg.bat--x90--y120

我提供了一些echo命令来帮助您完成这个过程。但是 最终的结果是--x的值为90,--y的值为120(如果按照上面指定的方式运行示例;-)

然后可以使用“if defined”条件语句来确定是否运行代码块。 让我们说跑步: “arg.bat--x hello world” 然后我可以使用语句“IF DEFINED--x echo%--x%”,结果将是“helloworld”。如果您运行批处理,它应该更有意义

@setlocal enableextensions enabledelayedexpansion
@ECHO off
ECHO.
ECHO :::::::::::::::::::::::::: arg.bat example :::::::::::::::::::::::::::::::
ECHO :: By:      User2631477, 2013-07-29                                   ::
ECHO :: Version: 1.0                                                         ::
ECHO :: Purpose: Checks the args passed to the batch.                        ::
ECHO ::                                                                      ::
ECHO :: Start by gathering all the args with the %%* in a for loop.          ::
ECHO ::                                                                      ::
ECHO :: Now we use a 'for' loop to search for our keys which are identified  ::
ECHO :: by the text '--'. The function then sets the --arg ^= to the next    ::
ECHO :: arg. "CALL:Function_GetValue" ^<search for --^> ^<each arg^>         ::
ECHO ::                                                                      ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ECHO.

ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: From the command line you could pass... arg.bat --x 90 --y 220       ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO.Checking Args:"%*"

FOR %%a IN (%*) do (
    CALL:Function_GetValue "--","%%a" 
)

ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: Now lets check which args were set to variables...                   ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: For this we are using the CALL:Function_Show_Defined "--x,--y,--z"   ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
CALL:Function_Show_Defined "--x,--y,--z"
endlocal
goto done

:Function_GetValue

REM First we use find string to locate and search for the text.
echo.%~2 | findstr /C:"%~1" 1>nul

REM Next we check the errorlevel return to see if it contains a key or a value
REM and set the appropriate action.

if not errorlevel 1 (
  SET KEY=%~2
) ELSE (
  SET VALUE=%~2
)
IF DEFINED VALUE (
    SET %KEY%=%~2
    ECHO.
    ECHO ::::::::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::
    ECHO :: The KEY:'%KEY%' is now set to the VALUE:'%VALUE%'                     ::
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO.
    ECHO %KEY%=%~2
    ECHO.
    REM It's important to clear the definitions for the key and value in order to
    REM search for the next key value set.
    SET KEY=
    SET VALUE=
)
GOTO:EOF

:Function_Show_Defined 
ECHO.
ECHO ::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::::
ECHO :: Checks which args were defined i.e. %~2
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
SET ARGS=%~1
for %%s in (%ARGS%) DO (
    ECHO.
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO :: For the ARG: '%%s'                         
    IF DEFINED %%s (
        ECHO :: Defined as: '%%s=!%%s!'                                             
    ) else (
        ECHO :: Not Defined '%%s' and thus has no value.
    )
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO.
)
goto:EOF

:done
@setlocal enableextensions enabledelayedexpansion
@回音
回声。
回声:示例:arg.bat示例:
ECHO::By:User26314771013-07-29::
ECHO::版本:1.0::
ECHO::用途:检查传递给批处理的参数:
回声:::
ECHO::首先在for循环中收集所有带有%%*的参数:
回声:::
ECHO::现在我们使用一个“for”循环来搜索已标识的密钥:
ECHO::通过文本“--”实现。然后,该函数将--arg^=设置为下一个::
回声::arg。“调用:函数_GetValue”^^::
回声:::
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
回声。
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
ECHO::从命令行可以传递。。。arg.bat--x90--y220::
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
回声。
ECHO.正在检查参数:“%*”
对于%%a IN(%*)do(
调用:函数_GetValue“--”,“%%a”
)
回声。
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
ECHO::现在让我们检查哪些参数设置为变量…::
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
回声。
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
ECHO::为此,我们使用调用:函数_Show_Defined“-x,--y,--z”:
回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:回声:
回声。
调用:函数显示定义--x,-y,-z
端部
好了
:函数\获取值
REM首先我们使用find string来定位和搜索文本。
回声。%~2| findstr/C:“%~1”1>nul
REM接下来我们检查errorlevel返回,看看它是否包含键或值
REM并设置适当的操作。
如果不是错误级别1(
设置键=%~2
)否则(
设置值=%~2
)
如果定义值(
设置%KEY%=%~2
回声。
回音:回音:回音:回音:回音
@setlocal enableextensions enabledelayedexpansion
@ECHO off
ECHO.
ECHO :::::::::::::::::::::::::: arg.bat example :::::::::::::::::::::::::::::::
ECHO :: By:      User2631477, 2013-07-29                                   ::
ECHO :: Version: 1.0                                                         ::
ECHO :: Purpose: Checks the args passed to the batch.                        ::
ECHO ::                                                                      ::
ECHO :: Start by gathering all the args with the %%* in a for loop.          ::
ECHO ::                                                                      ::
ECHO :: Now we use a 'for' loop to search for our keys which are identified  ::
ECHO :: by the text '--'. The function then sets the --arg ^= to the next    ::
ECHO :: arg. "CALL:Function_GetValue" ^<search for --^> ^<each arg^>         ::
ECHO ::                                                                      ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ECHO.

ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: From the command line you could pass... arg.bat --x 90 --y 220       ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO.Checking Args:"%*"

FOR %%a IN (%*) do (
    CALL:Function_GetValue "--","%%a" 
)

ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: Now lets check which args were set to variables...                   ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO :: For this we are using the CALL:Function_Show_Defined "--x,--y,--z"   ::
ECHO ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
CALL:Function_Show_Defined "--x,--y,--z"
endlocal
goto done

:Function_GetValue

REM First we use find string to locate and search for the text.
echo.%~2 | findstr /C:"%~1" 1>nul

REM Next we check the errorlevel return to see if it contains a key or a value
REM and set the appropriate action.

if not errorlevel 1 (
  SET KEY=%~2
) ELSE (
  SET VALUE=%~2
)
IF DEFINED VALUE (
    SET %KEY%=%~2
    ECHO.
    ECHO ::::::::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::
    ECHO :: The KEY:'%KEY%' is now set to the VALUE:'%VALUE%'                     ::
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO.
    ECHO %KEY%=%~2
    ECHO.
    REM It's important to clear the definitions for the key and value in order to
    REM search for the next key value set.
    SET KEY=
    SET VALUE=
)
GOTO:EOF

:Function_Show_Defined 
ECHO.
ECHO ::::::::::::::::::: %~0 ::::::::::::::::::::::::::::::::
ECHO :: Checks which args were defined i.e. %~2
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO.
SET ARGS=%~1
for %%s in (%ARGS%) DO (
    ECHO.
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO :: For the ARG: '%%s'                         
    IF DEFINED %%s (
        ECHO :: Defined as: '%%s=!%%s!'                                             
    ) else (
        ECHO :: Not Defined '%%s' and thus has no value.
    )
    ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ECHO.
)
goto:EOF

:done
@echo off :start :: Insert your code here echo.%%1 is now:%~1 :: End insert your code here if "%~2" NEQ "" ( shift goto :start )
@echo off

rem Storing the program parameters into the array 'params':
rem Delayed expansion is left disabled in order not to interpret "!" in program parameters' values;
rem however, if a parameter is not quoted, special characters in it (like "^", "&", "|") get interpreted at program launch
set /a count=0
:repeat
    set /a count+=1
    set "params_%count%=%~1"
    shift
    if defined params_%count% (
        goto :repeat
    ) else (
        set /a count-=1
    )    
set /a params_0=count

rem Printing the program parameters stored in the array 'params':
rem After the variables params_1 .. params_n are set with the program parameters' values, delayed expansion can
rem be enabled and "!" are not interpreted in the variables params_1 .. params_n values
setlocal enabledelayedexpansion
    for /l %%i in (1,1,!params_0!) do (
        echo params_%%i: "!params_%%i!"
    )
endlocal

pause
goto :eof
prog_ZipDeleteFiles.bat "_appPath=C:\Services\Logs\PCAP" "_appFile=PCAP*.?"
set "_appPath=C:\Services\Logs\PCAP"
set "_appFile=PCAP*.?"
for /f "tokens* delims= " in %%A (%*) DO (
   set %%A
)
set "_appPath=C:\Services\Logs\PCAP"
set "_appPath=C:\Services\Logs\PCAP"
set "_appFile=PCAP*.?"
SETLOCAL EnableDelayedExpansion
echo on
:processArguments
:: Process all arguments in the order received
if defined %1 then (
    set %1
    shift
    goto:processArguments
) ELSE (
    echo off 
)
echo on
shift
:processArguments
:: Process all arguments in the order received
if defined %0 then (
    set %0
    shift
    goto:processArguments
) ELSE (
    echo off 
)
set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start

:done
(use %args% here)
@echo off
::For Run Use This = cmd /c ""Args.cmd" Hello USER Scientist etc"
setlocal EnableDelayedExpansion
set /a Count=0
for %%I IN (%*) DO (
 Echo Arg_!Count! = %%I
 set /a Count+=1 
)
Echo Count Of Args = !Count!
Endlocal
Test.bat uno dos tres cuatro cinco seis siete
@echo off 
setlocal EnableDelayedExpansion

echo Option 1: one by one (same line)
echo %3, %4, %5
echo.

echo Option 2: Loop For one by one
for %%a in (%3, %4, %5) do echo %%a
echo.

echo Option 3: Loop For with check of limits
set i=0
for %%a in (%*) do (
    set /A i=i+1
    If !i! GTR 2 if !i! LSS 6 echo %%a
)
echo.

echo Option 4: Loop For with auxiliary list
for /l %%i in (3,1,5) do  (
    set a=%%i
    set b=echo %%
    set b=!b!!a!
    call !b!
)
echo.

echo Option 5: Assigning to an array of elements previously
set e[0]=%0
set i=0 
for %%a in (%*) do (
    set /A i=i+1
    set e[!i!]=%%a
)
for  /l %%i in (3,1,5) do (
    echo !e[%%i]!
)
echo.

echo Option 6: using shift and goto loop. It doesn't work with for loop
set i=2
:loop6
    set /A i=i+1
    echo %3
    shift
    If %i% LSS 5 goto :loop6