如何分割与批处理文件空间deliminated嵌入的空格双引号字符串? [英] How to split double quoted strings with embedded spaces deliminated with spaces in a batch file?
问题描述
我与改进,我建议作为一个答案脚本奋力如何编写一个批处理文件来显示可执行文件和Python版本路径操控性上的Windows?问题Python脚本。以prevent的打开方式的对话框我想读 FTYPE
命令输出,提取物的路径
从中可执行文件并检查它是否存在。
之后
关闭@echo
SETLOCAL EnableDelayedExpansion
REM C:\\ FTYPE Python.File - >
REM Python.File =C:用空格\\路径,(括号)和%标志\\ python.exe%1%*
FOR / F令牌= 2 delims ==%% i的('FTYPE Python.File')做(
设置reg_entry = %%我
)
reg_entry的
内容是
C:用空格和(括号)和%体征\\路径\\ python.exe%1%*
我如何分割这让C:用空格\\路径,(括号)和%标志\\ python.exe
,%1
和%*
?
编辑结果
我试着用呼叫
阅读Aacini的回答后几乎工作。它不处理%
的迹象,但是。
关闭@echo
SETLOCAL EnableDelayedExpansion
设置输入=C:\\用空格和(括号内)和%%迹象\\ python.exe路径%% 1%% *
回音!输入!
来电:first_token输出输入!
回音!输出!
GOTO:EOF:first_token
集%〜1 =%2
GOTO:EOF
输出
C:用空格和(括号)和%体征\\路径\\ python.exe%1%*
C:用空格和(括号内)和1 \\路径
这是另一种解析器是非常相似的CALL解析器是简单FOR。有两个复杂因素:
1的FOR不得扩大而延迟扩展的情况下启用它包含!
。这是很容易处理的。
2 - 内容不得包含通配符 *
或?
。在?
可以暂时代替,然后返回。但有没有简单的方法来搜索和替换 *
。
由于这个问题是试图解析出的路径,路径不能包含通配符,这个问题是很容易,而无需使用一个呼叫来解决。我加了!
来测试用例的完整性。
关闭@echo
SETLOCAL disableDelayedExpansion
设置输入=C:用空格,符号&安培;,插入符^和(括号)和%%迹象\\ python.exe \\路径%% 1%% *
组输入
集产量
SETLOCAL enableDelayedExpansion
对于%%的一个做,如果没有定义输出的endlocal&AMP(输入!);设置输出= %%一
组输出
如果我们可以依靠的事实,第一个令牌将始终用引号括起来,那么解决的办法是更容易。我们可以FOR / F使用既EOL和DELIMS设置为。
关闭@echo
SETLOCAL disableDelayedExpansion
设置输入=C:用空格,符号&安培;,插入符^和(括号)和%%迹象\\ python.exe \\路径%% 1%% *
组输入
集产量
SETLOCAL enableDelayedExpansion
(!输入)FOR / F EOL ^ = ^^ ^ delims = ^%%的一个做ENDLOCAL&安培;设置输出=%% A
组输出
不过,我只是看着我的FTYPE输出,并且发现了一些条目未报价,即使它们包含在路径中有空格!我不认为任何在此页面上的答案会处理这个问题。其实问题的背后,整个premise可能存在缺陷。
I'm struggling with improving script which I proposed as an answer to How to write a batch file showing path to executable and version of Python handling Python scripts on Windows? question. To prevent Open With dialog box I'd like to read output of ftype
command, extract path of an
executable from it and check if it exists.
After this
@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
contents is
"c:\path with spaces and (parentheses) and % signs\python.exe" "%1" %*
How do I split this to get
"c:\path with spaces, (parentheses) and % signs\python.exe"
, "%1"
and %*
?
EDIT
I tried using call
after reading Aacini's answer and it almost works. It doesn't handle %
sign, however.
@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
Output
"c:\path with spaces and (parentheses) and % signs\python.exe" "%1" %*
"c:\path with spaces and (parentheses) and 1"
An alternative parser that is very similar to the CALL parser is the simple FOR. There are two complicating factors:
1- The FOR must not be expanded while delayed expansion is enabled in case it contains !
. This is easily handled.
2- The content must not contain wildcards *
or ?
. The ?
can be temporarily substituted for and then returned. But there is no easy way to search and replace *
.
Since this problem is trying to parse out a path, and paths cannot contain wildcards, this problem is easy to solve without using a CALL. I added !
to the test case for completeness.
@echo off
setlocal disableDelayedExpansion
set input="c:\path with spaces, ampersand &, carets ^ and (parentheses)! and %% signs\python.exe" "%%1" %%*
set input
set "output="
setlocal enableDelayedExpansion
for %%A in (!input!) do if not defined output endlocal & set output=%%A
set output
If we can rely on the fact that the first token will always be enclosed in quotes, then the solution is even easier. We can use FOR /F with both EOL and DELIMS set to "
.
@echo off
setlocal disableDelayedExpansion
set input="c:\path with spaces, ampersand &, carets ^ and (parentheses)! and %% signs\python.exe" "%%1" %%*
set input
set "output="
setlocal enableDelayedExpansion
for /f eol^=^"^ delims^=^" %%A in ("!input!") do endlocal & set output="%%A"
set output
However, I just looked at my FTYPE output, and discovered some entries were not quoted, even if they contain spaces in the path! I don't think any of the answers on this page will handle this. In fact the entire premise behind the question may be flawed.
这篇关于如何分割与批处理文件空间deliminated嵌入的空格双引号字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!