Search code examples
batch-filecmddos

bat file to move all files in a dir and subdir to a new folder based on many extensions


I was looking at this as reference: batch file Copy files with certain extensions from multiple directories into one directory

I have about 300k+ files contained in [sub]folders with many file types that need to be moved to folders based on their file extension. I need help forming the cmd to place files in respective folders based on extension.

Simi-Pseudo code:

for /R C:\Recovery %f.%EXT move %f C:\RecoverySorted\%EXT

The code above doesn't work properly of course. Need help revising.

If there could be error checks for non-extension files that would be great as well. I noticed a few files without extensions. Thanks!


Solution

  • @Echo OFF
    
    Set "Folder=C:\windows"
    Set "DestDir=C:\MySortedFiles"
    
    FOR /R "%Folder%" %%# in ("*") DO (
        If not exist "%DestDir%\%%~x#" (MKDIR "%DestDir%\%%~x#")
        Echo [+] Moving: "%%~nx#"
        Move "%%#" "%DestDir%\%%~x#\" 1>NUL
    )
    
    Pause&Exit
    

    Note: Don't use a last slash \ when typying the DestDir path.