Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
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.

share|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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .