title: How to rename the folders and subfolders including the files present in it recursively using Java NIO Files?
body: <p>I cant able to rename the folder which contains a files or sub folders in it.</p>
  
  <p>My Folder structure is</p>
  
  <pre><code>D:
    root
        popcorn-folder1     
             popcorn-subfolder1   
                 popcorn-subfile1
             popcorn-file1         
        popcorn-folder2 
             popcorn-subfolder2  
             popcorn-file2 
  </code></pre>
  
  <p>My resulting Directory should be</p>
  
  <pre><code>D:
    root
        folder1     
             subfolder1   
                 subfile1
             file1         
        folder2 
             subfolder2  
             file2  
  </code></pre>
  
  <p>My tried code is </p>
  
  <pre><code>package com.din.pach;
  
  import java.io.File;
  import java.io.IOException;
  import java.nio.file.FileVisitResult;
  import java.nio.file.FileVisitor;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.nio.file.StandardCopyOption;
  import java.nio.file.attribute.BasicFileAttributes;
  
  public class FileNio {
  
  public static void main(String[] args) throws IOException {
  
      Path sourcePath      = Paths.get("D:\\root\\");
  
      Files.walkFileTree(sourcePath, new FileVisitor&lt;Path&gt;() {
          @Override
          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
              // System.out.println("pre visit dir:" + dir);
              //rename(dir);
              //renameFile(dir);
              return FileVisitResult.CONTINUE;
          }
  
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
              //System.out.println("visit file: " + file);
              renameFile(file);
              System.out.println("====================================================================");
              return FileVisitResult.CONTINUE;
          }
  
          @Override
          public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
              //   System.out.println("visit file failed: " + file);
              return FileVisitResult.CONTINUE;
          }
  
          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
              //  System.out.println("post visit directory: " + dir);
              renameDirectory(dir);
              return FileVisitResult.CONTINUE;
          }
  
  
  
      });
  
  
  
  }
  
  public static void renameFile(Path file) throws IOException {
  
  
      boolean isDirectory = Files.isDirectory(file);
      boolean isWritable = Files.isWritable(file);
      System.out.println("isDirectory-&gt; "+isDirectory);
      System.out.println("isWritable-&gt; "+isWritable);
  
      Path sourcePath      = Paths.get(file.toString());
      String origName = file.getFileName().toString();
      String newName = origName.replaceAll("POPCORN-", "");
      if (isWritable&amp;&amp;!isDirectory) {
  
  
  
  
          System.out.println("fname-&gt; "+origName);
          /*get the path of the directory*/
          String baseLoc = file.getParent().toString();
          System.out.println("baseLoc-&gt; "+baseLoc);
          if (origName.contains("POPCORN-")  /*|| origName.contains("#") || origName.contains("@")*/){
  
              System.out.println("Orig name-&gt; "+origName);
              /*origName = origName.replaceAll("&amp;", "_and_");
          origName = origName.replaceAll("@", "_at_");*/
  
  
              System.out.println("New Name-&gt; "+newName);
              String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
              System.out.println("newLoc-&gt; "+newLoc);
              //File newFile = new File(newLoc);
  
  
              Path destinationPath = Paths.get(newLoc);
  
              Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
  
          } else {
              System.out.println("No write permission");
          }
      }else{
  
          /*if(origName.contains("POPCORN-")  || origName.contains("#") || origName.contains("@")){
              Files.copy(sourcePath, sourcePath.resolveSibling(newName),StandardCopyOption.REPLACE_EXISTING);
          }*/
      }
  }
  
  
  
  public static void renameDirectory(Path file) throws IOException {
  
  
      boolean isDirectory = Files.isDirectory(file);
      boolean isWritable = Files.isWritable(file);
      System.out.println("isDirectory-&gt; "+isDirectory);
      System.out.println("isWritable-&gt; "+isWritable);
  
      Path sourcePath      = Paths.get(file.toString());
      String origName = file.getFileName().toString();
      String newName = origName.replaceAll("POPCORN-", "");
      if (isWritable&amp;&amp;isDirectory) {
  
              if(origName.contains("POPCORN-")  /*|| origName.contains("#") || origName.contains("@")*/){
                  Files.move(sourcePath, sourcePath.resolveSibling(newName),StandardCopyOption.ATOMIC_MOVE);
              }
  
          } else {
              System.out.println("No write permission");
          }
      }
  }
  </code></pre>
  
  <p>In above code, I can rename the files successfully. But below exception is thrown </p>
  
  <pre><code>Exception in thread "main" java.nio.file.AccessDeniedException: D:\root\POPCORN-folder1 -&gt; D:\root\folder1
  at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
  at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
  at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
  at java.nio.file.Files.move(Unknown Source)
  at com.din.pach.FileNio.renameDirectory(FileNio.java:121)
  at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:45)
  at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:1)
  at java.nio.file.Files.walkFileTree(Unknown Source)
  at java.nio.file.Files.walkFileTree(Unknown Source)
  at com.din.pach.FileNio.main(FileNio.java:19)
  </code></pre>
  
  <p>the implementation is based on this article <a href="http://tutorials.jenkov.com/java-nio/files.html" rel="nofollow noreferrer">Java NIO Files</a></p>
  
  <p>This is sequel of <a href="https://stackoverflow.com/q/48300897/2018343">Renaming a folder name which has sub directories is not working using java File.rename.to()</a></p>
  
  <p>EDIT: I have enabled the write permission for all the folders and files.</p>
  
  <p>But there is no answer to it. </p>
  
  <p>UPDATE: updated the Full console output</p>
  
  <pre><code>   isDirectory-&gt; false
     isWritable-&gt; true
     fname-&gt; popcorn-file1.txt
     baseLoc-&gt; D:\root\popcorn-folder1
     Orig name-&gt; popcorn-file1.txt
     New Name-&gt; file1.txt
     newLoc-&gt; D:\root\popcorn-folder1\file1.txt
     ====================================================================
     isDirectory-&gt; false
     isWritable-&gt; true
     fname-&gt; popcorn-subfile1.txt
     baseLoc-&gt; D:\root\popcorn-folder1\popcorn-subfolder1
     Orig name-&gt; popcorn-subfile1.txt
     New Name-&gt; subfile1.txt
     newLoc-&gt; D:\root\popcorn-folder1\popcorn-subfolder1\subfile1.txt
     ====================================================================
     isDirectory-&gt; true
     isWritable-&gt; true
     isDirectory-&gt; true
     isWritable-&gt; true
     Exception in thread "main" java.nio.file.AccessDeniedException: D:\root\popcorn-folder1 -&gt; D:\root\folder1
  at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
  at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
  at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
  at java.nio.file.Files.move(Unknown Source)
  at com.din.pach.FileNio.renameDirectory(FileNio.java:121)
  at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:45)
  at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:1)
  at java.nio.file.Files.walkFileTree(Unknown Source)
  at java.nio.file.Files.walkFileTree(Unknown Source)
  at com.din.pach.FileNio.main(FileNio.java:19)
  </code></pre>