web-dev-qa-db-ja.com

渡された引数の一覧をWindowsのバッチスクリプト(.bat)で取得する

スクリプトに渡されるすべての引数のリストを保持する、Bashの$@に対するWindowsバッチ対応物を見つけたいのですが。

それともshiftを気にする必要がありますか?

361
wheleph

dancavallaro はそれを正しく持っています、すべてのコマンドラインパラメータ(スクリプト名自体を除く)のための%*。また、これらは便利かもしれません:

%0 - バッチファイルの呼び出しに使用されるコマンド(foo..\fooc:\bats\foo.batなど)
%1は最初のコマンドラインパラメータです。
%2は2番目のコマンドラインパラメーターです。
など、%9まで(およびSHIFTは9日以降のものに使用できます)。

%~nx0 - 呼び出しメソッドに関係なく、バッチファイルの実際の名前(some-batch.bat)
%~dp0 - スクリプトへのドライブとパス(d:\ scripts)
%~dpnx0 - スクリプトの完全修飾パス名(d:\ scripts\some-batch.bat)

https://www.ss64.com/nt/syntax-args.html および https://www.robvanderwoude.com/parameters.htmlでより多くの情報の例

550
matt wilkie

%*はスクリプトに渡されたすべての引数を保持しているようです。

132
dancavallaro

%1 ... %nおよび%*は引数を保持しますが、コンテンツが解釈されるため、引数にアクセスするのは難しい場合があります。
したがって、通常のステートメントでこのようなものを処理することは不可能です

myBatch.bat "&"^&

Cmd.exeがアンパサンドの1つを実行しようとするため、各行が失敗します(%1の内容は"&"&です)

set var=%1
set "var=%1"
set var=%~1
set "var=%~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 onの出力をリダイレクトするには、2つのFOR-LOOPSが必要です。

余分な文字* #は、/?(REMのヘルプを表示します)などのコンテンツに対して安全にするために使用されます。
または行末のキャレット^は、複数行の文字として機能します。

FOR/Fは、遅延展開をオフにして動作する必要があります。それ以外の場合、コンテンツは「!」で動作します。破壊されるでしょう。
param1の余分な文字を削除した後、取得しました。

また、param1を安全に使用するには、遅延展開を有効にします。

編集:%0への1つのコメント

%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
62
jeb

私はあなたがこれらの情報を調べる必要がある次の時にそれを見つけました。ブラウザを開いてそれをグーグルするのではなく、あなたの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 %*
46
KFL

スクリプトへのすべての引数を取得する方法は次のとおりです。

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

これは、引数を取得してそれらをenv varsとして設定するためのかなり簡単な方法です。この例では、それらをキーと値と呼びます。

次のコード例を "args.bat"として保存します。次に、コマンドラインから保存したバッチファイルを呼び出します。例:arg.bat --x 90 --y 120

私はあなたをプロセスを通して進めるためにいくつかのechoコマンドを提供しました。しかし最終的な結果として、 - xの値は90になり、 - yの値は120になります(つまり、上記の例のように実行した場合は;-))。

その後、 'if defined'条件文を使用して、コードブロックを実行するかどうかを決定できます。 "arg.bat --x hello-world"というステートメントを使用すると、結果は "hello-world"になります。バッチを実行するともっと理にかなっているはずです。

@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
4
user2631477

次のコードは配列( 'params')をシミュレートします - スクリプトが受け取ったパラメーターを受け取り、それらを変数params_1 .. params_nに格納します。ここで、n = params_0 =配列の要素数

@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
2
user5280669

スペースを含む引用符でパラメータを指定した場合、%*は正しく機能しません。私が見つけた最良の解決策は、すべての引数を結合するループを持つことです。 https://serverfault.com/a/22541

set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start

:done
(use %args% here)
1
Speedstone

enter image description here

ループを使用するには、すべての引数を取得し、純粋なバッチで取得します。

@echo off && setlocal EnableDelayedExpansion

 set "_cnt=0" && for %%Z in (%*) do ( 
 set "_arg_=%%Z" && set /a "_cnt=!_cnt! + 1" && set "_arg_[!_cnt!]=!_arg_!"
 shift && for /l %%l in (!_cnt! 1 !_cnt!) do echo/ The argument n:%%l is: !_arg_[%%l]!
 )

goto :eof 

あなたのコードは必要なところで引数番号を使って何かをする準備ができています、...

 @echo off && setlocal EnableDelayedExpansion

 set "_cnt=0" && for %%Z in (%*) do ( 
 set "_arg_=%%Z" && set /a "_cnt=!_cnt! + 1" && set "_arg_[!_cnt!]=!_arg_!"
 shift 

 )

 run_command !_arg_[1]! !_arg_[2]! !_arg_[2]!> log.txt
1
It Wasn't Me

For Commadを使用して、引数のリストを取得できます。

ヘルプ= For /?

ヘルプ= Setlocal /?

ここに私の道=

@echo off
::For Run Use This = cmd /c ""Args.cmd" Hello USER programming-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

シフトコマンドは必要ありません!!

0
scientist_7

上記の多くの情報が私をさらに調査し、最終的に私の答えに導いたので、私がそれが他の誰かに役立つことを願って私がしたことに貢献したいと思いました:

  • また、ファイル内で処理できるように、さまざまな変数をバッチファイルに渡したいと思いました。

  • 引用符を使用してそれらをバッチファイルに渡しても問題ありませんでした。

  • 私はそれらが以下のように処理されることを望みます - しかし手動で書き出す代わりにループを使用します:

だから私はこれを実行したいと思いました:

prog_ZipDeleteFiles.bat "_appPath=C:\Services\Logs\PCAP" "_appFile=PCAP*.?"

そしてforループの魔法を通してバッチファイルの中でこれをしてください:

set "_appPath=C:\Services\Logs\PCAP"
set "_appFile=PCAP*.?"

私が抱えていた問題は、forループを使用しようとしたすべての試みがうまくいかなかったことです。以下の例:

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

理由は、ループの最初の繰り返し中にforループが行全体を読み取り、2番目のパラメーターを%% Bに割り当てたためです。 %*はすべての引数を表すため、処理する行は1行だけです。つまり、forループは1回だけ実行されます。これは意図的な結果であり、私の論理は間違っていました。

そのため、forループの使用をやめ、if、shift、およびgotoステートメントを使用して、実行しようとしていたことを単純化しました。ちょっとしたハックには同意しましたが、私のニーズにより適しています。すべての引数をループ処理し、それらをすべて処理した後でifステートメントを使用してループから抜け出すことができます。

私が達成しようとしていたことに対する勝利の声明:

echo on
:processArguments
:: Process all arguments in the order received
if defined %1 then (
    set %1
    shift
    goto:processArguments
) ELSE (
    echo off 
)

UPDATE - 代わりに以下を修正しなければなりませんでした。%1を参照しようとしているときにシフトをこのように使用するときに、すべての環境変数を公開していました。

echo on
shift
:processArguments
:: Process all arguments in the order received
if defined %0 then (
    set %0
    shift
    goto:processArguments
) ELSE (
    echo off 
)
0
John Ahearn
 @ echo off 
:start 
 
 ::ここにコードを挿入してください
 echo。%% 1 is now:%〜1 
 。] :: "%〜2" NEQ "の場合はここにコードを挿入してください
 
(
 shift 
 goto:start 
)
0
user298107