如何在单引号求值中将带引号的参数传递给可执行文件,例如 FOR/F "delims="%%i IN ('"executable path"arg1 "arg2其中包含空格"') 做...
?
根据许多答案here ,我尝试在 Windows 批处理脚本中使用控制台应用程序的输出,并使用单引号让控制台对其进行评估。
但是,我需要引用一些我想要传递给该可执行文件的参数,这些参数也需要引用,因为路径也包含空格。
但是当我这样做时,可执行路径周围的引用就会中断。
这是可能的行:
FOR /F "delims=" %%i IN ('"!PathToExe!" action^=sanitize string^="!album!"') DO set "falbum=%%i"
(!PathToExe! 和 !album! 都包含空格。看来我需要在这里转义等号,因此需要抑扬符。延迟扩展是 上)
以上结果为“结果 A”:exe 路径的引用已损坏
'<Part of path to exe>' is not recognized as an internal or external command, operable program or batch file.
我尝试了不同引用用法和转义的不同组合,但尚未找到使其发挥作用的方法。
以下是一些尝试及其结果:
FOR /F "delims=" %%i IN ('"!PathToExe!" action^=sanitize string^=!album!') DO set "falbum=%%i")
!album! 周围没有引号会导致“结果 B”:正如预期的那样,只有第一个单词会与 string= 一起传递,所有其他单词都分散为单独的参数。
FOR /F "delims=" %%i IN ('"!PathToExe!" action^=sanitize string^=^"!album!^"') DO set "falbum=%%i")
FOR /F "delims=" %%i IN ('^"!PathToExe!^" action^=sanitize string^=^"!album!^"') DO set "falbum=%%i")
尝试转义 string= 参数或 exe 路径和字符串参数的引号:结果 A (仍然破坏 exe 路径的引用,给出:)
'<Part of path to exe>' is not recognized as an internal or external command, operable program or batch file.
FOR /F "delims=" %%i IN ('"!PathToExe!" action^=sanitize string^=^'!album!^'') DO set "falbum=%%i")
使用转义单引号:再次结果 B
FOR /F "delims=" %%i IN ('"!PathToExe!" action^=sanitize string^='"!album!") DO set "falbum=%%i")
在string=值之前结束单引号并简单地将其放在引号中似乎会导致所有内容被视为单个第一个参数(命令/路径):
The system cannot find the file '"<path to exe>" action=sanitize string='"<Album var with spaces and whatnot>".
引号是变量的一部分还是在 IN ('...')
行中逐字输入并不重要。
简单测试:
如果您将 %SystemRoot%\System32\cmd.exe
复制到包含空格的目录(例如, C:\带空格的文件夹\
并粘贴以下脚本:
@echo off
setlocal EnableDelayedExpansion
set PathToExe="%~dp0cmd.exe"
REM Toggle the next line to compare between quoted path with spaces and path without quotes or spaces:
REM set PathToExe=cmd.exe
set string=%~dp0
FOR /F "delims=" %%i IN ('!PathToExe! /C CD C:\windows\system32 ^& dir !string!') DO set "fstring=%%i"
echo !fstring!
pause
这应该说明在一个声明中包含两个引用的部分所面临的挑战。
如果 !string! 变量保持不带引号,您将得到“系统找不到指定的文件。”。
如果 !PathToExe! 变量的引号中断,您将看到类似“'C:\folder' 未被识别为内部或外部命令、可操作程序或批处理文件。”的内容。
最佳答案
for/F
命令在用于捕获和解析命令输出时,实际上使用 cmd/C
来执行该命令,该命令以特定方式处理引号。从它的帮助(cmd/?
):
If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters: 1. If all of the following conditions are met, then quote characters on the command line are preserved: - no /S switch - exactly two quote characters - no special characters between the two quote characters, where special is one of: &<>()@^| - there are one or more whitespace characters between the two quote characters - the string between the two quote characters is the name of an executable file. 2. Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.
这意味着:
for /F "delims=" %%I in ('"executable path" arg1 "arg2 which contains spaces"') do (…)
实际上尝试执行命令行:
cmd /C "executable path" arg1 "arg2 which contains spaces"
导致:
executable path" arg1 "arg2 which contains spaces
这显然是无效的语法。
为了解决此问题,请提供一对额外的引号:
for /F "delims=" %%I in ('^""executable path" arg1 "arg2 which contains spaces"^"') do (…)
最好转义这些额外的引号(通过 ^"
),因此无需更改任何潜在的转义序列。
关于windows - 在 Windows 批处理中获取单引号命令评估中带引号参数的命令结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64885490/