令牌不是由 [batch] 从变量创建的
Tokens not creating from a variable by for [batch]
我想通过使用 / 作为分隔符从变量中提取标记。
我用过这个::
set fileinput=properties\iam\self_iam_properties.xml
for /f "tokens=*delims=\" %%a in (%fileinput%) do @echo %%a
这给了我以下错误:
The system cannot find the file properties\iam\self_iam_properties.xml.
为什么要搜索文件,我只需要使用变量中存在的值。我不想从文件中获取值。
同样在解析之后,我希望创建一个名为 self_iam_parsedoutput.txt
的文件,因此我需要从输入文件名中提取 self_iam_
。如何进行?
将 %fileinput%
放在双引号中以将其视为字符串。
for /f "tokens=*delims=\" %%a in ("%fileinput%") do @echo %%a
请注意,因为您使用的是 tokens=*
,它实际上不会拆分为多个变量。如果您希望路径的每个部分都有自己的变量,您可以使用 tokens=1-26
在 for /f
中转义变量的第二种方法是 运行 输出变量的命令。
for /f %a in ('echo.%variable%')
如果您 运行遇到双引号问题,此表格很有用。
至于文件名更改,如果输入文件始终为 xyz_properties.xml
并且输出始终为 xyz_parsedoutput.txt
,那么您可以使用形式为 %variable:old=new%
[ 的简单文本替换=20=]
set inputfile=self_iam_properties.xml
set outputfile=%inputfile:properties.xml=parsedoutput.txt%
我想通过使用 / 作为分隔符从变量中提取标记。 我用过这个::
set fileinput=properties\iam\self_iam_properties.xml
for /f "tokens=*delims=\" %%a in (%fileinput%) do @echo %%a
这给了我以下错误:
The system cannot find the file properties\iam\self_iam_properties.xml.
为什么要搜索文件,我只需要使用变量中存在的值。我不想从文件中获取值。
同样在解析之后,我希望创建一个名为 self_iam_parsedoutput.txt
的文件,因此我需要从输入文件名中提取 self_iam_
。如何进行?
将 %fileinput%
放在双引号中以将其视为字符串。
for /f "tokens=*delims=\" %%a in ("%fileinput%") do @echo %%a
请注意,因为您使用的是 tokens=*
,它实际上不会拆分为多个变量。如果您希望路径的每个部分都有自己的变量,您可以使用 tokens=1-26
在 for /f
中转义变量的第二种方法是 运行 输出变量的命令。
for /f %a in ('echo.%variable%')
如果您 运行遇到双引号问题,此表格很有用。
至于文件名更改,如果输入文件始终为 xyz_properties.xml
并且输出始终为 xyz_parsedoutput.txt
,那么您可以使用形式为 %variable:old=new%
[ 的简单文本替换=20=]
set inputfile=self_iam_properties.xml
set outputfile=%inputfile:properties.xml=parsedoutput.txt%