My Windows batch file is supposed to read file names and create a directory that is named according to the filename's 2nd to 5th letter:
for %%f in (.\*.txt) do (
set string=%%~nf
mkdir %string:~2,5%
)
The value of 'string' is not updated though, i.e. it is the same in each step of the loop. How can I have it updated?
This is the cmd output:
>for %f in (.\*.txt) do (
set string=%~nf
mkdir le3
)
>(
set string=file1
mkdir le3
)
>(
set string=file2
mkdir le3
)
A subdirectory or file le3 already exists.
>(
set string=file3
mkdir le3
)
A subdirectory or file le3 already exists.
delayedexpansion. Essentially, a%var%within a code block is replaced by the value ofvarwhen the block is parsed. To access the changing value within a block, you feed first to invokedelayedexpansionusing asetlocal enabledelayedexpansioncommand, and then use!var!to access the dynamic value of the variable.set !string!=%%~nfshould beset string=%%~nf- you intend to set the variablestring, not the variablecurrent content of string