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

batch-file

Solution

@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.

Problem

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

Original source