I have a directory full of folders that are named in this manner:
ABC-L2-0001__2ABC12345-0101_xxxx
I need to move a lot of files that are named
You exchanged the positions of fileName and folderName. You don't take the first 8 characters from file name, but characters 13,9, and you don't look for these characters at middle of the folder name, but at the beginning. Check this fixed code:
:start
@echo off
setlocal enableDelayedExpansion
for /f "tokens=*" %%f in ('dir *.model /b') do (
set filename=%%f
set folder8=!filename:~0,9!
set "targetfolder="
for /f %%l in ('dir "?????????????!folder8!*" /a:d /b') do (
set targetfolder=%%l
)
if defined targetfolder move "!filename!" "!targetfolder!"
)
:end
You also should know that for
and for /D
plain commands are more efficient than for /F
combined with dir /B
command.
:start
@echo off
setlocal enableDelayedExpansion
for %%f in (*.model) do (
set filename=%%f
set folder9=!filename:~0,9!
set "targetfolder="
for /D %%l in ("?????????????!folder9!*") do (
set targetfolder=%%l
)
if defined targetfolder move "!filename!" "!targetfolder!"
)
:end