Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

windows - Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder

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/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
   )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...