延迟扩展字符串变量的操作失败

时间:2017-12-20 09:44:20

标签: string batch-file for-loop substring delayedvariableexpansion

我正在尝试从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

有人请帮我纠正代码,或者建议任何其他更简单的方法。

1 个答案:

答案 0 :(得分:2)

问题是执行>。解析器将<@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! 解释为重定向,因此它将失败并出现语法错误。使用引号按预期处理它(`set&#34; var = value&#34;):

<filename>c:\temp\file1</filename>
c:\temp\file1

输出是:

public void triggerZAP() throws IOException, InterruptedException, ClientApiException
{       
    String[] command = { "CMD", "/C",zapLocation + "ZAP.exe" };
    ProcessBuilder build = new ProcessBuilder(command);
    build.directory(new File(zapLocation));
    Process p = build.start();
    p.waitFor();
    Thread.sleep(5000);
    ClientApi api = new ClientApi(zapAddress, zapPort);
    currentURL = controls.getCurrentUrl();
    System.out.println("Spider : " + currentURL);
    ApiResponse resp = api.spider.scan(currentURL, null, null, null, null);
    scanId = ((ApiResponseElement) resp).getValue();
    while (true)
    {
        Thread.sleep(1000);
        progress = Integer.parseInt(((ApiResponseElement) api.spider.status(scanId)).getValue());
        System.out.println("Spider progress : " + progress + "%");
        if (progress >= 100)
        {
            break;
        }
    }
    System.out.println("Spider complete");
    System.out.println(new String(api.core.xmlreport()));

}