a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.basic.security.utils;
 
import java.io.File;
 
public class RenameFiles {
    static String dirName = "F:\\ttorial.com_sorted\\linux\\[Ttorial.Com] Advanced Linux_ The Linux Kernel";
    static String[] videoExtensions = {
            ".flv",
            ".mp4",
            ".mov",
            ".wmv",
            ".flv"
    };
 
    public static void main(String[] args) {
        if (!dirName.endsWith("\\")) {
            dirName += "\\";
        }
        File file = new File(dirName);
        if (!file.exists()) {
            file.mkdir();
        }
        listSubDir();
    }
 
    private static void listSubDir() {
        File dir = new File(dirName);
        File[] subDirs = dir.listFiles();
        for (File subDir : subDirs) {
//            System.out.println(subDir.getAbsolutePath());
            if (subDir.isFile()) {
                String prefix = getPrefix(subDir);
                if (isVideoFile(subDir)) {
                    rename(prefix, subDir);
                }
            }
            if (subDir.isDirectory()) {
                listFileInSubDir(subDir);
            }
        }
    }
 
    private static void listFileInSubDir(File subDir) {
        File files[] = subDir.listFiles();
        String prefix = getPrefix(subDir);
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (isVideoFile(file)) {
                rename(prefix, file);
            }
        }
    }
 
    private static String getPrefix(File subDir) {
        String dirName = subDir.getName();
        if (dirName.length() > 3) {
            dirName = dirName.substring(0, 3);
        }
        return dirName = dirName.replaceAll("\\D+", "");
    }
 
    private static void rename(String prefix, File file) {
        String prefixPattern = prefix + "_";
        if (file.getName().contains("\\" + prefixPattern)) {
            return;
        }
//        prefixPattern = "";
        String newFileName = dirName + prefixPattern + file.getName();
        System.out.println(file.getAbsolutePath() + "---" + newFileName);
        file.renameTo(new File(newFileName));
    }
 
    private static boolean isVideoFile(File file) {
        if (file.isDirectory()) {
            return false;
        }
        String fileName = file.getName();
        for (String extension : videoExtensions) {
            if (fileName.endsWith(extension)) {
                return true;
            }
        }
        return false;
    }
}