[英]How to get a path variable in a text file by using FOR in a Windows batch file?
I try to retrieve a path in a text file identified by a tag. 我尝试在由标签标识的文本文件中检索路径。
I have a line like that in my text file: 我的文本文件中有这样一行:
"mypath":"C:\myfolder"
I need to retrieve the path corresponding to mypath
tag. 我需要检索与
mypath
标记相对应的路径。
I tried this: 我尝试了这个:
for /f tokens^=2^,3^ delims^=^"^:^" %%a in ('type %mytextfile%^|find """mypath"""') do (
echo %%a
echo %%b
)
But it doesn't work, the result is: 但这不起作用,结果是:
mypath
C
So my problem probably comes from the colon character that is used in the delimiter and in the string I want to get. 因此,我的问题可能来自定界符和想要获取的字符串中使用的冒号字符。
Two points: First, just use quotes for the for
options instead of escaping everything. 有两点:首先,只对
for
选项使用引号,而不要转义所有内容。 Second, you can add a *
after a tokens option to imply that no more tokenizing should be done and the rest should be returned as a single token: 其次,您可以在tokens选项之后添加
*
,以表示不再需要进行进一步的标记化,其余应作为单个标记返回:
for /f "tokens=1* delims=:" %%A in ('findstr /b /c:"""mypath""" %mytextfile%') do (
echo %%~A
echo %%~B
)
A few more things: 还有几件事:
type
when the command you're piping to can read files just fine. type
任何内容。 :-) findstr /b
matches the search string at the beginning of the line, as an added check that you've really got the correct line. findstr /b
与该行开头的搜索字符串匹配,这是对您是否确实具有正确行的补充检查。 ~
for printing the variables just eliminates the quotes around the text, so you don't have to deal with them anymore. ~
只是消除了文本周围的引号,因此您不必再处理它们了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.