I am looping through a series of sub-directories using a for loop
For /R %1 %%f IN (*.shp) do (
I need to convert the file path %%f from a backslash to a forward slash.
set old=%%f
set new=!old:\=/!
echo !new!
echo|(set /p="CALL spatial.importShapefileToLayer('%%~nf', '!new!');" & echo.) >> test.cypher
As the code is now, the !new! text is written as a literal to the test.cypher
CALL spatial.importShapefileToLayer('N00E006_D100_S004_T007_L00_U0_R0', '!new!');
What I need to write to the output is the value of !new!
CALL spatial.importShapefileToLayer('N00E006_D100_S004_T007_L00_U0_R0', 'c:/test/N00E006_D100_S004_T007_L00_U0_R0.shp');
The echo of the !new! to the screen does show the correction of backslash to forward slash, so the !new! variable is receiving the correction
Ideas on how to correctly use the !new! contents in the output string would be appreciated.
edit : setlocal EnableDelayedExpansion
was included in the batch file
CodePudding user response:
Somebody smarter than I am will likely direct you to How does the Windows Command Interpreter (CMD.EXE) parse scripts? for an explanation about why this is happening, but as far as I can tell, the parentheses (or maybe the pipe) are causing things to get expanded and processed in the wrong order so !new!
never has a chance to get expanded properly.
To fix this, move the (
to the start of the line.
@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
)