For Programmers: Free Programming Magazines  


Home > Archive > Clipper > August 2007 > Field Name in Multiple DBF's









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Field Name in Multiple DBF's
Donald Leask

2007-08-10, 7:55 am

Hi All

I need to know which files (of a group of about thirty or so) contain a
particular field named CODE?

How can I do this easily?


--
Donald Leask
Johannesburg, South Africa



Richard Bos

2007-08-10, 7:55 am

"Donald Leask" <donl@mweb.co.za> wrote:

> I need to know which files (of a group of about thirty or so) contain a
> particular field named CODE?
>
> How can I do this easily?


Use each database (shared, read-only, by preference) in turn; ask for
its dbstruct(); run through the returned array to see whether the first
member of its members is "CODE". I don't think there is a one-shot
solution simpler than that.

Richard
SEI

2007-08-10, 7:55 am

Hello,

Not simpler, just a different approach :-)

.................
private i:=0, j:=0, cName:="CODE", lExist:=.F.
private aFile:={"myFile1", "myFile2",...}

select 0
for i:=1 to len(aFile)
use (aFile[i])
lExist:=.F.
for j:=1 to FCount()
if FieldPos(cName) > 0
lExist:=.T.
exit
endif
if lExist == .T.
//........... here goes your code
//........... what to do if the field is present
endif
next
use
next
...............


Ella


On 10 Aug, 14:09, "Donald Leask" <d...@mweb.co.za> wrote:
> Hi All
>
> I need to know which files (of a group of about thirty or so) contain a
> particular field named CODE?
>
> How can I do this easily?
>
> --
> Donald Leask
> Johannesburg, South Africa



Ray Marron

2007-08-10, 6:55 pm

On Aug 10, 5:46 am, SEI <seis...@citromail.hu> wrote:
> Hello,
>
> Not simpler, just a different approach :-)
>
> ................
> private i:=0, j:=0, cName:="CODE", lExist:=.F.
> private aFile:={"myFile1", "myFile2",...}
>
> select 0
> for i:=1 to len(aFile)
> use (aFile[i])
> lExist:=.F.
> for j:=1 to FCount()
> if FieldPos(cName) > 0
> lExist:=.T.
> exit
> endif
> if lExist == .T.
> //........... here goes your code
> //........... what to do if the field is present
> endif
> next
> use
> next


I guess by "different approach" you mean "doesn't work". ;)
The whole "for j" loop is not only unnecessary, but your "if lExist"
code will never be reached because of the earlier "exit".

for i := 1 to len(aFile)
use (aFile[i])
if FieldPos(cName) > 0
// do stuff here...
endif
use
next

--
Ray Marron

E. Fridman

2007-08-10, 6:55 pm

Richard,
[color=darkred]

I don't think there is a one-shot solution simpler than that. <<

I believe __ FIELDPOS("CODE") > 0 __ is simpler.

SEI

2007-08-10, 6:55 pm

Oops, sorry

...........
private i:=0, cName:="CODE"
private aFiles:={"myFile1.dbf", "myFile2.dbf", ...}
............
select 0
for i:=1 to len(aFiles)
use (aFiles[i])
if FieldPos(cName) > 0
// ... your code if the field exists
endif
use
next
............

On 10 Aug, 14:09, "Donald Leask" <d...@mweb.co.za> wrote:
> Hi All
>
> I need to know which files (of a group of about thirty or so) contain a
> particular field named CODE?
>
> How can I do this easily?
>
> --
> Donald Leask
> Johannesburg, South Africa



Encetenup

2007-08-12, 10:09 pm

http://www.videomoviesonline.com/Me...eg?movie=726648
Donald Leask

2007-08-20, 3:55 am

"Donald Leask" <donl@mweb.co.za> wrote in message
news:1186744305.49936@wblv-ip-nnrp-1...
> Hi All
>
> I need to know which files (of a group of about thirty or so) contain a
> particular field named CODE?



