windows 根据文件名批量创建文件夹,并将多个相关文件移动到创建的文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28921039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch Create a Folder Based on Filename, and Move Multiple Related Files to The Created Folder
提问by ByteShare
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.
多年来,我总是将我所有的电影连同它们的所有附加内容都倾倒在一个文件夹中。在经历了几次挫折之后,我现在意识到保留 nfo 和 art 的备份可以节省我数小时的生命,并在添加新的 Kodi 设置(电视、平板电脑、笔记本电脑等)时保持数据库更有用。所以我试图根据电影的文件名批量创建一个文件夹,并将多个相关文件移动到创建的文件夹中。
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.
明确地说,我想为电影制作一个文件夹名称,并将所有相关文件放入该文件夹中。电影文件名的格式可能会发生变化,因为它们并不都具有相同的信息,即导演剪辑版、未剪辑版、终极版,这些都在刹车 [] 中。在所有情况下,主要电影名称都是最小的文件名,我从不希望文件夹使用文件名中包含“[Extra]”的文件命名。一般来说,非电影视频的格式是“电影(年份)[额外信息] ... [额外信息]这里的小描述”。
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
- \'71 [额外] sup stuff.mp4
- \'71.mkv
- \3 天哪!(2013)-fanart.jpg
- \3 天哪!(2013)-海报.jpg
- \3 天哪!(2013).mp4
- \3 天哪!(2013).信息
- \3 女人 (1977)-fanart.jpg
- \3 女人 (1977)-poster.jpg
- \3 女人 (1977).mp4
- \3 女人 (1977).info
- \为奴十二年 (2013).mp4
- \13 (2010).mp4
- \你的案例 (2013).avi
- \A Single Shot (2013).mp4
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA] [额外] Test.mp4
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA].avi
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA].srt
- \GBF (2013).mp4
- \GI 乔报复 (2013).mkv
- \GI 乔报复 (2013).srt
- \测试电影 (1977) [通讯].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
- \'71\'71 [Extra] sup stuff.mp4
- \'71\'71.mkv
- \3 天哪!(2013)\3 天啊!(2013)-fanart.jpg
- \3 天哪!(2013)\3 天啊!(2013)-海报.jpg
- \3 天哪!(2013)\3 天啊!(2013).mp4
- \3 天哪!(2013)\3 天啊!(2013).信息
- \3 个女人 (1977)\3 个女人 (1977)-fanart.jpg
- \3 个女人 (1977)\3 个女人 (1977)-poster.jpg
- \3 个女人 (1977)\3 个女人 (1977).mp4
- \3 个女人 (1977)\3 个女人 (1977)。
- \为奴十二年 (2013)\为奴十二年 (2013).mp4
- \13 (2010)\13 (2010).mp4
- \你的事 (2013)\你的事 (2013).avi
- \A Single Shot (2013)\A Single Shot (2013).mp4
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA]\Abre Los Ojos (1997) [睁开你的眼睛] [SPA] [Extra] Test.mp4
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA]\Abre Los Ojos (1997) [睁开你的眼睛] [SPA].avi
- \Abre Los Ojos (1997) [睁开你的眼睛] [SPA]\Abre Los Ojos (1997) [睁开你的眼睛] [SPA].srt
- \GBF (2013)\GBF (2013).mp4
- \GI 乔报复 (2013)\GI 乔报复 (2013).mkv
- \GI 乔报复 (2013)\GI 乔报复 (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].
任何人都可以帮我处理这个批处理代码吗?并非所有文件名都是 Movie (Year) [Comm] ...,有些只是 Movie 或 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.
总结:我想创建一个基于电影的文件夹,即具有特定视频扩展名的任何文件,例如 mp4、mkv、avi 等......也不包含:[Extra]; 然后将所有关联的文件移动到该文件夹中。
I've gone through these sources:
我已经通过这些来源:
- Batch create folders based on part of file name and move files into that folder
- Batch Created Folder By Filename Part and Move
- Batch create folders based on series of files and move to subdirectory
- Batch script to create folders based on filenames
- create folders based on a file name and move those files into that folder
- https://superuser.com/questions/762113/cmd-command-to-create-folder-for-each-file-and-move-file-into-folder
- Batch script that moves each file in a folder into their own folders named after the file?
- Batch command move png files to folders same name files and folders
- Batch command to check for partial filename then move
- An A-Z Index of the Windows CMD command line: http://ss64.com/nt/
- 根据文件名的一部分批量创建文件夹并将文件移动到该文件夹中
- 按文件名部分批量创建文件夹并移动
- 基于一系列文件批量创建文件夹并移动到子目录
- 基于文件名创建文件夹的批处理脚本
- 根据文件名创建文件夹并将这些文件移动到该文件夹中
- https://superuser.com/questions/762113/cmd-command-to-create-folder-for-each-file-and-move-file-into-folder
- 将文件夹中的每个文件移动到以文件命名的自己的文件夹中的批处理脚本?
- 批处理命令将png文件移动到文件夹同名文件和文件夹
- 批处理命令检查部分文件名然后移动
- Windows CMD 命令行的 AZ 索引:http: //ss64.com/nt/
回答by Aacini
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
)
)
回答by Vinoth
Just save the below code with .Bat extension in the actual source location and it will do the trick.
只需在实际源位置保存以下带有 .Bat 扩展名的代码,它就可以解决问题。
@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
pause
回答by GolezTrol
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.
这样做并不容易,因为您必须对文件名进行排序。您可以使用 指定排序顺序dir
,但它不会首先具有最短的名称。例如foo.wmv
按字母顺序在 后面foo subtitles.srt
,因为空格在点之前。
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. :)
因此,作为一种解决方法,也许您可以拥有一个包含基本电影名称的“已知扩展名”列表,然后移动与它们共享基本名称的所有文件。下面的脚本就是这样做的。它列出了所有带有avi
,mp4
和mkv
扩展名的文件,但也许您必须以 开头nfo
,这可能适用于许多电影。该脚本使用这些文件的文件名为相关文件创建目录并查找相关文件。它似乎可以解决问题,但请在使用前进行备份。:)
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 mostof the work automatically.
剧本并不完美。最大的缺点是,如果一部电影包含多个 mp4 文件,它将为每个文件创建一个文件夹。但是,如果您进行几次迭代,每次都以特定顺序使用不同的扩展(从nfo
其他类似的扩展开始,然后继续使用电影二进制扩展等),您可能至少可以自动完成大部分工作。
@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
回答by Keethesh
I think that this will work, but try on a test folder first :
我认为这会起作用,但请先尝试测试文件夹:
@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
cd ..
del %%~na
pause
edit: I changed the code, mixed syntax of batch and bash.
编辑:我更改了代码,批处理和 bash 的混合语法。