批量遍历删除或重命名指定目录下的指定文件--方便用的时候直接copy

批量遍历删除或重命名指定目录下的指定文件--方便用的时候直接copy

本文连接:https://www.cnblogs.com/muphy/p/15620563.html

文件很多的时候,不可能手动一个个操作,记住工具能快速解决 

 

批量遍历删除 DeleteFile.java

javac DeleteFile.java -encoding utf-8
java DeleteFile "D:\\BaiduNetdiskDownload\\录播视频及资料" "录播视频"
//package me.muphy;

import java.io.File;
import java.nio.file.Files;

/**
 * 遍历删除指定文件
 *
 * @className: DeleteFile
 * @author: 若非
 * @date: 2021-11-29 16:57
 */
public class DeleteFile {
    static String fileName = "azi";
    static String pathname = "E:\\workspace\\tt";

    public static void main(String[] args) {
        if (args.length > 1) {
            pathname = args[0];
            fileName = args[1];
        }
        if (fileName == null || fileName.trim().isEmpty()) {
            System.out.println("删除文件名不能为空!");
            return;
        }
        System.out.println("开始删除文件!");
        deleteFile();
        System.out.println("删除文件完成!");
    }

    private static void deleteFile() {
        File file = new File(pathname);
        if (!file.exists()) {
            System.out.println("指定路径不存在!");
            return;
        }
        if (!file.isDirectory()) {
            System.out.println("指定路径不是文件夹路径!");
            return;
        }
        deleteFile(file);
    }

    // 深度优先
    private static void deleteFile(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.getName().equals(fileName)) {
            doDeleteFile(file);
            return;
        }
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                deleteFile(listFile);
            }
        }
    }

    private static void doDeleteFile(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                doDeleteFile(listFile);
            }
        }
        boolean delete = false;
        try {
            Files.delete(file.toPath());
            delete = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (delete) {
            System.out.println("已删除目录:" + file.getAbsolutePath());
        } else {
            System.out.println("删除目录失败:" + file.getAbsolutePath());
        }
    }
}

 

批量遍历重命名 RenameFile.java

javac RenameFile.java -encoding utf-8
java RenameFile "E:\workspace\RuoYi-Vue-Plus\" "ruoyi" "muphy"
//ackage me.muphy;

import java.io.File;

/**
 * 批量遍历重命名指定文件
 *
 * @className: RenameFile 
 * @author: 若非
 * @date: 2021-11-29 16:57
 */
public class RenameFile {
    static String oldName = "azi";
    static String newName = "azu";
    static String pathname = "E:\\workspace\\tt";

    public static void main(String[] args) {
        if (args.length > 2) {
            pathname = args[0];
            oldName = args[1];
            newName = args[2];
        }
        if (oldName == null || oldName.trim().isEmpty()) {
            System.out.println("原文件名不能为空!");
            return;
        }
        if (newName == null || newName.trim().isEmpty()) {
            System.out.println("新文件名不能为空!");
            return;
        }
        System.out.println("开始重命名文件!");
        renameFile();
        System.out.println("重命名文件完成!");
    }

    private static void renameFile() {
        File file = new File(pathname);
        if (!file.exists()) {
            System.out.println("指定路径不存在!");
            return;
        }
        if (!file.isDirectory()) {
            System.out.println("指定路径不是文件夹路径!");
            return;
        }
        renameFile(file);
    }

    private static void renameFile(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                renameFile(listFile);
            }
        }
        renameTo(file);
    }

    private static void renameTo(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        // if (file.getName().startsWith(oldName))
        if (file.getName().equals(oldName)) {
            boolean rename = false;
            // File nf = new File(file.getParentFile(), file.getName().replaceFirst(oldName, newName));
            File nf = new File(file.getParentFile(), newName);
            try {
                rename = file.renameTo(nf);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (rename) {
                System.out.println("已重命名目录:" + file.getAbsolutePath());
            } else {
                System.out.println("重命名目录失败:" + file.getAbsolutePath());
            }
        }
    }
}

 

posted @ 2021-11-29 17:55  明月心~  阅读(199)  评论(0编辑  收藏  举报