Thanks everybody but not quite what I meant - I have over a hundred DBF
files and I want to know (easily?) which files contain the field named CODE
without opening everyone with say DBU and checking the field names - just
lazy I guess. ;-)

Donald.


Stephen Quinn

2007-08-20, 3:55 am

Donald

> Thanks everybody but not quite what I meant - I have over a hundred DBF
> files and I want to know (easily?) which files contain the field named
> CODE without opening everyone with say DBU and checking the field names -
> just lazy I guess. ;-)


A simple loop would do it

aList2 := {}
aList1 := Directory( '*.DBF' )
FOR i := 1 TO Len( aList1 )

USE ( aList1[ i ][ 1 ] ) NEW
IF Fieldpos( 'CODE' ) > 0
aadd( aList2, aList1[ i ][ 1 ] )
ENDIF
CLOSE
NEXT
// do whatever with the 2nd list

CYA
Steve


rl

2007-08-20, 3:55 am

Hi Donald,

Here is the source for a utility I wrote in '95 to search all the dbfs
matching a given wildcard (a*.dbf, *.dbf, etc) for a given fieldname.
Eg. DBS *.dbf CODE == would list all DBFs which contain a field called
CODE. Omit the == from the command line and it will return all DBFs
which contain a field where 'CODE' is part of the fieldname. I've
pasted the source files DBS.PRG and DBS.LNK below. Hope this helps.

Richard

// BOF DBS.PRG

/*
DBS.prg : DBS() : scan specified tables and list structure
of
those which have fields with names
matching those specified

compile : clipper /b/m/n/w/es2/p

link : blinker @dbs.lnk

*/


#include 'Common.ch'
#include 'Inkey.ch'
#include 'SimpleIO.ch'
#include 'Directry.ch'
#include 'DbStruct.ch'


#define CRLF Chr(13) + Chr(10)



function DBS ( p1, p2, p3, p4, p5, p6, p7, p8, p9 )

local aFiles, nCntr, nKey, cFldName, cFiSpec, aParms, nPCount, ;
aFldSpec, lExact

lExact := .F.

aParms := AstripRef( { p1, p2, p3, p4, p5, p6, p7, p8, p9 }, NIL)

nPCount := Len(aParms)

do case
case nPCount == 1
cFiSpec := '*'
aFldSpec := { aParms [1] }

case nPCount == 0
? 'Syntax: DBS <Filespec> <Field1> [ .. <FieldN> ] [ == ]'

return ( NIL )

otherwise
cFiSpec := aParms [1]
aFldSpec := { }

for nCntr := 2 to Len(aParms)
if Upper(aParms [nCntr]) == '=='
lExact := .T.

else
Aadd(aFldSpec, Upper(aParms [nCntr]))

endif

next

endcase

if (Fext(cFiSpec) == '.')
cFiSpec += 'dbf'

else
if ( Fext(cFiSpec) == "" )
cFiSpec += '.dbf'

endif

endif

aFiles := Asort( Directory(cFiSpec), NIL, NIL, ;
{ | aFileA, aFileB | aFileA [F_NAME] < aFileB
[F_NAME] } ;
)

for nCntr:= 1 to Len(aFiles)
nKey:= Nextkey()

if (nKey == K_ESC .or. nKey == K_CTRL_C)
?
? Chr(7) + '*** Terminated by user ***'

exit

else
if ( nKey == K_CTRL_S )
Inkey(0)

endif

endif

use (aFiles [nCntr][F_NAME]) shared

Outstru(aFldSpec, ; // list of fields to
match
aFiles [nCntr][F_NAME], ; // filename
DbStruct(), ; // structure array
lExact ; // exact or partial
match
)

next

dbCloseAll()

return ( NIL )


static function OutStru ( aFldSpec, cFilename, aStru, lExact )

local nFldCntr, cType, lIsMatch

if ! FieldMatch(aFldSpec, aStru, lExact)
return ( NIL )

