java 批量重命名文件V2

java 批量重命名文件V2

 

 

import java.io.File;

public class BatRename {

    public static long fileCount = 0;

    public static long dirCount = 0;

    public static long renamedCount = 0;

    public static long deletedCount = 0;
    

    public static void main(String[] args) {

        String filePath = "E:\\battest";
        String replaceFrom = ".swf";
        String replaceTo = ".swf1";

        if (Rename(filePath, replaceFrom, replaceTo) == false) {
            System.out.println("PROCESS ERROR");
        } else {
            System.out.println("PROCESSED DONE");
        }

        System.out.println("total directories : " + dirCount);
        System.out.println("total files : " + fileCount);
        System.out.println("renamed files' Count : " + renamedCount);
        System.out.println("deleted files' Count : " + deletedCount);
    }

    private static boolean Rename(String path, String replaceFrom,
            String replaceTo) {

        try {
            File fl = new File(path);
            String[] files = fl.list();
            File f = null;
            File renamedFile = null;

            String filename = "";
            for (String file : files) {

                f = new File(fl, file);

                if (f.isDirectory()) {
                    System.out.println("\n==> directory : " + path
                            + java.io.File.separator + file);

                    if (Rename(path + java.io.File.separator + file,
                            replaceFrom, replaceTo) == false) {
                        System.out.print("\tPROCESS ERROR");
                    }

                    dirCount++;
                } else {

                    filename = f.getName();
                    System.out.print(path + java.io.File.separator + filename);

                    if (filename.endsWith(replaceFrom)
                            || filename.contains(replaceFrom)) {
                        System.out.print("\t\t-->\t\t");
                        if ((new File(fl.getAbsolutePath()
                                + java.io.File.separator
                                + filename.replace(replaceFrom, replaceTo)))
                                .exists() == true) {
                            f.delete();

                            System.out.print("deleted");

                            deletedCount++;
                        } else {

                            renamedFile = new File(fl.getAbsolutePath()
                                    + java.io.File.separator
                                    + filename.replace(replaceFrom, replaceTo));

                            f.renameTo(renamedFile);
                            System.out.print(path + java.io.File.separator
                                    + renamedFile.getName());

                            renamedCount++;
                        }
                    }

                    System.out.println();

                    fileCount++;
                }
            }

            return true;
        } catch (Exception ex) {

            ex.printStackTrace();
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值