简体   繁体   English

重命名Java文件夹中的所有文件

[英]Rename All Files In Folders Java

I have one folder which contains many sub-folders. 我有一个包含许多子文件夹的文件夹。 To make it more clear here is an example of the folders: 为了更加清楚,这里是文件夹的示例:

Movies: MovieTitle: Moviefile.mp4 (Movie File) MovieSubtitles.srt (Subtitles) MovieSeries: MovieTitle: Moviefile.mp4 MovieSubtitles.srt I need to rename each mp4 and srt file to the following "MovieTitle". Movies: MovieTitle: Moviefile.mp4 (Movie File) MovieSubtitles.srt (Subtitles) MovieSeries: MovieTitle: Moviefile.mp4 MovieSubtitles.srt我需要将每个mp4和srt文件重命名为以下“ MovieTitle”。 If the movie is part of a series it should be named to Series "Title + Movie Title". 如果电影是系列的一部分,则应将其命名为“标题+电影标题”。 Lets use Star Wars as an example for series and how to name. 让我们以《星球大战》作为系列以及命名方法的示例。 "Star Wars" would be the name of a directory in "Movies". “星球大战”将是“电影”中目录的名称。 In "Star Wars" are 6 Folders each with a mp4 and srt file in it. 在“星球大战”中,有6个文件夹,每个文件夹中都带有mp4和srt文件。 For episode 1 of star wars the mp4 and srt file should be renamed to: "Star Wars - Episode 1.mp4" and "Star Wars - Episode 1.srt". 对于《星球大战》第1集,mp4和srt文件应重命名为:“《星球大战-第1集》 mp4”和“《星球大战-第1集》 srt”。 If Episode 1 was not part of series it should be named to just "Episode 1.mp4" 如果第1集不是该系列的一部分,则应将其命名为“ Episode 1.mp4”

Here is the code that I have come up with so far: 这是到目前为止我提出的代码:

    public static void renaming(File[] files){
    String dir1, dir2;
    for(File movie: files){ //Main folder containing all of the movies.
        dir1 = movie.getName();
        for(File filesInMovie: movie.listFiles()){
            if(filesInMovie.isDirectory()){ //This means that it is a series.
                dir2 = filesInMovie.getName();
                for(File i: filesInMovie.listFiles()){
                    i.renameTo(dir1 + " - " + dir2);
                }
            }else{
                filesInMovie.renameTo(dir1)
            }               
        }

    }
}

I realize that renameTo is an actual function in Java. 我意识到renameTo是Java中的实际功能。 I thought it would rename files until I read what it actually does (which I am still a little fuzzy on). 我以为它会重命名文件,直到我读懂它的实际作用为止(对此我还有些模糊)。 So my main question is how would I get this code to properly rename the files. 所以我的主要问题是如何获得此代码以正确地重命名文件。

Just some extra things you should know: One Directory Contains all of the movies. 您应该了解的一些其他事项:一个目录包含所有电影。 There are possibilities for each folder in the movies folder It has other folders in it (It is a series) It has a mp4 and srt file in it If you have any questions please ask!!! 电影文件夹中的每个文件夹都有可能。它有一个其他文件夹(这是一个系列)。其中有mp4和srt文件。如果您有任何疑问,请询问!!!

Using Guava and Java 7, here is what you can do: 使用Guava和Java 7,您可以执行以下操作:

public final class Renamer
{
    private static final Joiner JOINER = Joiner.on(" - ");

    private static final class RenamerVisitor
        extends SimpleFileVisitor<Path>
    {
        private final Path baseDir;

        private RenamerVisitor(final Path baseDir)
        {
            this.baseDir = baseDir;
        }

        @Override
        public FileVisitResult visitFile(final Path file,
            final BasicFileAttributes attrs)
            throws IOException
        {
            final Path relpath = baseDir.relativize(file);
            final String targetName
                = JOINER.join(Iterables.transform(relpath,
                Functions.toStringFunction()));
            final Path dstPath = baseDir.resolve(targetName);
            System.out.printf("'%s' -> '%s'\n", file, dstPath);
            return FileVisitResult.CONTINUE;
        }
    }

    public static void main(final String... args)
        throws IOException
    {
        final Path baseDir = Paths.get("/home/fge/tmp/jsr203/docs");
        Files.walkFileTree(baseDir, new RenamerVisitor(baseDir));
    }
}

This is for my environment in a directory. 这是针对我的环境的目录。 Sample output: 样本输出:

[...]
'/home/fge/tmp/jsr203/docs/java/io/class-use/File.html' -> '/home/fge/tmp/jsr203/docs/java - io - class-use - File.html'
'/home/fge/tmp/jsr203/docs/java/io/class-use/FilePermission.html' -> '/home/fge/tmp/jsr203/docs/java - io - class-use - FilePermission.html'
'/home/fge/tmp/jsr203/docs/java/io/File.html' -> '/home/fge/tmp/jsr203/docs/java - io - File.html'

In order to make it use the "real thing", replace the System.out.println() in visitFile with Files.move() and you're done! 为了使它使用“真实的东西”,用Files.move()替换visitFileSystem.out.println() visitFile

Of course, you can also choose to copy instead, or even create a (hard) link if your filesystem supports it . 当然,您也可以选择复制, 或者在文件系统支持的情况下甚至创建(硬)链接

Side note: unlike File 's .renameTo() , Files.move() does not silently fail! 旁注:与File.renameTo()Files.move()不会默默失败!

(and after that, you should drop Java 6's old file API fast) (然后,您应该快速删除Java 6的旧文件API)

File#getName() returns a String. File#getName()返回一个字符串。 You are using this to get the name of the current file and then attempting to rename the file using File#rename(String) where the method is actually defined as File#renameTo(File) . 您正在使用它来获取当前文件的名称,然后尝试使用File#rename(String)重命名该文件,其中该方法实际上定义为File#renameTo(File) That is it the argument is expected to be a file to where you are attempting to rename the file. 就是说,该参数应该是您要重命名该文件的文件。

Some Suggestions: 一些建议:

  1. Look at Files.move() use this to move the file to target - Link has example of this 查看Files.move()使用Files.move()文件移动到目标-链接有此示例

  2. Don't use the get name method for the first directory - use the file directly (Something like below but still not correct I think) and this is if you want the file in the same base directory movies 不要对第一个目录使用get name方法-直接使用文件(如下所示,但我认为仍然不正确),这是如果您希望将文件放在相同的基本目录movies

     i.renameTo(new File(movie," - " + filesInMovie.getName() + ext); 

    Still needs work for extension of file ext however which will come from the file i 仍然需要扩展文件ext工作,但这将来自文件i

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM