7 Replies Latest reply: May 7, 2008 2:29 PM by 791266 RSS

    how do I rename a file to a folder?  (problem with \'s?)

    797160
      I'm trying to write a file to a folder doing this:
          String outputDir = "C:\Documents and Settings\mgs145\Desktop\";
          String newfilename = outputDir + tempname;
          oldfile.renameTo(new File(newfilename));
      However, nothing's happening, and I think the problem is in the outputDir assignment line.
      As it's shown here, I get compiler errors for the \'s, so I've been monkeying around with them, but cant' get them to work. Can someone tell me how I need to do this?
      Thanks!
        • 1. Re: how do I rename a file to a folder?  (problem with \'s?)
          791266
          Is that really your code? The backslash within a string must be escaped with another backslash. E.g.
          "c:\documents"
          will become
          "c:\\documents"
          Kaj

          Edited by: kajbj on 2008-maj-07 21:27
          • 2. Re: how do I rename a file to a folder?  (problem with \'s?)
            baftos
            "C:\\Documents and Settings\\mgs145\\Desktop\\"
            This is how a single \ is represented in a string literal.
            There may be other problems as well.
            • 3. Re: how do I rename a file to a folder?  (problem with \'s?)
              791266
              @Op. There are also restrictions on how you can rename a file. What gets printed if you print oldfile and newfile?

              Kaj
              • 4. Re: how do I rename a file to a folder?  (problem with \'s?)
                797160
                Thank you both. I fixed that line, putting in
                's, but it's still not working. I thought that was the culprit, but obviously not. So, here's my code:
                File f = new File("C:/Documents and Settings/mgs145/Desktop/PDF Export");
                String[] ffs = null;
                if ( f.exists()  &&  f.isDirectory() ) 
                    {ffs = f.list();
                    }
                String outputDir = "C:\\Documents and Settings\\mgs145\\Desktop\\";
                String wsName = getWSName();
                //the next lines are used in Intralink - they are used to configure the Intralink window, and are not the culprit
                IL.openWindow("Workspace", wsName, "");
                IL.openWindow( "TableConfig", "", "" ); 
                IL.removeAllVisibleColumns(  ); 
                IL.addVisibleColumn( "Name" ); 
                IL.addVisibleColumn( "Revision" ); 
                IL.ok();
                // end intralink window configuration
                IL.selectAll("WSPI");
                Object selobs[] = IL.getSelectedObjects("WSPI");
                IL.deselectAll("WSPI");
                int len = selobs.length;
                for (int i = 0; i<ffs.length; i++)
                    {
                    String tempname = ffs;
                File oldfile = new File(tempname);
                if (!tempname.endsWith(".pdf"))
                {break;
                }
                if (tempname.startsWith("ENGINEER - "))
                {tempname = tempname.substring(11);
                        }
                tempname = tempname.substring(0,tempname.length()-4);
                String rev = "null";
                for (int j=0; j<len; j++)
                {
                String tablename = IL.getTableCellValue("WSPI",j,0).toString();
                if (tablename.startsWith(tempname))
                {
                rev = IL.getTableCellValue("WSPI",j,1).toString();
                break;
                }
                }

                tempname = tempname + "_" + rev + ".pdf";
                String newfilename = outputDir + tempname;
                JOptionPane.showMessageDialog(null, newfilename,"Oops.",JOptionPane.ERROR_MESSAGE); // debug
                JOptionPane.showMessageDialog(null, oldfile.getName(),"Oops.",JOptionPane.ERROR_MESSAGE); // debug
                oldfile.renameTo(new File(newfilename));
                }
                JOptionPane.showMessageDialog(null, "Finished!","Finished",JOptionPane.ERROR_MESSAGE); // notify user
                IL.closeWindow();
                The JOption messages I use for debugging seem to indicate that everythign is working correctly; It's finding the correct old files to rename, and giving the correct new name; it's just the rename itself that's not working.
                
                Edited by: lkb3 on May 7, 2008 12:41 PM
                
                Edited by: lkb3 on May 7, 2008 12:42 PM
                
                Edited by: lkb3 on May 7, 2008 12:43 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: how do I rename a file to a folder?  (problem with \'s?)
                  791266
                  Is the file open when you try to do this? Are you renaming to the same directory or are you trying to move it to another directory as well?
                  • 6. Re: how do I rename a file to a folder?  (problem with \'s?)
                    797160
                    The files are not open.

                    As you see it here, obviously it's trying to move it to a different folder.
                    I also tried changing
                    String newfilename = outputDir + tempname;
                    to
                    String newfilename = tempname;
                    so that the folder wouldn't be changed, but that didn't work either.
                    • 7. Re: how do I rename a file to a folder?  (problem with \'s?)
                      797160
                      I figured it out. Previously, with the command

                      String tempname = ffs;
                      File oldfile = new File(tempname);
                      I was not giving oldfile an actual existing path & file. I changed it to:

                      String tempname = ffs[i];
                      String path = f.getPath();
                      String fullName = path + "/" + tempname;
                      File oldfile = new File(fullName);

                      and now it works.