1

ファイルとディレクトリを操作しようとしている Java の初心者です。すべての子ディレクトリで無効なファイル名を検索しながら、ファイル名を自動的に変更できるプログラムを作成したかったのです。サーバーに大量のファイルをロードしようとしていますが、サーバーの設定では特殊文字を含むファイル名が許可されていません。まず、パスをディレクトリに渡すと、そのディレクトリ内の無効な名前を持つすべてのファイルの名前を変更するコードを書くことができました。

パブリック クラスの名前変更 {

public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup";

public static void main(String[] args) {

    //LinkedList<File> fileList = new LinkedList<File>();
    File obj = new File(baseLoc);
    int count = 0;

    for (File file: obj.listFiles())
    {

        String origName = file.getName();

        if (origName.contains("&")  || origName.contains("#") || origName.contains("@"))
        {
            System.out.println("Original name: "+origName);
            origName = origName.replaceAll("&", "_and_");
            origName = origName.replaceAll("@", "_at_");
            String newName = origName.replaceAll("#", "_");
            System.out.println("New Name: "+newName);
            String newLoc = baseLoc+"/"+newName;
            File newFile = new File(newLoc);
            System.out.println(file.renameTo(newFile));
            count++;
        }

    }
}

}

今、私は同じことをしたいのですが、今回だけ、子ディレクトリでもすべてのファイルの名前を変更したいのです。誰かがそれを達成する方法を教えてもらえますか?

4

2 に答える 2

1

再帰はあなたの友達です

/**Removes 'invalid' characters (&,#,@) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/
public int renameDirectory(File base){
    //LinkedList<File> fileList = new LinkedList<File>();

    int count=0;//count the renamed files in this directory + its sub. You wanted to do this?

    //Process each file in this folder.
    for (File file: base.listFiles()){

        String origName = file.getName();
        File resultFile=file;

        if (origName.contains("&")  || origName.contains("#") || origName.contains("@")){
            //I would replace the if statement with origName.matches(".*[&#@].*") or similar, shorter but more error prone.
            System.out.println("Original name: "+origName);
            origName = origName.replaceAll("&", "_and_");
            origName = origName.replaceAll("@", "_at_");
            String newName = origName.replaceAll("#", "_");
            System.out.println("New Name: "+newName);
            String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
            File newFile = new File(newLoc);
            System.out.println(file.renameTo(newFile));
            count++;
            resultFile=newFile;//not sure if you could do file=newFile, tired
        }

        //if this 'file' in the base folder is a directory, process the directory 
        if(resultFile.isDirectory()){//or similar function
            count+=renameDirectory(resultFile);
        }
    }
    return count; 
}
于 2013-10-30T15:23:44.750 に答える
0

持っているコードをユーティリティ メソッド (例: public void renameAll(File f){}) に移動します。ファイルがディレクトリであるかどうかを確認し、その内容でメソッドを再帰的に呼び出す条件を設定します。その後、現在行っていることを実行します。

public void renameAll(File[] files){

    for(File f: files){
        if(f.isDirectory){
           renameAll(f.listFiles());
        }
        rename(f);

    }

}

public void rename(File f){ }
于 2013-10-30T15:17:59.877 に答える