我正在努力改进脚本,我建议作为How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?问题的答案。为了防止打开方式对话框我想读ftype
命令的输出,从它的 可执行文件的解压路径,并检查它是否存在。如何将带嵌入空格的双引号字符串拆分为批处理文件中的空格?
这
@echo off
setlocal EnableDelayedExpansion
rem c:\ftype Python.File ->
rem Python.File="c:\path with spaces, (parentheses) and % signs\python.exe" "%1" %*
for /f "tokens=2 delims==" %%i in ('ftype Python.File') do (
set "reg_entry=%%i"
)
reg_entry's
内容之后是
"c:\path with spaces and (parentheses) and % signs\python.exe" "%1" %*
如何拆分此得到 "c:\path with spaces, (parentheses) and % signs\python.exe"
,"%1"
和%*
?
编辑
我在阅读Aacini的答案后尝试使用call
,它几乎奏效。但是,它不处理%
标志。
@echo off
setlocal EnableDelayedExpansion
set input="c:\path with spaces and (parentheses) and %% signs\python.exe" "%%1" %%*
echo !input!
call :first_token output !input!
echo !output!
goto :eof
:first_token
set "%~1=%2"
goto :eof
输出
"c:\path with spaces and (parentheses) and % signs\python.exe" "%1" %*
"c:\path with spaces and (parentheses) and 1"
我看着我的计算机的FTYPE输出,发现文件不包含在引号内的一些条目,即使路径包含空格。我担心你使用FTYPE的策略可能不可靠。 – dbenham
@dbenham我也注意到了这一点。然而,考虑到所有这些问题,我写了什么似乎是简单的批处理文件,我掩盖了这个问题。 –