user3343126
user3343126

Reputation: 11

Move files on set count of 300 to a new folder with a sequence name with in the same folder

I am looking for a way to sort of the files. I have a folder which contains 100000 files is JPG. I want to run a batch file on this folder which would Create a Folder for Every 300 File and store the 300 files in it.

1 Folder - 300 Files 2 Folder - 300 Files. .. 32 Folder - 300 Files.

I want to Script on execution to Create a Folder with Random Name Once it Scan thru 300 File and Put all the 300 Files in that folder.

I am not good with the complex part of batch procession , but I managed to list the files, but now I have to make a count on the files till its 300 and then make a folder and store it and go for the next count..

Please let me know if anyone can help with it.

Thank You.

Regards, Sheldon

Upvotes: 1

Views: 436

Answers (3)

Magoo
Magoo

Reputation: 80033

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: If sequential is set to a numeric, will generate sequential directories starting at #assigned.
:: If sequential is set to nothing (no characters between '=' and '"') will generate random names
SET "sequential=0"
SET "sourcedir=u:\test"
FOR /f "tokens=1*delims=:" %%a IN ('dir /b /a-d "%sourcedir%\*.jpg" ^|findstr /n /r "." ') DO (
 SET /a dest=%%a %% 300
 IF !dest!==1 CALL :newdestdir
 REM Next line for testing. Will simply display proposed move.
 ECHO MOVE "%sourcedir%\%%b" "!destdir!\"
 REM Next line for operating. Will execute proposed move if REM is edited-out
 REM MOVE "%sourcedir%\%%b" "!destdir!\" >nul
)

GOTO :EOF

:newdestdir
SET "destdir=%sourcedir%\%random%"
IF DEFINED sequential (SET /a sequential+=1&SET "destdir=%sourcedir%\%sequential%")
IF EXIST "%destdir%" GOTO newdestdir
MD "%destdir%"
GOTO :eof

You'd need to set your source directory and choose whether you want to create sequential directorynumbers or random. Either way, if a directory already exists, it will be skipped and the next directorynumber selected.

You'd need to remove the rem from the indicated line to actually perform the move after verification. The previous ECHO line could be REMmed-out or removed if you don't want a display.

Upvotes: 0

Endoro
Endoro

Reputation: 37569

The subfolders get numbered from 0...x:

@ECHO OFF &SETLOCAL disableDelayedExpansion
CD /d "Folder100000"
FOR /f "tokens=1*delims=:" %%a IN ('DIR /b /a-d ^|FINDSTR /n $') DO (
    FOR /f %%c IN ('SET /a %%a/300') DO (
        ECHO MD %%~c 2>nul
        ECHO MOVE "%%~b" %%~c
    )
)

Replace Folder100000 with your folder name and remove the echos.

Upvotes: 0

foxidrive
foxidrive

Reputation: 41234

Test this on some sample files: You can change 300 to 50 to sort into batches of 50 for example.

The folder prefix is not guaranteed to be unique but it may work ok for you.

@echo off
setlocal enabledelayedexpansion
set c=999
set prefix=%random%%random%
for %%a in (*.jpg) do (
set /a c=c+1
set /a d=c %% 300
if !d! equ 0 set "f=%prefix%-!c!"&md "!f!"
move "%%a" "!f!" >nul
)
echo moved
pause
goto :EOF

Upvotes: 1

Related Questions