9

For years I've always had all my movies dumped into one folder, along with all of their extras. I've realized now after several frustrations keeping a backup of nfo and art would save me hours of life, and keep the database more usable when adding a new Kodi setup (TV, Tablet, Laptop, etc...). So I'm trying to batch create a folder based on the filename of the movie, and move multiple related files to the created folder.

To be clear, I want to make a folder name of the movie and have all the related files go into that folder. The movie filesnames' format can change because they don't all have the same info, i.e. Director's cut, unedit, ultimate edition, which are in brakets []. The main movie name would in all cases be the smallest filename, and I'd never want the folder to be named with a file that contained "[Extra]" in the filename. Generally speaking the format is "Movie (year) [extra info] ... [extra info] small description here" for the non-movie videos.

The best I've come up with is:

for %%i in (*) do md "%%~ni" && move "%%~i" "%%~ni"

The problem right now is I'm creating a folder for every file and moving all the files to the respective folders but related content is now in different folders and things with the exact same filename is getting left in the main folder.

An example of what I'm trying to accomplish:

  • \'71 [Extra] sup stuff.mp4
  • \'71.mkv
  • \3 Geezers! (2013)-fanart.jpg
  • \3 Geezers! (2013)-poster.jpg
  • \3 Geezers! (2013).mp4
  • \3 Geezers! (2013).nfo
  • \3 Women (1977)-fanart.jpg
  • \3 Women (1977)-poster.jpg
  • \3 Women (1977).mp4
  • \3 Women (1977).nfo
  • \12 Years A Slave (2013).mp4
  • \13 (2010).mp4
  • \A Case Of You (2013).avi
  • \A Single Shot (2013).mp4
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA] [Extra] Test.mp4
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA].avi
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA].srt
  • \G.B.F. (2013).mp4
  • \G.I. Joe Retaliation (2013).mkv
  • \G.I. Joe Retaliation (2013).srt
  • \Test Movie (1977) [Comm].mp4

To:

  • \'71\'71 [Extra] sup stuff.mp4
  • \'71\'71.mkv
  • \3 Geezers! (2013)\3 Geezers! (2013)-fanart.jpg
  • \3 Geezers! (2013)\3 Geezers! (2013)-poster.jpg
  • \3 Geezers! (2013)\3 Geezers! (2013).mp4
  • \3 Geezers! (2013)\3 Geezers! (2013).nfo
  • \3 Women (1977)\3 Women (1977)-fanart.jpg
  • \3 Women (1977)\3 Women (1977)-poster.jpg
  • \3 Women (1977)\3 Women (1977).mp4
  • \3 Women (1977)\3 Women (1977).nfo
  • \12 Years A Slave (2013)\12 Years A Slave (2013).mp4
  • \13 (2010)\13 (2010).mp4
  • \A Case Of You (2013)\A Case Of You (2013).avi
  • \A Single Shot (2013)\A Single Shot (2013).mp4
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA]\Abre Los Ojos (1997) [Open Your Eyes] [SPA] [Extra] Test.mp4
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA]\Abre Los Ojos (1997) [Open Your Eyes] [SPA].avi
  • \Abre Los Ojos (1997) [Open Your Eyes] [SPA]\Abre Los Ojos (1997) [Open Your Eyes] [SPA].srt
  • \G.B.F. (2013)\G.B.F. (2013).mp4
  • \G.I. Joe Retaliation (2013)\G.I. Joe Retaliation (2013).mkv
  • \G.I. Joe Retaliation (2013)\G.I. Joe Retaliation (2013).srt
  • \Test Movie (1977) [Comm]\Test Movie (1977) [Comm].mp4

Can anyone help me with this batch code? Not all the files names are Movie (Year) [Comm] ..., some are just Movie, or Movie [Comm].

In summary: I want to create a folder based on a movie, ie any file with a specific video extension, such as mp4, mkv, avi, etc... that also doesn't contain: [Extra]; then move all associated files into that folder.

I've gone through these sources:

  1. Batch create folders based on part of file name and move files into that folder
  2. Batch Created Folder By Filename Part and Move
  3. Batch create folders based on series of files and move to subdirectory
  4. Batch script to create folders based on filenames
  5. create folders based on a file name and move those files into that folder
  6. https://superuser.com/questions/762113/cmd-command-to-create-folder-for-each-file-and-move-file-into-folder
  7. Batch script that moves each file in a folder into their own folders named after the file?
  8. Batch command move png files to folders same name files and folders
  9. Batch command to check for partial filename then move
  10. An A-Z Index of the Windows CMD command line: http://ss64.com/nt/
Community
  • 1
  • 1
ByteShare
  • 99
  • 1
  • 1
  • 6
  • Are all of your filenames in that format? – Millie Smith Mar 07 '15 at 23:00
  • Easiest answer is no. Not all of the files have commentary, and some have other info such as director's cut and/or unedited. I'll update my question to make it more clear, but I suppose the easiest would be (for me) to have a folder made with the main movie name, which would in all cases be the smallest filename and everything get put in that folder. Generally speaking the format is "Movie (year) [extra info] ... [extra info] small description if necessary". – ByteShare Mar 07 '15 at 23:22

3 Answers3

10

Although your description is ample, you forgot to give some specific details; for example: starts all file names with the "Movie (year)" part? If so, then this Batch file do what you want:

EDIT: Ops, I misread the requirements! This version works correctly:

