I have some files

07-02.jpg until

37-03.jpg until37-48.jpg

I want to create folder

07, 08.... ,37

and then move multiple files

07-01.jpg, 07-02.jpg, 07-03.jpg into folder 07
08-01.jpg, 08-02.jpg, 08-03.jpg into folder 08
37-01.jpg, 37-02.jpg, 37-03.jpg into folder 37

how would a batch script look like to acchieve this?


  • It is unlikely that a batch script exists that does exactly this, so you would need to write one. – Paul Sep 25 '13 at 4:36

No batch file needed:

for %F in (*-*.jpg) do @for /f "delims=-" %A in ("%F") do @md "%A" 2>nul&move /y "%F" "%A" >nul

Double up the percents if used within a batch script.

| improve this answer | |

This should work:

@echo off
setlocal enableextensions enabledelayedexpansion
rem parameter 1: first prefix (e.g 7)
rem parameter 2: last prefix (e.g 37)
rem parameter 3: max counter (e.g. 48)
FOR /L %%A IN (%1,1,%2) DO (
    rem arrange the padding
    set pre=0%%A
    set pre=!pre:~-2!
    rem create the folder
    mkdir !pre!
    FOR /L %%B IN (1,1,%3) DO (
        rem arrange the padding
        set num=0%%B
        set num=!num:~-2!
        rem file name
        set curr_file=!pre!-!num!.jpg
        rem ECHO !curr_file!
        move !curr_file! !pre!

Save in a batch file (e.g. store_files.bat) and invoke it with proper params:

store_files.bat 7 37 48


Edit: the solution of dbenham is more elegant.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.