3 Replies Latest reply on Mar 15, 2008 12:18 AM by 807591

    How to move a director with contents to a new directory

      I am trying to move a directory ( and it's contents to a new directory). I have tried the following:

      tmpDir = new File (nonCCResource.getParent().getLocation().toString(), nonCCResource.getName()+".tmp");
      boolean success = tmpDir.mkdirs();
      File dir = new File(nonCCResource.getParent().getLocation().toString(), nonCCResource.getName());
      boolean success1 = dir.renameTo(tmpDir);

      When checking success1 it is false. And the success is true.

      Any ideas how I can do this.


        • 1. Re: How to move a director with contents to a new directory
          Some platforms won't let you rename (or remove) a directory, if it is busy: like when a process has it as working directory etc.
          • 2. Re: How to move a director with contents to a new directory
            First you create a File object that represents Y.tmp in a directory X. This of course may not exist.

            Next you call mkdirs, which creates a directory named X/Y.tmp. This does exist because the code returned true.

            Next you create a File object that represents Y in a directory X. This may or may not exist, but you seem to believe that it does exist.

            Finally you try to rename that from X/Y to X/Y.tmp. But since X/Y.tmp already exists (you created it earlier), that fails.
            • 3. Re: How to move a director with contents to a new directory
              //heres the code mine application too had the same need so u too can njoy :) well u need to add ur own folder path name thats all .. any help u can give with jtree + checkboxes of mycomputer (full explorer)

              import java.io.File;

              public class FolderTraversar {
              int x;
              String newFolderPath=null;

              public String indent = "";
              public File originalFileObject;
              public File fileObject;

              public FolderTraversar(File fileObject)

              this.originalFileObject = fileObject;
              this.fileObject = fileObject;


              public void traverse()

                   public void recursiveTraversal(File fileObject)

                   if (fileObject.isDirectory())     

                             indent = getIndent(fileObject);

                             //System.out.println("hello" + x);

                   String y=fileObject.getAbsolutePath().substring(x,fileObject.getAbsolutePath().length());
              // System.out.println( indent + fileObject.getPath());
              newFolderPath = "e:\\songs" + y;

              String newFolderName = fileObject.getName();
              String songs = newFolderPath;
              File f = new File(songs);


              File allFiles[] = fileObject.listFiles();
              for(File aFile : allFiles){

              }else if (fileObject.isFile()){


              String y=fileObject.getAbsolutePath().substring(x,fileObject.getAbsolutePath().length()-fileObject.getName().length());
              // System.out.println( indent + fileObject.getPath());
              newFolderPath = "e:\\songs" + y;

              File dir = new File(newFolderPath);

              //String file = fileObject.getAbsolutePath());

              // Move file to new directory
              boolean success = fileObject.renameTo(new File(dir, fileObject.getName()));



              public String getIndent(File fileObject)
              String original = originalFileObject.getAbsolutePath();

              String fileStr = fileObject.getAbsolutePath();     
              String subString =
              fileStr.substring(original.length(), fileStr.length());

              String indent = "";
              for(int index=0; index<subString.length(); index ++){
              char aChar = subString.charAt(index);

              if (aChar == File.separatorChar){
              indent = indent + " ";
              return indent;

              public static void main(String[] args) {

              String folderPath =
              "C:\\Documents and Settings\\jahan\\Desktop\\files";

              FolderTraversar traversal = new FolderTraversar(new File(folderPath));