user2274612
user2274612

Reputation: 95

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

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

Upvotes: 9

Views: 53472

Answers (2)

Magoo
Magoo

Reputation: 80211

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

Upvotes: 5

Ferruccio
Ferruccio

Reputation: 100748

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

Upvotes: 10

Related Questions