curiousdboy
curiousdboy

Reputation: 11

Sorting files into folders, according to a part of filename using Windows Batch file

I have around 12,000 .jpg files in a data-set folder in G:\train with names such as

0002_c1s1_000451_03.jpg, 0002_c1s1_000551_01.jpg...

up to

...1500_c6s3_086542_02.jpg , 1500_c6s3_086567_01.jpg

I want to move them to new folders with their initial filename such as 0002, 0005, 0007,... 1496, 1500

What I need is a Windows batch file to create new folders & move files quickly without a hassle. I've tried few other answers to no avail.

Upvotes: 0

Views: 580

Answers (2)

numdig
numdig

Reputation: 121

Something like this should do, as a .cmd/.bat script file:

@rem The DEBUG_RUN variable enables a step-by-step mode, it will cause the 
@rem script to pause after processing every file. Remove the DEBUG_RUN (and 
@rem the pause instruction) when you are confident it does what you want,
@rem restart the script and enjoy :-)

:main
@setlocal
    @set DEBUG_RUN=1
    @pushd "G:\train"
        @for %%f in (*.jpg) do @(
            call :mvToSubDir "%%~nxf"
            if defined DEBUG_RUN pause
        )
    @popd
    @pause
@endlocal
@goto:eof

:mvToSubDir
    @set fn=%~1
    @set dn=%fn:~0,4%
    @if not exist "%dn%" mkdir "%dn%"
    move "%fn%" "%dn%"
@goto:eof

Upvotes: 1

BlockWizard
BlockWizard

Reputation: 1

you can try a series of for loops to accomplish this

for /l %%f in (1,1,12000) do ( mkdir %%f for %%d in (c:\sourcedir) do ( move %%d %%f ) )

i didn't fully understand how you wanted to sort them but i hope this helps. You can use the "for /?" command and feel free to ask for more help if this isn't enough. I wish you luck with your endeavors.

Upvotes: 0

Related Questions