Search code examples
batch-filewindows-10windows-explorer

Break a folder with many files to multiple subfolders (Windows 10)


I have a folder with 110.000 files and I want a way to break this folder into multiple subfolders containing say 3000 files each (with a batch script perhaps?). (Trying a copy/paste with WinExplorer gets stuck in "Preparing to Copy".)

For example:

BigFolder
  |
NewFolder
|     |    |    |    |
Sub1  Sub2 Sub3 Sub4 Sub5...

Solution

  • I am surprised to find the same case of mine. I had 30,000 files that needed to be sorted, so I asked question on this page: Fast methods to copy(move) files in batch file

    This is Compo's script:

    @Echo Off
    If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
    SetLocal EnableDelayedExpansion
    Set "DirN=-1"
    
    :Check_DirN
    Set/A "DirN+=1"
    If Exist "%DirN%" GoTo Check_DirN
    Set "limit=700"
    For %%A In (*.bat *.cmd *.txt) Do (
        If Not Exist "%DirN%" MD "%DirN%"
        If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
        Set/A "limit-=1"
        If !limit! Lss 0 GoTo Check_DirN
    )
    Echo(Task Done!
    Timeout -1 1>Nul
    

    And this is what I use and I edited for a bit for the purpose:

    @Echo Off
    If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
    taskkill /f /im explorer.exe >nul
    taskkill /f /im SearchIndexer.exe >nul
    sc stop WSearch >nul
    sc config WSearch start= disabled >nul
    
    SetLocal EnableDelayedExpansion
    Set "DirN=-1"
    
    :Check_DirN
    Set/A "DirN+=1"
    If Exist "%DirN%" GoTo Check_DirN
    cls
    echo Moving files to Directory %DirN%...
    Set "limit=2999"
    MD "%DirN%"
    For %%A In (*.html) Do (
        RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
        Set/A "limit-=1"
        If !limit! Lss 0 GoTo Check_DirN
    )
    Echo(Task Done!
    
    start explorer.exe
    start SearchIndexer.exe
    sc config WSearch start= delayed-auto >nul
    sc start WSearch >nul
    Timeout -1 1>Nul
    

    You can remove taskkill, start and sc part if desired. I added this part because explorer and Windows Search Indexer will cause waste of memory when moving files. I recommend you to run the script with Administrator privilege.

    Try to test the script in small scale to see if it does work.