dcsimg
Results 1 to 16 of 16
  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    Problem with FileCopy

    Any thoughts on wy the filecopy does not work. Error indicates bad file name or number. The two fileezists are positive

    ' check the the file to be copied is in the current directory and the destination folder exists
    Fileexists = Dir(gPath & mSourceFileName) <> "": If Fileexists = Not True Then gEsc = 1
    Fileexists = Dir(mDestFilePath) <> "": If Fileexists = Not True Then gEsc = 2
    If gEsc <> 0 Then
    Item = mSourceFileName: If gEsc = 2 Then Item = mDestFilePath
    MsgBox " the location of " & Item & " was not found. The backup cannot be completed."
    Call cmdAbort_Click: Exit Sub
    End If

    ' if backup was selected then copy data file
    If OptSelect(1).Value = True Then
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " on " & Date & " @" & Time
    MsgBox "Backup Completed", vbOKOnly + vbInformation, "NOTICE"
    Call cmdAbort_Click

  2. #2
    Lively Member
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    93

    Re: Problem with FileCopy

    Quote Originally Posted by marv@stinson View Post
    Code:
            FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " on " & Date & " @" & Time
    By including
    Code:
    Time
    in the destination path you have colons in it which is not legal in the filesystem. You got to replace them with underscores or the like.

    Wolfgang

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,273

    Re: Problem with FileCopy

    Recommend formatting the Date and/or Time using Format() to prevent any potential of adding unwanted characters: \ / : etc
    Ex: Format(Now(), "yyyymmddhhnnss")
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,235

    Re: Problem with FileCopy

    Right now i'm trying to understand, what the purpose of that code is.

    Yes, he said "Backup" and "Filecopy", but this is some of the weirdest code-logic i have seen
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    I say you're crazy not to!
    --------------------------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,543

    Re: Problem with FileCopy

    check the the file to be copied is in the current directory and the destination folder exists
    not sure this is actually correct as the 2nd Dir does not return directories
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,235

    Re: Problem with FileCopy

    What i don't understand, why he has to even check if the source-file and destination-folder exists.
    The only two scenarios i can imagine running into this issue is if he's entering Path\Filename and Destination-Folder by Hand (in a textbox),
    or if between choosing Source-File and Destination-Folder and actually executing the FileCopy-Code someone deletes one of those.

    Every other scenario includes at least some BrowseForFolder/BrowseForFile-Dialogue, and there you can only choose something which exists.

    And no, i'm not going into silent code-execution.

    Nevermind that his logic is skewed.
    He executes this whole checking File/Folder exists, but makes it dependent of an Optionbox later, if false, the whole thing doesn't get executed.
    Get the logic straight and read up on some useful API's (https://msdn.microsoft.com/en-us/lib...v=vs.85).aspx)
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    I say you're crazy not to!
    --------------------------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    Re: Problem with FileCopy

    Yeah, I am attempting to backup a data file to a separate drive. There are two opSelect choices involed backup and restore

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    Re: Problem with FileCopy

    I did do that at one point. Got same result.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,543

    Re: Problem with FileCopy

    did you see the part about illegal characters in the filename? both date and time produce invalid characters, so use one of the formatting options suggested
    when i tested i got error 76, rather than the 53 you were getting, so maybe you have some other problem in addition to that

    when you check the destination folder it will only return true if there is already some file in the directory, if it is an empty directory it will return false
    you should specify to return directories

    post your updated code, also the contents of your variables
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    Re: Problem with FileCopy

    Thanks for the response. Your point appears to be valid I have tried the two statements
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Format(Now, " MM-dd-YYYY at HH-mm-SS")
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Now


    the first one works the second gives the Bad Filename or number error. Why would this be? I was not aware of the now statement but its value is exactly what I want to attach to the file name of the file to be copied. Is there any way to overcome this?

  11. #11
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    336

    Re: Problem with FileCopy

    Quote Originally Posted by marv@stinson View Post
    Thanks for the response. Your point appears to be valid I have tried the two statements
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Format(Now, " MM-dd-YYYY at HH-mm-SS")
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Now


    the first one works the second gives the Bad Filename or number error. Why would this be?
    This has been answered multiple times in the previous posts. Feel free to consult the following Wikipedia page that describes characters that are prohibited in file or folder names. https://en.wikipedia.org/wiki/Filena...ters_and_words

    Quote Originally Posted by marv@stinson View Post
    I was not aware of the now statement but its value is exactly what I want to attach to the file name of the file to be copied. Is there any way to overcome this?
    Find an operating system that allows those characters to be used as a filename, or write your own such operating system. Or adjust your desired filename format to one that contains only allowable characters in the filename.

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,273

    Re: Problem with FileCopy

    Thanks for the response. Your point appears to be valid I have tried the two statements
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Format(Now, " MM-dd-YYYY at HH-mm-SS")
    FileCopy gPath & gDataFileName, mDestFilePath & mSourceFileName & " " & Now


    the first one works the second gives the Bad Filename or number error. Why would this be? I was not aware of the now statement but its value is exactly what I want to attach to the file name of the file to be copied.
    If the value is exactly what you want, don't understand the problem then.

    Regarding why the 2nd one failed. You can't just use Now. Try this: MsgBox Now
    What you see is what will be included in the file name. What's even worse is that the format shown in the message box is per user regional settings for date/time. This means, even if it can work for one user, it is not guaranteed to work for another with different regional settings values.

    To overcome this, you use the Format() statement to force a specific, user-defined (if desired), format.

    In my signature below is a link to VB help files on-line. May want to bookmark it and then spend some time perusing the site to get an idea of all the functions and methods that are available to you.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,314

    Re: Problem with FileCopy

    Quote Originally Posted by marv@stinson View Post
    the first one works the second gives the Bad Filename or number error. Why would this be?
    As others have stated invalid path/filename. Namely the / and : characters that appear by default in a date time string.

    When you use Format(Now, " MM-dd-YYYY at HH-mm-SS") you are telling it to use - in pace of the / and : characters and the result is a valid filename. Whereas without the format statement you are generating an invalid filename.

    Personally I would not even use the dashes I would just use YYYYMMDDHHmmSS that way it will correctly sort on the filename.
    Last edited by DataMiser; Jun 14th, 2018 at 11:56 PM.

  14. #14
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    596

    Re: Problem with FileCopy

    Format must be the most resilient function in the run-time as in your sample format " MM-dd-YYYY at HH-mm-SS" you are using mm to mean months and minutes at the same time. And Format produces correct output just be noticing if there are hours/years or seconds/days around the mm part!

    As date formatting documentation states, please use nn for minutes.

    cheers,
    </wqw>

  15. #15
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,959

    Re: Problem with FileCopy

    It was documented at some point, that if a format has both date and time elements the second mm is interpreted as minutes.
    It might still be in the office documentation.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  16. #16
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    596

    Re: Problem with FileCopy

    Format(Now, "hh-mm-ss") works as expected for me. It seems more resilient than that.

    cheers,
    </wqw>

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.