@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
   set "filename=%%a"
   setlocal EnableDelayedExpansion
   for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
      set "basename=!filename!"
      md "!basename!"
   )
   move "!filename!.%%b" "!basename!"
   for /F "delims=" %%c in ("!basename!") do (
      endlocal
      set "basename=%%c
   )
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Update: Yup, it works for everything I've thrown at it besides files without a year in them. It strips out the extra info, which isn't a deal breaker for me, ie [Comm] or [Rifftrax] – ByteShare Mar 08 '15 at 01:03
  • Check the modification... What do you want to do if the shortest name include `[Extra]` in it? – Aacini Mar 08 '15 at 01:08
  • That picked up my test movie without a year and seemed to do a really good job, but 3 Women (1977), was made into a folder and moved, and 3 Geezers! (2013) never had a folder made for it, nor was moved. – ByteShare Mar 08 '15 at 01:13
  • Plase, post a complete list of the files that cause problems and the required result; modify the original question (I suggest you to eliminate the list of links to other answers...) – Aacini Mar 08 '15 at 01:16
  • Ops! I just fixed a small bug... Please, copy again the program and test it `:-)` – Aacini Mar 08 '15 at 01:20
  • Please, DON'T post names here, in comments! Modify your question and enclose the list of names in code tags – Aacini Mar 08 '15 at 01:23
  • Tested your newer code 3 Geezers had a folder created but the files weren't. The only hickup was Abre Los Ojos (1997) [Extra] Test had its own folder made, otherwise everything else looked good. Updated the examples in my question. – ByteShare Mar 08 '15 at 01:38
  • The problem of filenames with exclamation marks (like "3 Geezers! (2013)") has been solved in the last mod (please, test it). The problem of avoid "[Extra]" in the base name is _much_ more complex, because in this case there are not enough data to include this file in the proper directory... – Aacini Mar 08 '15 at 01:44
  • This seemed to fix almost all the problems. I then applied it to a much larger database of files. The problem files were those with periods, such as Dr. or G.B.F., or ... The only other files where those with non-English Characters, such as Ancient Species 紅穀子 but that last isn't really an issue for me as it is like maybe 2-10 files. Correction any any movie that starts with A (Space) and a name are all matched together. Updating example list. – ByteShare Mar 08 '15 at 01:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72502/discussion-between-byteshare-and-aacini). – ByteShare Mar 08 '15 at 06:43
  • Work like a charm! Thank you sir for this beautiful peace of art! – ChainList Mar 14 '17 at 20:26
5

Just save the below code with .Bat extension in the actual source location and it will do the trick.

@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
pause
Vinoth
  • 51
  • 1
  • 2
1

It's not easy to do this, because you'd have to sort the file names. You can specify a sort order using dir, but it won't have the shortest names first. For instance foo.wmv is alphabetically behind foo subtitles.srt, because space comes before dot.

So as a work-around, maybe you could have a list of 'known extensions' which contain the basic movie names, and then move all the files that share the base name with them. The following script does just that. It lists all files with avi, mp4 and mkv extensions, but maybe you would have to start with nfo, which may be available for many movies. The script uses the file names of those files to make directories for and find related files. it seems to do the trick, but please make a backup before you use it. :)

The assumption in this script is that the file with the known extension also has the shortest name. If there are other files that have shorter names (or otherwise not matching) then they won't be moved to that folder.

The script isn't perfect. The biggest disadvantage is that if one movie contains multiple mp4 files, it will create a folder for each of them. But if you do a couple of iterations, each time using different extensions in a specific order (start with nfo and other similar extensions, then move on the movie binary extensions, etc) you might be able to do at least most of the work automatically.

@echo off
setlocal

REM store current directory. Using separate variable makes it 
REM easier to change behaviour too.
set dir=%cd%

REM call subroutine for each supported extension.
call :do .info
call :do .mp4
call :do .avi
call :do .mkv

REM Main program done.
echo Press a key to close.
pause
exit /b

:do
set ext=%1
REM loop through all files with the given extension.
for /f "tokens=*" %%f in ('dir /b /on "%dir%\*%ext%"') do (
  REM trim the extension and use the base name as directory name.
  setlocal EnableDelayedExpansion
  set thefile=%%~nf
  echo !thefile!
  md "%dir%\!thefile!"
  REM move all files that start with the same base name.
  move "%dir%\!thefile!*.*" "%dir%\!thefile!\"
)
REM exit subroutine
exit /b
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • thank you first. I tried your script on a few test files and it said" The syntax of the command is incorrect" for the file: 3 Geezers! (2013).mp4 I haven't changed the script at all. But to you point, yes, the foldername could come from only the files with specific extensions, ie mkv, mp4, avi, etc.. I was thinking it over and to simply things It would be any file with a video extension that doesn't contain "[Extra]". Oh, and it created 5 folders with two movies, movies: 3 Geezers! (2013) & 3 Women (1977) Folders: (1977), (2013), 3, Geezers, Women – ByteShare Mar 08 '15 at 00:37
  • The error you got is probably due to the spaces in the name. I tested with some dummy names that didn't have spaces. I added some extra quotes in the subroutine which should take care of this. You can find the updated script in the question. I think this should solve the other issue (creating those weird folders) too. – GolezTrol Mar 08 '15 at 01:03
  • That seemed to have similar issues with extra directories. Updated my example with the files I'm using to test with – ByteShare Mar 08 '15 at 01:29