【发布时间】:2011-10-28 21:55:28
【问题描述】:
我正在努力改进我提出的作为How to write a batch file showing path to executable and version of Python handling Python scripts on Windows? 问题答案的脚本。为了防止 Open With 对话框,我想读取 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 我也注意到了这一点。然而,考虑到我在编写看似简单的批处理文件时遇到的所有这些问题,我忽略了这个问题。
标签: windows parsing batch-file