4

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?

Dinesh Ravi
  • 1,209
  • 1
  • 17
  • 35
  • use an IDE and refactor your code/structure – Edwin Jan 19 '18 at 11:22
  • I have tried with nio. but It is complex to stop at one level. With out nio I dont know how to control with levels.. – Dinesh Ravi Jan 19 '18 at 11:26
  • What kind of IDE do you use? – degath Jan 19 '18 at 11:27
  • Eclipse is the IDE – Dinesh Ravi Jan 19 '18 at 11:28
  • 1
    You already ask other questions how to rename a file/folder in java, which had also a working code. But in on [question](https://stackoverflow.com/q/48320280/8097737) was a `AccessDeniedException` thrown. So is this really a problem related to java? Can you rename the specific folders with other tools? –  Jan 19 '18 at 11:42
  • @devpuh Check the comment of that answer that didnot solved and I have updated the question aswell. yes of course this is related to java – Dinesh Ravi Jan 19 '18 at 11:54
  • @DineshRavi I know, but this doesn't answer the question "can you rename the folder with other tools?". Also it's possible that you can't rename a file/folder even when you have all permissions, since its locked. –  Jan 19 '18 at 11:57
  • @devpuh I dont know any other tool other that manually renaming each folder. but there are > 1000 folders. – Dinesh Ravi Jan 19 '18 at 12:00
  • 1
    @DineshRavi so can you manually rename one of those files which causes the trouble? –  Jan 19 '18 at 12:02
  • @devpuh yes, manually I can rename all the folders and files. – Dinesh Ravi Jan 20 '18 at 13:01
  • @DineshRavi When you can manually rename all these files and folders (without any warnings) it should be possible to rename them with the code you provided. I guess: The question is not why the code doesn't work, but rather how the environment affects it. What and how are this files/folders created? Does the program still run which created (or works with) the files/folders? In your code to create the new name you use `replaceAll`, do you ensure that there didn't already exist a a file/folder with this name. –  Jan 22 '18 at 06:55
  • @devpuh I found a way to solve the above problem. Check my answer below – Dinesh Ravi Jan 22 '18 at 12:44
  • @DineshRavi so the problem was that two or more folders share the same name after renaming? (e.g.: "Folder1" and "Folder2" are both renamed to "Directory") –  Jan 22 '18 at 13:03

2 Answers2

14
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);
}
Ann
  • 463
  • 5
  • 15
  • 2
    Avoid the use of `System.exit(0);` –  Jan 19 '18 at 11:32
  • 2
    If you need to make not OS based path, instead of File newDir = new File(dir.getParent() + "\" + newDirName); use File newDir = new File(dir.getParent() + File.separator + newDirName); – Luis Carlos Aug 13 '18 at 11:48
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;
    }
}
Dinesh Ravi
  • 1,209
  • 1
  • 17
  • 35
  • 2
    You are aware that [`FileUtils.copyDirectory`](https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#copyDirectory-java.io.File-java.io.File-) overrides existing files in the destination directory when it already exists? –  Jan 22 '18 at 13:02
  • @devpuh Thanks I dont know that – Dinesh Ravi Jan 23 '18 at 03:53