Search code examples
filebatch-filedirectoryfilenamescreation

Batch based file/folder creation


I have a lot of movies in different genre folders. What I want to do through a batch file is create a folder for each movie in the same genre folder as where the movie is. For example:

/Movies/SciFi/
/Movies/SciFi/the matrix.avi
/Movies/SciFi/the matrix reloaded.mkv
/Movies/Comedy/
/Movies/Comedy/borat.avi
/Movies/Comedy/dinner for schmucks.avi

Basically I want to run a batch file from the parent folder Movies, scan for all *.mkv *.avi *.mp4 files in the genre folders and create folders based on the movie names. The name of the folders should be:

.(moviename without extension).backdrop

Ofcourse the folder shouldn't be created if it already exists. This is what I have so far:

for /f %%f in ('dir *.avi *.mkv *.mp4 /b') do md .%%~nf.backdrop

This has some disadvantages though: I have to run it from within each genre folder and if a file-name has a space in it, this command will only catch everything up till the space, rendering the aboce command pretty much useless.

Hopefully you guys can help me out.


Solution

  • No need for the /r option if you don't want to walk a hierarchy. Also, a simple if statement can avoid trying to create an already existing folder

    for /D %%D in (\Movies\*) do for %%F in ("%%~D\*.avi" "%%~D\*.mkv" "%%~D\*.mp4") do (
      if not exist "%%~D\%%~nF.backdrop" md "%%~D\%%~nF.backdrop"
    )