How to rename the folder in java

19,393

Solution 1

Here is how I solved my problem.

  1. I get the directory present at specified depth.
  2. Create new directory with altered name
  3. Used FileUtils (Apache Commons IO) to copy the files to the new folder.
  4. Manually delete all those old folder.

package com.so.practice;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;

public class Sof {
    public static void main(String[] args) throws IOException {
        List<File> files = getDirs(new File("path to root folder"), 0); //level 0 is inside parent , level 1, and so on
        System.out.println(files);
        String[] paths = new String[files.size()];

        int i = 0;
        for (File file : files) {
            paths[i++] = file.getAbsolutePath();
        }
        String matchword = "Folder1";

        //File  f = null;
        HashMap<String, String > old_new = new HashMap<>();
        for (int j = 0; j < paths.length; j++) {
            System.out.println(paths[j]);
            String old_path = paths[j];
            String foldername = new File(paths[j]).getName();
            //02_PA__OPCON.MES.GC.Configuration 
            //02_Configuration

            if(old_path.contains(matchword)){
                paths[j] =paths[j].replaceAll(matchword, "Directory");

                old_new.put(old_path, paths[j]);

            }else{
                System.out.println("skipping->"+old_path);
            }
            //f = new File(paths[j]);
            //f.mkdirs();
        }

        for(String key : old_new.keySet()){
            FileUtils.copyDirectory(new File(key), new File(old_new.get(key)));
        }

        //FileUtils.copyDirectory(new File(old_new.get), new File(arg0));
     }

     static List<File> getDirs(File parent, int level){
        List<File> dirs = new ArrayList<File>(); //to store 
        for(File f: parent.listFiles()){
            if(f.isDirectory()) {
                if (level==0) dirs.add(f);
                else 
                    if (level > 0) dirs.addAll(getDirs(f,level-1)); //recursion
            }
        }
        return dirs;
    }
}

Solution 2

File dir = new File(dirPath);
if (!dir.isDirectory()) {
  System.err.println("There is no directory @ given path");
} else {
    System.out .println("Enter new name of directory(Only Name and Not Path).");
    String newDirName = scanner.nextLine();
    File newDir = new File(dir.getParent() + "\" + newDirName);
    dir.renameTo(newDir);
}
Share:
19,393
Dinesh Ravi
Author by

Dinesh Ravi

Updated on June 14, 2022

Comments

  • Dinesh Ravi
    Dinesh Ravi almost 2 years

    My folder structure looks like

    D:
    |- Folder1
       |- File1
    |- Folder2
       |- File2 
    

    output:

    D:
    |- Directory1   <- renamed
       |- File1 
    |- Directory2   <- renamed
       |- File2 
    

    The question is how to rename the folders one level down?