更改父目录和子目录中的文件名

时间:2013-10-30 15:06:39

标签: java file-io

我是Java的初学者,试图使用文件和目录。我想创建一个程序,我可以自动更改文件名,同时在所有子目录中搜索无效的文件名。我实际上是在尝试将大量文件加载到服务器上,但服务器设置不允许包含特殊字符的文件名。首先,我能够编写代码,如果我将路径传递给目录,则会重命名该目录中名称无效的所有文件:

public class reNaming {

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++;
        }

    }
}

}

现在我想做同样的事情,但这次我想要在子目录中重新命名所有文件。有人可以指导我如何实现这一目标吗?

2 个答案:

答案 0 :(得分: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; 
}

答案 1 :(得分: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){ }