package FileRenamed;
import java.io.File;
import java.util.Scanner;
public class FileRenamed {
public static void main(String[] args) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入根目录:");
String path = sc.next();
if (path.contains("电视剧")) {
ranamedOne(path);
} else if (path.contains("照片")) {
ranamedTwo(path);
} else if (path.contains("电影")) {
ranamedThree(path);
}
}
}
public static void ranamedOne(String path) {
File file = new File(path);
File[] fileList = file.listFiles();
int i = 0, number = 0;
for (File files : fileList) {
// 获取文件的后缀名(.xxx)
String ext = files.getName().substring(files.getName().lastIndexOf("."));
// 获取文件中的数字,就是哪一集的数字,进行替换
if (files.getName().contains("第") && files.getName().contains("集")) {
number = Integer.parseInt(
files.getName().substring(files.getName().lastIndexOf("第") + 1, files.getName().indexOf("集")));
} // 文件中的名字
String sname = file.getName();
// 组合新的文件名
StringBuffer sbf = new StringBuffer();
if (number != 0) {
sbf.append(path + "/" + sname + " 第" + number + "集" + ext);
} else {
sbf.append(path + "/" + sname + ext);
}
// 进行重命名
boolean b = files.renameTo(new File(sbf.toString()));
if (b == true) {
i++;
}
if (i == fileList.length) {
System.out.println("该文件夹下面的文件已经全部重命名完成");
}
}
}
public static void ranamedTwo(String path) {
File dir = new File(path);
File[] files = dir.listFiles();
int x = 0;
for (int i = 0; i < files.length; i++) {
String ext = files[i].getName().substring(files[i].getName().lastIndexOf("."));
StringBuffer sbf = new StringBuffer(path + "/" + (i + 1) + ext);
boolean b = files[i].renameTo(new File(sbf.toString()));
if (b == true) {
x++;
}
if (x == files.length) {
System.out.println("该文件夹下面的文件已经全部重命名完成");
}
}
}
public static void ranamedThree(String path) {
File file = new File(path);
File[] files = file.listFiles();
int x = 0;
for (int i = 0; i < files.length; i++) {
String sname = files[i].getName().substring(0, files[i].getName().indexOf("."));
if (files[i].getName().contains("-")) {
sname = files[i].getName().substring(0, files[i].getName().indexOf("-"));
}
String ext = files[i].getName().substring(files[i].getName().lastIndexOf("."));
StringBuffer sbf = new StringBuffer(path + "/" + sname + ext);
boolean b = files[i].renameTo(new File(sbf.toString()));
if (b == true) {
x++;
}
if (x == files.length) {
System.out.println("该文件夹下面的文件已经全部重命名完成");
}
}
}
}