获取windowsbatch file中的最后一个命令行参数

我需要得到最后一个parameter passing给Windows批处理脚本,我该怎么做?

最简单的也许是最可靠的方法就是使用cmd自己的参数解析,然后shift直到不再存在。

由于这破坏了%1等的使用,所以你可以在一个子程序中做到这一点:

 @echo off call :lastarg %* echo Last argument: %LAST_ARG% goto :eof :lastarg set "LAST_ARG=%~1" shift if not "%~1"=="" goto lastarg goto :eof 

这将得到参数的数量:

 set count=0 for %%a in (%*) do set /a count+=1 

为了得到最后的论点,你可以这样做

 for %%a in (%*) do set last=%%a 

请注意,如果命令行有不平衡的引号,则会失败 – 命令行将被重新解析for而不是直接使用用于%1的解析。