windows - 最后一个参数中的正斜杠会导致批处理文件 ("%~dp0") 的目录路径发生更改

标签 windows batch-file cmd command-line-arguments

我正在学习如何在批处理脚本中使用参数,并最终创建了某种用于读取参数和设置参数的模板

@echo off
SetLocal EnableDelayedExpansion
set needextra=
set errstat=
set noflag=
set param_f=Not set yet
set param_g=You didn't use G-flag

:readARGS
IF [%1] == [] goto :endARGS
call :arg_%1 2> nul
IF ERRORLEVEL 1 call :arg_default %1
SHIFT
IF DEFINED needextra (
    set %needextra%=%~1
    SHIFT
    set needextra=
)
goto :readARGS
:endARGS
echo path to directory of batch script: %~dp0
echo    - noflag:  !noflag!
echo    - param_f: !param_f!
echo    - param_g: !param_g!
EndLocal
exit /b 0

打印目录的第一个回显对于我的问题很重要(见下文) 之后,我为每个标志 (arg_/flag) 创建一个函数,并为没有标志的参数 (arg_default) 创建一个函数:

:arg_/f           -- flag f: set param_f to value of next argument
set needextra=param_f
exit /b 0

:arg_/g           -- flag g: just set the param_g to a value
set param_g=You used the G-flag
exit /b 0

:arg_default      -- default, neither flag f or g: just set the noflag
echo noflag=%~1
exit /b 0

当我将所有内容放入批处理文件中时,假设 C:\Users\user\scripts\params.bat 并将目录放入我可以执行脚本的路径中:

> params "just an arg"
path to directory of batch script: C:\Users\user\scritpts\
   - noflag:  just an arg
   - param_f: Not set yet
   - param_g: You didn't use G-flag
> params another /G /F "this is f"
path to directory of batch script: C:\Users\user\scritpts\
   - noflag:  another
   - param_f: this is f
   - param_g: You used the G-flag

事实上,我将它放在函数中,我可以按照我希望的顺序输入参数,但如果我将 G 标志放在最后,我会得到这个奇怪的行为:

> params /F "this is f again" bizar /G
path to directory of batch script: C:\
   - noflag:  bizar
   - param_f: this is f again
   - param_g: You used the G-flag

%~dp0 仅返回 C:\!我尝试使用其他参数,将批处理文件移动到另一个目录,在目录中调用它,%~dp0 只返回C:\。事实上,每次最后一个参数包含“/”时,%~dp0 都会表现得“奇怪”,如下例所示:

> params /G /F stranger "what happens /here"
path to directory of batch script: C:\Users\user\script\what happens \
   - noflag:  what happens /here
   - param_f: stranger
   - param_g: You used the G-flag

有人可以解释一下为什么会发生这种情况吗?我无法弄清楚为什么,也无法在网上找到任何内容。我使用 Windows 10

我非常感谢您提供的任何帮助。

最佳答案

原因

params "just an arg"
params another /G /F "this is f"

作品和这个

params /G /F stranger "what happens /here"
params /F "this is f again" bizar /G

不起作用是......在这两种情况下它都不起作用!

正如 Magoo 指出的那样,您的问题是 shift 命令。默认情况下,它会移动所有参数,因此第十个参数存储在%9中,旧的%9存储在%中8 ...并且 %1 存储在 %0 中,丢失对当前批处理文件的引用。

在您的代码中,您将移动所有参数,直到所有参数都得到处理。这将最后一个参数留在 %0 中,此时事情变得有趣了。

当请求 %~dp0 时,最后一个参数存储在 %0 内,并且当我们请求参数内引用的元素的驱动器和文件夹时, cmd 尝试解析它,假设您知道您要求的内容,并且该变量包含对文件系统中元素的有效引用,将其转换为绝对路径,然后检索请求的元素。

这里你遇到了两种情况

  • %0 仅包含一个简单的字符串,不包含斜杠或反斜杠。 cmd 将其作为存储在当前事件目录中的文件名进行处理,因此,当您请求驱动器和路径时,将检索当前事件目录的驱动器和路径。在您的“工作”案例中,您以
  • 结尾
"C:\Users\user\script\just an arg" -> %~dp0 = "C:\Users\user\script\"
"C:\Users\user\script\this is f"   -> %~dp0 = "C:\Users\user\script\"
  • %0 包含带有斜杠或反斜杠的字符串。与之前的情况相同,但字符串包含一个相对路径(第一个失败的情况),该路径以当前事件目录或根文件夹的路径(第二个失败的情况)为前缀,因此以
  • 结尾
"C:\Users\user\script\what happens /here" -> %~dp0 = "C:\Users\user\script\what happens "
"C:/g" -> %~dp0 = "C:\"

如何解决?

解决这个问题的最简单方法可能是,正如 Magoo 所建议的那样,在对参数进行任何移动之前保存 %0 参数的值。

另一个选项是将 shift 命令更改为 shift/1,以表明移位将从第一个参数开始,保留 %0 不变。

如果无法使用这些选项,您仍然可以从子例程内部检索对批处理文件的引用

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Ensure %0 is discarded    
    shift
    echo %0

    call :getCurrentBatchFolder dp0
    echo %dp0%

    exit /b

:getCurrentBatchFolder returnVar
    set "%~1=%~dp0"
    goto :eof

关于windows - 最后一个参数中的正斜杠会导致批处理文件 ("%~dp0") 的目录路径发生更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646491/

相关文章:

windows - 如何设置在启动 perfmon 时运行的默认计数器?

java /SPNEGO : Unwanted SPN canonicalization?

windows - 适用于 Windows 的 Opera 移动模拟器不再可用?

batch-file - 自动化 FFMPEG

batch-file - PuTTY命令行用户输入

windows - 列出文件名和日期

batch-file - 批处理-我可以让cmd显示我正在使用的颜色然后使用它吗?

JAVA_HOME 未指向 JDK

C — 使用 chdir() 函数

windows - 如何访问位于 Windows 上具有保留系统名称(如 "CON")的文件夹中的本地文件?