Search code examples
batch-filedos

Move files to folder name contained in the file name


I am trying to move files that has partial data in its name to new folder of that string in the filename!

myfiles[sql-data-2232]sql.dat  [want to move it and create folder sql-data-2232]
myfiles[jpg-data-2232]jpg.img  [want to move it and create folder jpg-data-2232]
myfiles[whatever-number]xyz.xyz [want to move it and create folder whatever-number]

brackets in the file name contains the folder name which the file to be moved to!


Solution

  • Test this on a folder of sample files.

    @echo off
    for /f "delims=" %%a in ('dir "myfiles*" /b /a-d ') do (
       for /f "tokens=2 delims=[]" %%b in ("%%a") do MD "%%b" 2>nul & move "%%a" "%%b" >nul
    )
    echo done
    pause