Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Opening all dbfs in a directory
Message
De
16/10/2013 11:04:01
Mike Yearwood
Toronto, Ontario, Canada
 
 
À
16/10/2013 09:20:38
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01585571
Message ID:
01585632
Vues:
90
J'aime (1)
Rick

You like to preach - hoping people will learn from your example. If you won't learn from mine, why should anyone listen to yours?

You should have done this simple task far more elegantly and without risky practices.

Don't comment every line. That just clutters the code. Comment the general functionality - explain the why - more than the how - at the top. Make the code readable.

Don't use public. Make the routine and all its variables self-contained and protected from the rest of the application. That means start with LOCAL and if needed private and rarely public. I did the same thing you did without an extra function, less memory variables, no array, less lines of code. You'll also see that correctly applied, the m. is not onerous at all.

Note the USE IN SELECT("thisDdf") below. It is a one-liner to close the table. Compared to the IF USED...USE technique,
this is faster when the table is USED and is the same when the table is NOT USED.

Even better, when the USE fails because the DBF is bad, the file was not opened.
The code skips immediately to the CATCH skipping the USE IN SELECT("thisDbf").

That makes for good tracing during debugging, which is an important consideration when writing code.

No need to push/pop any global error handler.
loError gives more detail about the cause of why the file didn't open.

I tested this code including a bad file and it worked here. Why post untested code?
* Loop through current folder to find bad dbf
* files by trying to open each. Log failures.
CLEAR
LOCAL lnBadCount, lcTable, loError
lnBadCount = 0
*Grabs first DBF
lcTable = SYS(2000,"*.DBF")
DO WHILE NOT m.lcTable == ""
  TRY
    USE (m.lcTable) IN 0 ALIAS thisDbf
    USE IN SELECT("thisDbf")
  CATCH TO loError
    ? "Table " + m.lcTable + " is bad."
    lnBadCount = m.lnBadCount + 1
  ENDTRY
  *Grabs NEXT DBF
  lcTable = SYS(2000,"*.DBF",1)
ENDDO
* Tell the user how it went
? IIF(m.lnBadCount = 0, "No errors.", TRANSFORM(m.lnBadCount) + " table(s) could not be opened.")
>I have used this type of solution before in a stand-alone PRG for testing tables (untested code):
>
>
>PUBLIC nCount, gnDbcnumber, gaDatabase[1], gnBadCount
>
>* Redirect our error handler to display relevant information
>ON ERROR DO flagError WITH gaDatabase[nCount, 1]
>
>* Get directory of all DBF files 
>gnDbcnumber = ADIR(gaDatabase, '*.DBF')
>
>* Loop for number of tables to find bad ones
>gnBadCount  = 0
>FOR nCount = 1 TO gnDbcnumber
>    * Try to open it
>    USE (gaDatabase[nCount, 1]) IN 0 ALIAS thisDbf
>
>    * If it opened, close it
>    IF USED("thisDbf")
>        USE IN thisDbf
>    ENDIF
>NEXT
>
>* Restore our error handler
>ON ERROR
>
>* Tell the user how it went
>? IIF(gnBadCount = 0, "No errors", TRANSFORM(gnBadCount) + " tables could not be opened")
>
>
>FUNCTION flagError
>LPARAMETERS pcTable
>    * Note:  you could add additional passed parameters to verify the error message, etc.
>    ? "Table " + pcTable + " is bad"
>    gnBadCount = gnBadCount + 1
>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform