Windows 批处理文件回显修改后的字符串

Windows Batch-file echo a modified string

我正在使用 for 循环遍历一系列子目录

For /R %1 %%f IN (*.shp) do (

我需要将文件路径 %%f 从反斜杠转换为正斜杠。

  set old=%%f
  set new=!old:\=/!
  echo !new!
  echo|(set /p="CALL spatial.importShapefileToLayer('%%~nf', '!new!');" & echo.) >> test.cypher

就像现在的代码一样,!new! text 以文字形式写入 test.cypher

  CALL spatial.importShapefileToLayer('N00E006_D100_S004_T007_L00_U0_R0', '!new!'); 

我需要写入输出的是 !new!

的值
  CALL spatial.importShapefileToLayer('N00E006_D100_S004_T007_L00_U0_R0', 'c:/test/N00E006_D100_S004_T007_L00_U0_R0.shp'); 

新!的回声!屏幕确实显示了将反斜杠更正为正斜杠,所以 !new!变量正在接受更正

关于如何正确使用 !new!输出字符串中的内容将不胜感激。

编辑:setlocal EnableDelayedExpansion
已包含在批处理文件中

比我聪明的人可能会指导您去 How does the Windows Command Interpreter (CMD.EXE) parse scripts? 解释为什么会发生这种情况,但据我所知,括号(或者可能是管道)导致事情变得以错误的顺序扩展和处理,因此 !new! 永远没有机会正确扩展。

要解决此问题,请将 ( 移至行首。

@echo off
setlocal enabledelayedexpansion
if exist test.cypher del test.cypher
for /r "%~1" %%A in (*.shp) do (
    set old=%%A
    set new=!old:\=/!
    echo !new!
    (echo|set /p="CALL spatial.importShapefileToLayer('%%~nA', '!new!');" & echo.) >> test.cypher
)