Liberty BASIC Community Forum
Liberty BASIC Programming Discussions >> Liberty BASIC Code >> Copying a database

Copying a database
Post by LBcode on Jan 10th, 2016, 4:46pm

Can code be created in a LB program that would copy and save that database? I want it to be used as a backup or like a restore point for the program.
Re: Copying a database
Post by metro on Jan 10th, 2016, 6:36pm

you may find these useful
Re: Copying a database
Post by LBcode on Jan 10th, 2016, 7:41pm

I had been to the CopyFileFunction link. The file.operations link seems to be what I will have to use. I was hoping I could copy and move a folder instead of individual files since it would be a lot easier. Thanks for your help metro.
Re: Copying a database
Post by metro on Jan 10th, 2016, 8:39pm

sorry, thought one file for a DB
there maybe a better option than this
why not try a batch file.
you could use DefaultDir$ and MyDocument folders (or other destination)
create a *.bat file within your code and execute with the "RUN" command
using a wildcard you could copy all *.dat files from one folder to another
I have also used batch files to backup and compress or restore and decompress databases using arj

[MS-DOS] The COPY command

    COPY /Y /A/B [d:][path]filename /A/B [d:][path][filename] /V


    /Y - Replace existing files without any confirmation prompt.
    /-Y - Displays a confirmation prompt before overwriting the existing files.
    /A - Copy ASCII files
    /B - Copy Binary files
    /V - Check if file was properly copied 

CSIDL.PERSONAL = 5    'My Documents Folder

myDocuments$ = GetSpecialfolder$(CSIDL.PERSONAL)

print "My Documents folder location is: "
print myDocuments$

if right$(myDocuments$,1)<>"\" then
end if

open myDocuments$ + "testxxxx.txt" for output as #f
print #f, "Test"
close #f


Function GetSpecialfolder$(CSIDL)
    struct IDL,cb As Long, abID As short
    calldll #shell32, "SHGetSpecialFolderLocation",_
        0 as long, CSIDL as long, IDL as struct, ret as long
    if ret=0 then
        Path$ = Space$(512)
        calldll #shell32, "SHGetPathFromIDListA",id as long, Path$ as ptr, ret as long
        GetSpecialfolder$ = Left$(Path$, InStr(Path$, Chr$(0)) - 1)
        GetSpecialfolder$ = "Error"
    end if
    End Function 

Re: Copying a database
Post by Rod on Jan 11th, 2016, 02:49am

Some times you are better staying within Liberty especially if you wish to catch errors. Unless you pipe the .bat results to a file you will never know if the file copy was successful.

Also Liberty has NAME command

NAME a$ AS b$

NAME StringExpr1 AS StringExpr2

Which allows files to be copied or renamed.

The thing about databases is that if you copy whole databases you are inevitably copying the error. So you need to keep multiple time stamped copies so you can go back in time.

This also leads you to record and save all of the database transactions in similar order so that you can backtrack to when things were ok and go forward transaction by transaction. So if there has been a lot of activity you don't have the whole days transactions to reenter.

I would write the transaction records to two files as I go rather than hope there is an error free file at the end of the day to copy.
Re: Copying a database
Post by Chris Iverson on Jan 11th, 2016, 11:51am

I've got code lying around that can be modified to copy a whole folder instead of just one file. Let me look for it and get back to ya.
Re: Copying a database
Post by LBcode on Jan 11th, 2016, 1:02pm

Thanks guys. I have to admit some of these things are over my head but I am trying hard and testing things to figure it all out. I have learned so much from you guys in such a short time and I really do appreciate all your help. Thanks a lot.
Re: Copying a database
Post by Chris Iverson on Jan 11th, 2016, 8:18pm

Here's an example that uses the Windows Shell API to do the copy for us.

The only thing I'll note is that you MUST use full paths to files to do this. That's why I use the built-in DefaultDir$ variable.

srcPath$ = DefaultDir$ + "\bmp"
dstPath$ = DefaultDir$ + "\bmpcopy"
a =  CopyFolder(srcPath$, dstPath$)
If a = 0 then 
    print "Copy successful!"
    print "Uh oh, something went wrong."
End If

Function CopyFolder(src$, dest$)
    FO.COPY = 2
    FOF.SILENT = 4
    FOF.NOERRORUI = hexdec("400")
    FOF.NOCONFIRMMKDIR = hexdec("200")

        hWnd as ulong,_
        wFunc as ulong,_
        pFrom$ as ptr,_
        pTo$ as ptr,_
        fFlags as long,_
        fAnyOperationsAborted as long,_
        hNameMappings as long,_
        lpszProgressTitle$ as long

    SHFILEOPSTRUCT.hWnd.struct = 0
    SHFILEOPSTRUCT.pFrom$.struct = src$ + chr$(0) + chr$(0)
    SHFILEOPSTRUCT.pTo$.struct = dest$ + chr$(0) + chr$(0)
    SHFILEOPSTRUCT.fFlags.struct = FOF.NO.UI
    SHFILEOPSTRUCT.hNameMappings.struct = 0
    SHFILEOPSTRUCT.lpszProgressTitle$.struct = ""

    CallDLL #shell32, "SHFileOperationA",_
    SHFILEOPSTRUCT as struct,_
    CopyFolder as long
End Function 

Re: Copying a database
Post by LBcode on Jan 12th, 2016, 04:29am

Thanks Chris. I will work on it.
Re: Copying a database
Post by Chris Iverson on Jan 12th, 2016, 5:03pm

Let us know if you have any questions, or if you need anything else!
Re: Copying a database
Post by LBcode on Jan 12th, 2016, 7:34pm

Chris, You are the LB Maestro. Worked perfectly first time.
My database has 741 files in it and it copied them all in a flash.
Thank you!