简体   繁体   English

Java重命名一堆文件并将其移动到新文件夹

[英]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: 所以我在想:

  1. Traverse the .srt file 遍历.srt文件
  2. Get the numbers of that .srt file 获取该.srt文件的编号
  3. Search for the matching numbers for the .mp4 file 搜索.mp4文件的匹配编号
  4. Rename the .mp4 重命名.mp4
  5. Move those 2 files into the directory depending on the first number 根据第一个数字将这两个文件移动到目录中
  6. Loop back to no.1 循环回到第一

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.mp41.1 - SomeName.mp4给定name参数的SomeName值(男人,有很多name-s :)))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中将特定文件移动到新文件夹 - How to move specific files to new folder in java 如何使用Java代码将文件夹(包括子目录和文件)移动到新文件夹中 - How to move a folder(including subdirectories and files) into a new folder using Java code Java - 将所有文件从一个目标移动并重命名为另一个目标 - Java - Move and Rename all files from one destination to another 如何在java文件管理器应用程序中重命名文件和文件夹? - How to Rename files and folder in java file manager application? 重命名文件夹中的文件,而无需使用Java重命名扩展名 - Rename files in a folder with out renaming the extension using java 如何重命名 Java 中的文件夹 - How To Rename A Folder In Java 如何按月份过滤目录中的文件,zip 它们基于月份,重命名它们,将它们放在包含 zipfiles 的文件夹中? - How do I filter files in a directory by month, zip them based on month, rename them, place them in a folder that contains the zipfiles? 从Java文件夹中读取一堆JSON文件 - Read a bunch of JSON file from a folder in Java 在文件夹中编译一堆东西(Java,在Mac上) - Compiling a bunch of stuff in the folder (java, on a mac) 将按钮和文本放在新行上,并将其移动到Java屏幕的底部 - Place buttons and text on new line and move them to the bottom of the screen in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM