0

我正在尝试从 XML 文件中查找特定行并使用字符串操作操作提取值。

下面是我正在尝试的代码。

@echo off
setlocal enabledelayedexpansion

::Expected line is "<filename>c:\temp\file1</filename>"

for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
    SET LINE=%%i
)

echo !LINE!

SET FILENAME=!LINE:<filename>=!
SET FILENAME=%FILENAME:</filename>=%
ECHO !FILENAME!

输出是:

<filename>c:\temp\file1</filename>
The system cannot find the file specified.
ECHO is off.

我实际上想要这个值c:\temp\file1

有人请帮我更正代码或请提出任何其他更简单的方法。

4

1 回答 1

2

问题是执行set。解析器将>and解释<为重定向,因此它将因语法错误而失败。使用引号按预期处理它(`set "var=value"):

@echo off
setlocal enabledelayedexpansion
REM echo ^<filename^>c:\temp\file1^</filename^>>file1.props

::Expected line is "<filename>c:\temp\file1</filename>"

for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
    SET "LINE=%%i"
)

echo !LINE!

SET "FILENAME=!LINE:<filename>=!"
SET "FILENAME=%FILENAME:</filename>=%"
ECHO !FILENAME!

输出是:

<filename>c:\temp\file1</filename>
c:\temp\file1
于 2017-12-20T09:52:21.470 回答