Results 1 to 9 of 9
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Copy & Rename (02)

    Is it possible to use fs.CopyFile inconjunction with other code to copy the file from one directory and paste it to a different directory with a different file name.

    Example: File01.xls in directory C: to File01_Date_Time.xls in directory E:hold

    Thanks,
    John

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: File Copy & Rename (02)

    Something like this?

    Steve

    <pre>Sub CopyRename()
    Dim oFS
    Set oFS = CreateObject(" Scripting.FileSystemObject")
    oFS.CopyFile "C:File01.xls", _
    "E:holdFile01_Date_Time.xls"
    Set oFS = Nothing
    End Sub</pre>


  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Copy & Rename (02)

    Steve,

    Your suggestion works.

    I have another issue where I try including the TIME as part of the file name. XL does not save the file with ":" as part of the file name. Do you know of any other way to include the time stamp as part of the file name?

    Thanks,
    John

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: File Copy & Rename (02)

    You could use the Format function, for example, include Format(Time, "hhmm") in the file name, or Format(Now, "yyyyddmmhhmm")

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Copy & Rename (02)

    Thanks Hans

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: File Copy & Rename (02)

    Another one not explicitly stated by Hans that you might like is:
    Format(Now, "yyyy-dd-mm hh-mm")

    It contains some separation and allowable delimiters
    Steve

  7. #7
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Copy & Rename (02)

    Steve,

    I receive the error message "bad filename or number" when I try to pass variables into the code.

    Example:
    Sub CopyRename()
    Dim oFS
    Set oFS = CreateObject(" Scripting.FileSystemObject")
    oSource = "C:File01.xls"
    oDestination = "E:holdFile01_Date_Time.xls"
    oFS.CopyFile oSource, oDestination
    Set oFS = Nothing
    End Sub

    Any clues as to why?
    Thanks,
    John

  8. #8
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: File Copy & Rename (02)

    It works for me as you have written it.

    Do you have a file named "C:File01.xls" saved on your hard drive? This would give a "file not found" error
    A non-existent drive "E" or a bad path in the oDest would give a "path not found." error
    The oDest file does not have to exist though if it does it will be overwritten. I do not think you get a warning.

    Another option is that the name in oDest is not as you list here but something different which is not a valid filename.

    Steve
    PS there is an error in one of the lines (an extra space after the first dbl quote):
    Set oFS = CreateObject("Scripting.FileSystemObject")

  9. #9
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Copy & Rename (02)

    Steve,

    You are correct: it does indeed work. It was my error. I had a blank space before the "E" such as " E".

    I didn't see it.

    Thanks,
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •