获取 Windows 批处理文件中的最后一个命令行参数

发布于 2024-11-03 18:46:06 字数 41 浏览 0 评论 0原文

我需要将最后一个参数传递给 Windows 批处理脚本,我该怎么做?

I need to get last argument passed to windows batch script, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花心好男孩 2024-11-10 18:46:06

这将获取参数的计数:

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

要获取实际的最后一个参数,您可以执行以下操作

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

请注意,如果命令行具有不平衡的引号,这将失败 - 命令行将通过 for 重新解析,而不是直接解析使用用于 %1 等的解析。

This will get the count of arguments:

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

To get the actual last argument, you can do

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

Note that this will fail if the command line has unbalanced quotes - the command line is re-parsed by for rather than directly using the parsing used for %1 etc.

银河中√捞星星 2024-11-10 18:46:06

最简单也可能是最可靠的方法是仅使用 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

The easiest and perhaps most reliable way would be to just use cmd's own parsing for arguments and shift then until no more are there.

Since this destroys the use of %1, etc. you can do it in a subroutine:

@echo off
call :lastarg %*
echo Last argument: %LAST_ARG%
goto :eof

:lastarg
  set "LAST_ARG=%~1"
  shift
  if not "%~1"=="" goto lastarg
goto :eof
反差帅 2024-11-10 18:46:06

joey 的答案的增强版本:

@ECHO OFF
SETLOCAL

:test
    :: https://stackoverflow.com/a/5807218/7485823
    CALL :lastarg xxx %*
    ECHO Last argument: [%XXX%]
    CALL :skiplastarg yyy %*
    ECHO skip Last argument: [%yyy%]
GOTO :EOF

:: Return all but last arg in variable given in %1
:skiplastarg returnvar args ...
    SETLOCAL
        SET $return=%1
        SET SKIP_LAST_ARG=
        SHIFT
    :skiplastarg_2
        IF NOT "%~2"=="" SET "SKIP_LAST_ARG=%SKIP_LAST_ARG% %1"
        SHIFT
        IF NOT "%~1"=="" GOTO skiplastarg_2
    ENDLOCAL&CALL SET "%$return%=%SKIP_LAST_ARG:~1%"
GOTO :EOF

:: Return last arg in variable given in %1
:lastarg returnvar args ...
    SETLOCAL
      SET $return=%1
      SET LAST_ARG=
  SHIFT
    :LASTARG_2
      SET "LAST_ARG=%1"
      SHIFT
      IF NOT "%~1"=="" GOTO lastarg_2
    ENDLOCAL&call SET %$return%=%LAST_ARG%
GOTO :EOF

使用参数运行它:

abe“ba na na”“圆”

并得到:

最后一个参数:[“圆”]

跳过最后一个参数:[abe "ba na na"]

An enhanced version of joey's answer:

@ECHO OFF
SETLOCAL

:test
    :: https://stackoverflow.com/a/5807218/7485823
    CALL :lastarg xxx %*
    ECHO Last argument: [%XXX%]
    CALL :skiplastarg yyy %*
    ECHO skip Last argument: [%yyy%]
GOTO :EOF

:: Return all but last arg in variable given in %1
:skiplastarg returnvar args ...
    SETLOCAL
        SET $return=%1
        SET SKIP_LAST_ARG=
        SHIFT
    :skiplastarg_2
        IF NOT "%~2"=="" SET "SKIP_LAST_ARG=%SKIP_LAST_ARG% %1"
        SHIFT
        IF NOT "%~1"=="" GOTO skiplastarg_2
    ENDLOCAL&CALL SET "%$return%=%SKIP_LAST_ARG:~1%"
GOTO :EOF

:: Return last arg in variable given in %1
:lastarg returnvar args ...
    SETLOCAL
      SET $return=%1
      SET LAST_ARG=
  SHIFT
    :LASTARG_2
      SET "LAST_ARG=%1"
      SHIFT
      IF NOT "%~1"=="" GOTO lastarg_2
    ENDLOCAL&call SET %$return%=%LAST_ARG%
GOTO :EOF

Run it with the arguments:

abe "ba na na" "cir cle"

and get:

Last argument: ["cir cle"]

skip Last argument: [abe "ba na na"]

神妖 2024-11-10 18:46:06
set first=""
set last=""
for %%a in (%*) do (
SETLOCAL ENABLEDELAYEDEXPANSION
if !first!=="" (set first=!last!) else (set first=!first! !last!)
set last=%%a

)
ENDLOCAL & set "last=%last%" & set "first=%first%"
echo %last%  "and" %first%
set first=""
set last=""
for %%a in (%*) do (
SETLOCAL ENABLEDELAYEDEXPANSION
if !first!=="" (set first=!last!) else (set first=!first! !last!)
set last=%%a

)
ENDLOCAL & set "last=%last%" & set "first=%first%"
echo %last%  "and" %first%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文