I am trying to extract a substring in a for
loop witin a batch script, but I'm missing something, as it will be delayed string inside the loop.
Below is the code: Please help how to do this:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
FOR /F "tokens=1* delims=" %%A IN ('dir /b /a-d') DO (
echo %%A
set directory=%%A
:: am calculating length here based on some logic, lets assume it as 2
set length=2
set myvar = !directory:~0:length!
echo !myvar!
)
endlocal
pause
Output now
1.txt
ECHO is off.
2.txt
ECHO is off.
demo.cmd
ECHO is off.
Press any key to continue . . .
Expected output:
1.txt
1.
2.txt
2.
demo.cmd
de.
Press any key to continue . . .