Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Preserve sourcefile CreationTime on backup

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Preserve sourcefile CreationTime on backup

    This is a lot of explanation for two lines of source code, but someday you might appreciate it!

    When I want to make a backup of a file, I normally think of leaving the original in place, and making a copy to a backup location. I would think to use PB's FileCopy statement to copy the original to the backup destination. However, this simple process encountered unexpected interference from the OS (XP Pro and others)...

    First, the copy's CreationTime gets set to the current date and time, and the copy will NOT have the CreationTime of the original file. (Reflex reaction would be to use the SetFileTime API call, but the OS can also interfere with that...)

    Second, after the copy is deleted, and another copy is made to the same location, a feature of the OS called "file tunneling" could impose the CreationTime from the DELETED file onto your new copy. I was not able to overcome this effect with any use of the SetFileTime API call.

    See discussion at: http://www.powerbasic.com/support/pb...ad.php?t=39340

    HOWEVER, I found that NAME actually can be used to preserve the CreationTime, IF you reverse your thinking about the backup process!!!!

    Code:
    Name SourceFile As DestinationFile    ' << make backup FIRST, by MOVing original
    FileCopy DestinationFile, SourceFile  ' << restore original from backup copy
    This way, when the OS preserves the CreationTime from the DELETED original and imposes it on the new copy, the result is precisely what was wanted -- both files are identical, right down to their CreationTime stamps!

    So if it's important to you that your backup copy carry the same CreationTime stamp of the original, just follow the above two steps, and you're golden!



    To add your comments, please go to the discussion thread:
    http://www.powerbasic.com/support/pb...ad.php?t=39340

  • #2
    I noticed that I never posted a functional routine to demonstrate how to preserve the Creation Date on a file backup... simple (simplistic?) but effective!

    Code:
    'FileBACKUP_test.bas
    #Dim All
    
    Function BackupFile(ByVal SourceFile As String, ByVal DestinationFile As String) As Long
    
       'this works, and is the ONLY way I've found to have the "backed up" file retain the original CreationTime stamp!
       Name SourceFile As DestinationFile     'the order of these two statements is counter-intuitive, BUT...
       FileCopy DestinationFile, SourceFile   '...they accomplish the desired end result with ease!
       'the logical alternatives involving API calls DO NOT work due to "file tunneling" as discussed in:
       'http://www.powerbasic.com/support/pbforums/showthread.php?t=39340 '...teaser thread
       'http://www.powerbasic.com/support/pbforums/showthread.php?t=39419 '...concise explanation (above)
    
       If Err Then
          MsgBox Str$(Err),,"Error during FileCopy"
          Function = 0   'failure
       Else
          Function = 1   'success
       End If
    End Function
    
    'simple driver
    Function PBMain () As Long
       Local SourceFile, DestinationFile As String, RetVal As Long
    
       SourceFile = "c:\bat\MOVEME.txt"
       DestinationFile = "c:\copiedMOVEME2.txt"
    
       RetVal = BackupFile (SourceFile, DestinationFile)
       If RetVal Then
          MsgBox SourceFile & " ==> " & DestinationFile,,"File backup succeeded!"
       Else
          MsgBox SourceFile & " ==> " & DestinationFile,,"File backup failed!"
       End If
       'compare the source and destination files' Creation Dates - they're the same! I don't know of any other way to do that! 
    End Function

    Comment

    Working...
    X