Search code examples
filebatch-filemovesubdirectory

Move multiple files inside the folder to the sub folder inside that folder


Trying to find a way to move files within a directory into the subdirectory contained within it.

For example:

January
|   001.jpg
|   002.jpg
|   ...
|   100.jpg
|
\---Photo
February
|   001.jpg
|   002.jpg
|   ...
|   100.jpg
|
\---Photo

And March, April etc.

I've been doing it manually, but it can be time consuming.

Much appreciated all.


Solution

  • @ECHO OFF
    SETLOCAL
    rem The following setting for the directory is a name
    rem that I use for testing and deliberately includes spaces to make sure
    rem that the process works using such names. These will need to be changed to suit your situation.
    
    SET "sourcedir=u:\your files"
    
    REM (
    FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*" ') DO (
     FOR /f "tokens=2delims=/-." %%m IN ("%%~tb") DO (
     FOR %%c IN ("01,January" "02,February" "03,March" "04,April" "05,May" "06,June" "07,July" 
                 "08,August" "09,September" "10,October" "11,November" "12,December") DO (
      FOR /f "tokens=1,2delims=," %%u IN (%%c) DO IF %%u==%%m (
       MD "%sourcedir%\%%v" 2>NUL
       IF EXIST "%sourcedir%\%%v\%%b" (
        ECHO failed to MOVE %%b
       ) ELSE (
        ECHO MOVE "%sourcedir%\%%b" "%sourcedir%\%%v\"
       )
      )
     )
     )
    )
    GOTO :EOF
    

    Always verify against a test directory before applying to real data.

    The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

    You may need to adjust FOR /f "tokens=2delims=/-." This is correct for MY date format (dd/mm/yyyy) but if you use mm/dd/yy, then you'd need tokens=1.

    If you use 1-digit months, then change eg., "03,March" to "3,March"