[英]java rename bunch of files and move them to a new folder
So here's the case. 这样的话。
I have one root folder that contains more folders and each of that folders have bunch of files. 我有一个包含更多文件夹的根文件夹,并且每个文件夹都有一堆文件。 Those files are formatted '.srt' and '.mp4'.
这些文件的格式为“ .srt”和“ .mp4”。 Because of some particular reasons I added numbers at the end of the file when I started downloading those files from the server.
由于某些特殊原因,当我开始从服务器下载那些文件时,我在文件末尾添加了数字。 For example, 'Intro.srt - 1.1' and 'blabla720.mp4 - 1.1'.
例如,“ Intro.srt-1.1”和“ blabla720.mp4-1.1”。 And the numbers are increasing, 1.2, 1.3, 2.1, 2.2, 3.1, 3.2 etc.
并且数量在增加,分别为1.2、1.3、2.1、2.2、3.1、3.2等。
First I want to do is to move the numbers to the front like '1.1 - Intro.srt'. 首先,我想将数字移到最前面,例如“ 1.1-Intro.srt”。 I already manage how to do is by shifting the last characters to the right.
我已经设法将最后一个字符向右移动。
Now I want to rename these files to match the .srt with .mp4 and move them to a new folder. 现在,我想重命名这些文件以将.srt与.mp4匹配,然后将它们移动到新文件夹中。 For example: I want to rename '1.1 - blabla720.mp4' into the matching numbers of the .srt file which in this case is '1.1 - Intro.srt' so later on it will be '1.1 - Intro.mp4'.
例如:我想将“ 1.1-blabla720.mp4”重命名为.srt文件的匹配编号,在本例中为“ 1.1-Intro.srt”,因此以后将为“ 1.1-Intro.mp4”。 Then move them to a new folder 'Stage 1'.
然后将它们移动到新文件夹“阶段1”。 And for the files that starts with the number 2 will be moved to a new dir 'Stage 2' etc.
对于以数字2开头的文件,将被移动到新目录“ Stage 2”等。
I want to do this in Java and when I run the java file it will rename and move all the files inside that root dir. 我想在Java中执行此操作,当我运行Java文件时,它将重命名并移动该根目录中的所有文件。 (I already know how to recursively trace the entire folders).
(我已经知道如何递归地跟踪整个文件夹)。
So I was thinking: 所以我在想:
How do I approach this, should I use regex? 我该如何使用正则表达式?
I think that you should focus first on allocating the right .srt file to the right .mp4 file. 我认为您应该首先关注将正确的.srt文件分配给正确的.mp4文件。 regex won't help you there.
regex不会帮助您。
How about something simple like this (untested code): 像这样的简单代码(未经测试的代码):
//get all files into a single array
File[] allFiles = getAListOfAllFilesRecursively("rootFolder");
//sort the files by their path (File is Comparable)
Arrays.sort(allFiles);
for (int x = 0; x < allFiles.length(); x++) {
//if x ends with .srt, rename x+1 (the .mp4) based on the name we parse from x
//otherwise, rename x (the .mp4) based on the name we parse from x+1
if (allFiles[x].getName().endsWith(".srt")) {
String name = getNameFromFileName(allFiles[x].getName());
renameFileToName(allFiles[x+1], name);
} else {
String name = getNameFromFileName(allFiles[x+1].getName());
renameFileToName(allFiles[x], name);
}
}
You will probably want to add some checks here and there (you may want to verify the the number prefixes indeed match before renaming, in case you have a .srt file without a matching .mp4). 您可能需要在这里和那里添加一些检查(如果您有一个.srt文件没有匹配的.mp4,则可能需要在重命名之前验证数字前缀是否确实匹配)。
getAListOfAllFilesRecursively(String rootFolder)
needs to return an array of File objects you retrieve recursively like you mentioned. getAListOfAllFilesRecursively(String rootFolder)
需要返回一个递归检索的File对象数组,就像您提到的那样。 getNameFromFileName(File file)
needs to parse 1.1 - SomeName.srt
into SomeName
. getNameFromFileName(File file)
需要将1.1 - SomeName.srt
解析为SomeName
。 renameFileToName(File file, String name)
needs to rename the passed in file with a filename like 1.1 - abcd.mp4
to 1.1 - SomeName.mp4
given a value of SomeName
for the name
parameter (man, there are a lot of name-s :))). renameFileToName(File file, String name)
需要将传入文件的文件名重命名为1.1 - abcd.mp4
到1.1 - SomeName.mp4
给定name
参数的SomeName
值(男人,有很多name-s :)))。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.