Windows Batch File: Look for directory, if not exist, create, then move file to it

50,061

Solution 1

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "tokens=1-4delims=." %%a IN (
 'dir /b /a-d "%sourcedir%\*.*.*.mkv" '
 ) DO (
  MD "%destdir%\%%a" 2>NUL
  MOVE "%sourcedir%\%%a.%%b.%%c.%%d" "%destdir%\%%a\"
)
GOTO :EOF

This should do your moves. You'd have to change the directory names, of course - no idea where your source directory is, but destination becomes \movies in your case.

May be an idea to try ECHO MOVE first, just to make sure that the move is as-required.

The 2>nul on the MD suppresses the error messages saying the directory already exists.

Adding >nul to the end of the MOVE line will suppress the file moved message.

Solution 2

You can conditionally create the folder with:

if not exist \movies\showname mkdir \movies\showname

To move a file into it:

move ShowName.Episode.Title.mkv \movies\showname

To get more information about these commands, open a command prompt and type:

help if

and

help move
Share:
50,061
user2274612
Author by

user2274612

Updated on July 24, 2022

Comments

  • user2274612
    user2274612 over 1 year

    I am trying to create a batch file, or other script, to take the contents of one folder to a folder containing its name in another directory. For example:

    ShowName.Episode.Title.mkv should be moved to \movies\showname. if \movies\showname\ doesn't exist, the script would create it.

    There are, on average, 10-15 files at a time that would need moved.

    Any ideas?

    Thanks

  • sandyiit
    sandyiit over 7 years
    easy answer to a simple problem.