gpt4 book ai didi

windows - 在 Windows 批处理中的单引号命令评估中获取带引号参数的命令结果

转载 作者:行者123 更新时间:2023-12-03 11:10:06 25 4
gpt4 key购买 nike

如何在单引号评估中将带引号的参数传递给可执行文件 FOR /F "delims=" %%i IN ('"executable path" arg1 "arg2 which contains spaces"') do ... ?
根据许多答案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")
!专辑周围没有引号!结果是“ 结果 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 路径和字符串 arg 的引号: 结果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= value 之前结束单引号并在之后简单地将它放在引号中似乎导致所有内容都被视为单个第一个参数(命令/路径):

The system cannot find the file '"<path to exe>" action=sanitize string='"<Album var with spaces and whatnot>".


引号是变量的一部分还是字面输入在 IN ('...') 中都没有关系。线。
简单测试:
如果您复制 %SystemRoot%\System32\cmd.exe,您可以测试此行为。到带有空格的目录,例如 C:\folder with spaces\并在那里粘贴以下脚本:
@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 command,用于捕获和解析命令输出时,实际使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com