endif

?
?
?
?? ' ' + cFilename
?
?? ' ' + Replicate('-', Len(cFilename)) + CRLF
?
?? ' Field Name Type Len Dec' + CRLF + CRLF

for nFldCntr := 1 to Len(aStru)
// does this field match?
lIsMatch := .F.

if lExact
if Ascan( aFldSpec, ;
;
{ | cSpec | cSpec == aStru [nFldCntr]
[DBS_NAME] } ;
) > 0

lIsMatch := .T.

endif

else
if Ascan( aFldSpec, ;
;
{ | cSpec | cSpec $ aStru [nFldCntr]
[DBS_NAME] } ;
) > 0

lIsMatch := .T.

endif

endif

// field name
? ' ' + Str(nFldCntr, 5, 0) + ' ' + ;
Padr(aStru [nFldCntr][DBS_NAME], 10) + ' '

// type
?? (cType := aStru [nFldCntr][DBS_TYPE]) + ' '

// length
?? Str(aStru [nFldCntr][DBS_LEN], 3, 0) + ' '

// decimals
?? IIf(cType == 'N', Str(aStru [nFldCntr][DBS_DEC], 3, 0), '
') + ;
' ' + IIf(lIsMatch, '<---', ' ')

next

?
?? Chr(12)

return ( NIL )



static function FieldMatch ( aFldSpec, aStru, lExact )

local nFldCnt

for nFldCnt := 1 to Len(aStru)
if lExact
if Ascan( aFldSpec, ;
;
{ | cSpec | cSpec == aStru [nFldCnt]
[DBS_NAME] } ;
) > 0

return ( .T. )

endif

else
if Ascan( aFldSpec, ;
;
{ | cSpec | cSpec $ aStru [nFldCnt]
[DBS_NAME] } ;
) > 0

return ( .T. )

endif

endif

next

return ( .F. )



function AstripRef ( aArray, xVal )

local nCntr

for nCntr := Len(aArray) to 1 step -1
if Valtype(xVal) == "B"
if Eval(xVal, aArray [nCntr], nCntr)
Adel(aArray, nCntr)
Asize(aArray, Len(aArray) - 1)

endif

else
if XCompare(xVal, aArray [nCntr])

Adel(aArray, nCntr)
Asize(aArray, Len(aArray) - 1)

endif

endif

next

return ( aArray )



function Xcompare ( u1, u2 )

do case
case ! (Valtype(u1) == Valtype(u2)) // both arguments of same
data type?
return ( .F. )

case Valtype(u1) == "A" // arrays
return ( Acompare(u1, u2) )

case Valtype(u1) == "O"
return ( OCompare(u1, u2) )

end

return ( u1 == u2 )



function FExt ( cSpec )

local nPoint := Rat(".", cSpec := Rtrim(cSpec))

if nPoint > 0 .and. nPoint >= Len(cSpec) - 3
return ( Right(cSpec, 1 + (Len(cSpec) - nPoint)) )

end

return ( "" )



#define nCntr aV [ 1]
#define nMax aV [ 2]

function Acompare ( a1, a2 )

local aV [2]

if ! (Len(a1) == Len(a2))
return ( .F. )

endif

nMax := Len(a1)

for nCntr := 1 to nMax
do case
case (! (Valtype(a1 [nCntr]) == Valtype(a2 [nCntr])))
return ( .F. )

case Valtype(a1 [nCntr]) == 'A'
if (! Acompare(a1 [nCntr], a2 [nCntr]))
return ( .F. )

endif

case Valtype(a1 [nCntr]) == 'O'
if (! Ocompare(a1 [nCntr], a2 [nCntr]))
return ( .F. )

endif

case (! (a1 [nCntr] == a2 [nCntr]))
return ( .F. )

endcase

next

return ( .T. )

#undef nCntr
#undef nMax



function Ocompare ( o1, o2 )

local nCntr

if ! (Len(o1) == Len(o2))
return ( .F. )

end

for nCntr := 1 to Len(o1)
do case
case (! (Valtype(o1 [nCntr]) == Valtype(o2 [nCntr])))
return ( .F. )

case Valtype(o1 [nCntr]) == "A"
if (! Acompare(o1 [nCntr], o2 [nCntr]))
return ( .F. )

end

case Valtype(o1 [nCntr]) == "O"
if (! Ocompare(o1 [nCntr], o2 [nCntr]))
return ( .F. )

end

case (! (o1 [nCntr] == o2 [nCntr]))
return ( .F. )

end

next

return ( .T. )

// EOF DBS.PRG

#BOF DBS.LNK

NODEFLIB
NOBELL
BLINKER CACHE XMS 100%,50
BLINKER INCREMENTAL OFF
OUTPUT dbs

BEGINAREA
FILE dbs

ENDAREA

@cl520max.lnk

#EOF DBS.LNK


On Aug 20, 4:42 pm, "Donald Leask" <d...@mweb.co.za> wrote:
> "Donald Leask" <d...@mweb.co.za> wrote in message
>
> news:1186744305.49936@wblv-ip-nnrp-1...
>
>
>
> Thanks everybody but not quite what I meant - I have over a hundred DBF
> files and I want to know (easily?) which files contain the field named CODE
> without opening everyone with say DBU and checking the field names - just
> lazy I guess. ;-)
>
> Donald.



