1. 程式人生 > >windows文件夾嵌套太多,導致無法刪除的解決方法

windows文件夾嵌套太多,導致無法刪除的解決方法

string pri let absolute efi named print for ces

import java.io.File;

public class DeleteUtils
{

    public static void main(String[] args)
    {
        File dir = new File("D:\\code\\NCE\\Spliter_NCE");
        renameDirRecursively(dir);
        System.out.println("renameDirRecursively success");
        deleteRecursively(dir);
        System.out.println("deleteRecursively success");
    }

    private static void renameDirRecursively(File dir)
    {
        File[] listFiles = dir.listFiles();
        if (listFiles != null)
        {
            int subIndex = 0;
            for (File file : listFiles)
            {
                boolean isDir = file.isDirectory();
                StringBuilder afterName = new StringBuilder();
                subIndex = renameFile(file, subIndex, afterName);
                if (isDir)
                {
                    renameDirRecursively(new File(afterName.toString()));
                }
            }
        }
    }

    private static int renameFile(File file, int index, StringBuilder afterName)
    {
        String newName = file.getParent() + File.separator + index;
        if (afterName != null)
        {
            afterName.append(newName);
        }

        if (newName.equals(file.getAbsolutePath()))
        {
            return index + 1;
        }

        boolean success = file.renameTo(new File(newName));
        if (success)
        {
            return index + 1;
        }
        else
        {
            return renameFile(file, index + 1, afterName);
        }
    }

    private static void deleteRecursively(File file)
    {
        if (file.isDirectory())
        {
            File[] listFiles = file.listFiles();
            if (listFiles != null)
            {
                for (File subFile : listFiles)
                {
                    deleteRecursively(subFile);
                }
            }
            file.delete();
        }
        else
        {
            file.delete();
        }
    }

}

  

windows文件夾嵌套太多,導致無法刪除的解決方法