10 Replies Latest reply: Feb 13, 2013 10:13 PM by EJP RSS

    how to move a file ?

    user575089
      Hi ,

      I want to move a file from folder A to folder B in windows without changing its creation date .

      How do I do it ?
        • 1. Re: how to move a file ?
          jtahlborn
          what happens when you use File.renameTo()?
          • 2. Re: how to move a file ?
            user575089
            Why rename ?

            I want to move file without changing its creation time.

            rename can not move a file
            • 3. Re: how to move a file ?
              aksarben
              The method has an unfortunate name. Actually, it can move a file.
              • 4. Re: how to move a file ?
                rp0428
                >
                The method has an unfortunate name. Actually, it can move a file.
                >
                Please post a sample of a rename that moves a file on windows.

                On WinXP SP3 you can't do it.
                >
                E:\>help ren
                Renames a file or files.

                RENAME [drive:][path]filename1 filename2.
                REN [drive:][path]filename1 filename2.

                Note that you cannot specify a new drive or path for your destination file.

                E:\>
                • 5. Re: how to move a file ?
                  jtahlborn
                  rp0428 wrote:
                  >
                  The method has an unfortunate name. Actually, it can move a file.
                  >
                  Please post a sample of a rename that moves a file on windows.

                  On WinXP SP3 you can't do it.
                  >
                  E:\>help ren
                  Renames a file or files.

                  RENAME [drive:][path]filename1 filename2.
                  REN [drive:][path]filename1 filename2.

                  Note that you cannot specify a new drive or path for your destination file.

                  E:\>
                  >
                  you realize we are talking about java code here, not command line tools?
                  • 6. Re: how to move a file ?
                    EJP
                    The relevance of the Windows command-shell REN command to the File.renameTo() method escapes me. The underlying system call for Win32 is MoveFile(), which can certainly move files across folders, although not across file systems.
                    • 7. Re: how to move a file ?
                      805574
                      Actually this is the same behavior you see when using the standard C library. The C function rename will not change a name across file systems (or drives if on windows) either. If you wish to "move" a file across file systems, you need to write code to copy the file, then on success delete the original. A generic method could just note that the rename call failed for the cross file system reason, then fall back to the copy/delete method.

                      Based on the javadocs for File.renameTo( ), I would suspect in some implementations it may just be a wrapper call to the standard C library rename function.
                      • 8. Re: how to move a file ?
                        EJP
                        Actually this is the same behavior you see when using the standard C library. The C function rename will not change a name across file systems (or drives if on windows) either.
                        Of course it won't. That's because it calls MoveFile() under the hood. Nothing else it can sensibly do.
                        Based on the javadocs for File.renameTo( ), I would suspect in some implementations it may just be a wrapper call to the standard C library rename function.
                        Or the MoveFile() function. It really doesn't matter which.
                        • 9. Re: how to move a file ?
                          user575089
                          EJP wrote:
                          The relevance of the Windows command-shell REN command to the File.renameTo() method escapes me..............which can certainly move files across folders, although not across file systems.
                          I need to move file from folder A in C:\ to folder B in D:\ without changing  file creation time.

                          What is the solution ?
                          • 10. Re: how to move a file ?
                            EJP
                            If the folders are on the same drive, File.renameTo() will do that. If they aren't you have to copy and reset the creation time yourself, and I think you need Java 7 and NIO2 to do that.