Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I automatically backup and restore data files?

Tips -N- Tricks

How can I automatically backup and restore data files?

by  ChrisRChamberlain  Posted    (Edited  )
If you need a simple way to supplement normal backup procedures and ensure a user's data, (single user or workstation), can be automatically restored in the event of a system crash or other problem, try the following:-

Place [color blue]CABARC.exe[color black] and [color blue]EXTRACT.exe[color black], both available from http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B310618, (file named [color blue]CAB-SDK.exe[color black]), in the folder containing your databases, tables, etc.

If you are likely to have more than one application running, rename [color blue]CABARC.exe[color black] to [color blue]CABARCn.exe[color black], [color blue]EXTRACT.exe[color black] to [color blue]EXTRACTn.exe[color black], where you make [color blue]n[color black] to be the identification number of an application, so that you can detemine which copy is in use.

As the user quits the application a [color blue].CAB[color black] file is created, overwriting the previous one. The [color blue].CAB[color black] file is only created if the application shuts down correctly.

Both [color blue]CABARC.exe[color black] and [color blue]EXTRACT.exe[color black] will run hidden, so a user will be unable to interfere with their execution.

When an error is caused by corrupted data files, the files are extracted from the [color blue].CAB[color black] file.

The RepairData code is best placed in a dedicated form, the function of which is to explain to the user that they will need to start the application again.
[color green]
* Top of MAIN.prg
* The initial code is to determine if CABARC is still running,
* and if so quit the application
[color blue]
SET LIBRARY TO foxtools.fll
[color green]
* Register the Windows API functions that will be called
[color blue]
mGetWinTxt = RegFn("GetWindowText", "I@CI", "I")
mGetWindow = RegFn("GetWindow", "II", "I")
mIsWinVis = RegFn("IsWindowVisible", "I", "I")
[color green]
* Get the HWND (handle) to the main FoxPro window
[color blue]
foxhwnd = MAINHWND()
[color green]
* Produce a list of all windows
[color blue]
hwndNext = CallFn(mGetWindow,foxhwnd,0)
lCabFile = .F.
DO WHILE hwndNext <> 0
IF (hwndnext <> foxhwnd) AND ;
CallFn(mGetWindow,hwndnext,4) = 0

Stuffer = SPACE(64)
x = CallFn(mGetWinTxt,hwndnext,@Stuffer,64)
IF [CABARC] $ UPPER(Stuffer)
lCabFile = .T.
EXIT
ENDIF
ENDIF
hwndNext = CallFn(mGetWindow,hwndnext,2)
ENDDO

IF lCabFile
MESSAGEBOX([This application is still shutting down from the last session.],;
0 + 48 + 0 ,;
[Access denied])
QUIT
ENDIF
[color green]
* OK to proceed
[color blue]
ON ERROR DO RepairData
DO OpenDatabases [color green]&& Open databases, tables etc[color blue]
ON ERROR DO ErrorTrap WITH ;
SYS(16) ,;
LINENO() ,;
ERROR() ,;
MESSAGE(1) ,;
MESSAGE() [color green]&& Default error handler
* Normal code here
*
* Place this code to be the last lines of MAIN.prg
[color blue]
SET DEFA TO SYS(5)+SYS(2003)+[\tables\]
lcProg2Run = [cabarc n tables.cab *.dbc *.dbf *.cdx *.fpt]
DECLARE INTEGER WinExec IN kernel32 ;
STRING lpCmdLine ,;
INTEGER uCmdShow
IF !WinExec(lcProg2Run, 0) > 31
[color green]* MESSAGEBOX()[color blue]
ENDIF
QUIT

***************************
* Procedure: RepairData
* Syntax: Do RepairData
***************************
PROC RepairData

SET RESOURCE OFF
CLOSE ALL
[color green]* MESSAGEBOX([There has been a error etc])[color blue]
SET DEFA TO SYS(5)+ SYS(2003)+[\tables\]
lcProg2Run = [extract /y tables.cab *.*]
DECLARE INTEGER WinExec IN kernel32 ;
STRING lpCmdLine ,;
INTEGER uCmdShow
IF !WinExec(lcProg2Run, 0) > 31
[tab][color green]* MESSAGEBOX()[color blue]
ENDIF
QUIT
[color black]
* DISCLAIMER
* This Code is as is. There is no warranty expressed or
* implied. Use this code at your own risk.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top