Batch based file/folder creation

668 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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"
)
2
On

Quotes will handle the spaces;

@echo off
for /r "c:\movies\" %%d in (.) do (
    pushd %%d
    echo now in %%d
    for %%f in (*.avi *.mkv *.mp4) do (
        md "%%~nf.backdrop"
    )
    popd 
)

This should create SOMEMOVIE.backdrop in the Category\SOMEMOVE\ dir.

If you want them in the root just md "..\%%~nf.backdrop"

(This assumes there are no other subdirs in the movie dir as its recursive so would include them)