Link to home
Start Free TrialLog in
Avatar of teamdad
teamdad

asked on

Backing up and restoring a file

I am trying find some code to do a simple backup and resotore of a Data.ini file, In short; I have a Data.txt file in a directory called Support Files that I access with Application.StartupPath() I need a button to make a copy of the Data.txt file in the Support Files directory and place the copy of the file in a directory Support Files\Backups but have it renamed to a format of the name "Data" plus the date it was saved on and the .txt extension.  

If for some reason the user messes up the main Data.txt file in the Support Files directory I also need a button to allow them to open the Support Files\Backups directory and select the file they want to replace the bad Data.txt file with.  It will then overwrite the Data.txt file in the Support Files directory with the file from the Support Files\Backups directory and leave the backup copy in the Support Files\Backups directory too.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
teamdad,

In your first sentence you stated that the file was Data.ini, but in all subsequent references it is Data.txt.

If the file is indeed data.txt and not data.ini, then change all occurrences of "ini" in the code with "txt".

~IM
You should still be able to use the code srcalc gave you to make a Backup of the file:

    Dim datapath = Application.StartupPath & "\Support Files\"
    Public Sub backupData()
        If Not File.Exists(datapath & "Data.ini") Then
            MsgBox("There is no data to copy!")
        Else
            File.Copy(datapath & "Data.ini", datapath & "Backups\Data" & Now.Month & "-" & Now.Day & "-" & Now.Year & " " & Now.Hour & "-" & Now.Minute & "-" & Now.Second & ".ini")
        End If
    End Sub

~IM