3 Replies Latest reply: Mar 14, 2008 7:18 PM by 807591 RSS

    How to move a director with contents to a new directory

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

      System.out.println(nonCCResource.getLocation().toString());
      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.

      cheers,

      //mike
        • 1. Re: How to move a director with contents to a new directory
          807591
          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
            DrClap
            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
              807591
              //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)
              {

                   x=fileObject.getPath().length();
                   
              this.originalFileObject = fileObject;
              this.fileObject = fileObject;

                   }

              public void traverse()
              {
              recursiveTraversal(fileObject);
              }

                   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(y);
              // System.out.println( indent + fileObject.getPath());
              newFolderPath = "e:\\songs" + y;








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

              System.out.println(newFolderPath);


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

              }else if (fileObject.isFile()){

              if(fileObject.getPath().endsWith(".x"))
              {


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








              File dir = new File(newFolderPath);
              System.out.println(newFolderPath);

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


              // Move file to new directory
              boolean success = fileObject.renameTo(new File(dir, fileObject.getName()));
              System.out.println(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));
              traversal.traverse();
              }
              }