Donald Leask

2007-08-20, 3:55 am

Thanks Richard

Could I compile this with Clipper 5.3?

Donald.


"rl" <rl@raluke.com> wrote in message
news:1187596121.520489.198680@x40g2000prg.googlegroups.com...
> Hi Donald,
>
> Here is the source for a utility I wrote in '95 to search all the dbfs
> matching a given wildcard (a*.dbf, *.dbf, etc) for a given fieldname.
> Eg. DBS *.dbf CODE == would list all DBFs which contain a field called
> CODE. Omit the == from the command line and it will return all DBFs
> which contain a field where 'CODE' is part of the fieldname. I've
> pasted the source files DBS.PRG and DBS.LNK below. Hope this helps.
>
> Richard
>
> // BOF DBS.PRG
>
> /*
> DBS.prg : DBS() : scan specified tables and list structure
> of
> those which have fields with names
> matching those specified
>
> compile : clipper /b/m/n/w/es2/p
>
> link : blinker @dbs.lnk
>
> */
>
>
> #include 'Common.ch'
> #include 'Inkey.ch'
> #include 'SimpleIO.ch'
> #include 'Directry.ch'
> #include 'DbStruct.ch'
>
>
> #define CRLF Chr(13) + Chr(10)
>
>
>
> function DBS ( p1, p2, p3, p4, p5, p6, p7, p8, p9 )
>
> local aFiles, nCntr, nKey, cFldName, cFiSpec, aParms, nPCount, ;
> aFldSpec, lExact
>
> lExact := .F.
>
> aParms := AstripRef( { p1, p2, p3, p4, p5, p6, p7, p8, p9 }, NIL)
>
> nPCount := Len(aParms)
>
> do case
> case nPCount == 1
> cFiSpec := '*'
> aFldSpec := { aParms [1] }
>
> case nPCount == 0
> ? 'Syntax: DBS <Filespec> <Field1> [ .. <FieldN> ] [ == ]'
>
> return ( NIL )
>
> otherwise
> cFiSpec := aParms [1]
> aFldSpec := { }
>
> for nCntr := 2 to Len(aParms)
> if Upper(aParms [nCntr]) == '=='
> lExact := .T.
>
> else
> Aadd(aFldSpec, Upper(aParms [nCntr]))
>
> endif
>
> next
>
> endcase
>
> if (Fext(cFiSpec) == '.')
> cFiSpec += 'dbf'
>
> else
> if ( Fext(cFiSpec) == "" )
> cFiSpec += '.dbf'
>
> endif
>
> endif
>
> aFiles := Asort( Directory(cFiSpec), NIL, NIL, ;
> { | aFileA, aFileB | aFileA [F_NAME] < aFileB
> [F_NAME] } ;
> )
>
> for nCntr:= 1 to Len(aFiles)
> nKey:= Nextkey()
>
> if (nKey == K_ESC .or. nKey == K_CTRL_C)
> ?
> ? Chr(7) + '*** Terminated by user ***'
>
> exit
>
> else
> if ( nKey == K_CTRL_S )
> Inkey(0)
>
> endif
>
> endif
>
> use (aFiles [nCntr][F_NAME]) shared
>
> Outstru(aFldSpec, ; // list of fields to
> match
> aFiles [nCntr][F_NAME], ; // filename
> DbStruct(), ; // structure array
> lExact ; // exact or partial
> match
> )
>
> next
>
> dbCloseAll()
>
> return ( NIL )
>
>
> static function OutStru ( aFldSpec, cFilename, aStru, lExact )
>
> local nFldCntr, cType, lIsMatch
>
> if ! FieldMatch(aFldSpec, aStru, lExact)
> return ( NIL )
>
> endif
>
> ?
> ?
> ?
> ?? ' ' + cFilename
> ?
> ?? ' ' + Replicate('-', Len(cFilename)) + CRLF
> ?
> ?? ' Field Name Type Len Dec' + CRLF + CRLF
>
> for nFldCntr := 1 to Len(aStru)
> // does this field match?
> lIsMatch := .F.
>
> if lExact
> if Ascan( aFldSpec, ;
> ;
> { | cSpec | cSpec == aStru [nFldCntr]
> [DBS_NAME] } ;
> ) > 0
>
> lIsMatch := .T.
>
> endif
>
> else
> if Ascan( aFldSpec, ;
> ;
> { | cSpec | cSpec $ aStru [nFldCntr]
> [DBS_NAME] } ;
> ) > 0
>
> lIsMatch := .T.
>
> endif
>
> endif
>
> // field name
> ? ' ' + Str(nFldCntr, 5, 0) + ' ' + ;
> Padr(aStru [nFldCntr][DBS_NAME], 10) + ' '
>
> // type
> ?? (cType := aStru [nFldCntr][DBS_TYPE]) + ' '
>
> // length
> ?? Str(aStru [nFldCntr][DBS_LEN], 3, 0) + ' '
>
> // decimals
> ?? IIf(cType == 'N', Str(aStru [nFldCntr][DBS_DEC], 3, 0), '
> ') + ;
> ' ' + IIf(lIsMatch, '<---', ' ')
>
> next
>
> ?
> ?? Chr(12)
>
> return ( NIL )
>
>
>
> static function FieldMatch ( aFldSpec, aStru, lExact )
>
> local nFldCnt
>
> for nFldCnt := 1 to Len(aStru)
> if lExact
> if Ascan( aFldSpec, ;
> ;
> { | cSpec | cSpec == aStru [nFldCnt]
> [DBS_NAME] } ;
> ) > 0
>
> return ( .T. )
>
> endif
>
> else
> if Ascan( aFldSpec, ;
> ;
> { | cSpec | cSpec $ aStru [nFldCnt]
> [DBS_NAME] } ;
> ) > 0
>
> return ( .T. )
>
> endif
>
> endif
>
> next
>
> return ( .F. )
>
>
>
> function AstripRef ( aArray, xVal )
>
> local nCntr
>
> for nCntr := Len(aArray) to 1 step -1
> if Valtype(xVal) == "B"
> if Eval(xVal, aArray [nCntr], nCntr)
> Adel(aArray, nCntr)
> Asize(aArray, Len(aArray) - 1)
>
> endif
>
> else
> if XCompare(xVal, aArray [nCntr])
>
> Adel(aArray, nCntr)
> Asize(aArray, Len(aArray) - 1)
>
> endif
>
> endif
>
> next
>
> return ( aArray )
>
>
>
> function Xcompare ( u1, u2 )
>
> do case
> case ! (Valtype(u1) == Valtype(u2)) // both arguments of same
> data type?
> return ( .F. )
>
> case Valtype(u1) == "A" // arrays
> return ( Acompare(u1, u2) )
>
> case Valtype(u1) == "O"
> return ( OCompare(u1, u2) )
>
> end
>
> return ( u1 == u2 )
>
>
>
> function FExt ( cSpec )
>
> local nPoint := Rat(".", cSpec := Rtrim(cSpec))
>
> if nPoint > 0 .and. nPoint >= Len(cSpec) - 3
> return ( Right(cSpec, 1 + (Len(cSpec) - nPoint)) )
>
> end
>
> return ( "" )
>
>
>
> #define nCntr aV [ 1]
> #define nMax aV [ 2]
>
> function Acompare ( a1, a2 )
>
> local aV [2]
>
> if ! (Len(a1) == Len(a2))
> return ( .F. )
>
> endif
>
> nMax := Len(a1)
>
> for nCntr := 1 to nMax
> do case
> case (! (Valtype(a1 [nCntr]) == Valtype(a2 [nCntr])))
> return ( .F. )
>
> case Valtype(a1 [nCntr]) == 'A'
> if (! Acompare(a1 [nCntr], a2 [nCntr]))
> return ( .F. )
>
> endif
>
> case Valtype(a1 [nCntr]) == 'O'
> if (! Ocompare(a1 [nCntr], a2 [nCntr]))
> return ( .F. )
>
> endif
>
> case (! (a1 [nCntr] == a2 [nCntr]))
> return ( .F. )
>
> endcase
>
> next
>
> return ( .T. )
>
> #undef nCntr
> #undef nMax
>
>
>
> function Ocompare ( o1, o2 )
>
> local nCntr
>
> if ! (Len(o1) == Len(o2))
> return ( .F. )
>
> end
>
> for nCntr := 1 to Len(o1)
> do case
> case (! (Valtype(o1 [nCntr]) == Valtype(o2 [nCntr])))
> return ( .F. )
>
> case Valtype(o1 [nCntr]) == "A"
> if (! Acompare(o1 [nCntr], o2 [nCntr]))
> return ( .F. )
>
> end
>
> case Valtype(o1 [nCntr]) == "O"
> if (! Ocompare(o1 [nCntr], o2 [nCntr]))
> return ( .F. )
>
> end
>
> case (! (o1 [nCntr] == o2 [nCntr]))
> return ( .F. )
>
> end
>
> next
>
> return ( .T. )
>
> // EOF DBS.PRG
>
> #BOF DBS.LNK
>
> NODEFLIB
> NOBELL
> BLINKER CACHE XMS 100%,50
> BLINKER INCREMENTAL OFF
> OUTPUT dbs
>
> BEGINAREA
> FILE dbs
>
> ENDAREA
>
> @cl520max.lnk
>
> #EOF DBS.LNK
>
>
> On Aug 20, 4:42 pm, "Donald Leask" <d...@mweb.co.za> wrote:
>
>



rl

2007-08-20, 6:55 pm

Yes, I think so. I've never used 5.3. Only 5.2d. But can't see why it
wouldn't work. A few of the lines of code in the pasted text may have
become split across two lines but those should be easily identified
and fixed. I can email the source (or even the exe) to you if you send
me an email to moc.elpitlumr@lr (backwards).

On Aug 20, 6:28 pm, "Donald Leask" <d...@mweb.co.za> wrote:[color=darkred]
> Thanks Richard
>
> Could I compile this with Clipper 5.3?
>
> Donald.
>
> "rl" <r...@raluke.com> wrote in message
>
> news:1187596121.520489.198680@x40g2000prg.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com