Wiki Home

ADir Class


Namespace: Wiki
Hi Paul James

That's a good idea but ADIR produces a 2D array. I think you should support multiple files. I'd also switch to SYS(2000) that way this will never crash when there are too many files for VFP's array limits. -- Mike Yearwood

File Properties as Object Properties

(Added support for Hidden and System files.)
(Added flag handling and defaults for init-parameters) Thanks Thomas!

I got tired of trying to remember the meanings for each member of the ADir() result array, so, I created a class to do the heavy lifting.

The FileProps class validates the file existence and gathers the file properties from the ADir() array. While I was there, I split out some of the extra information I frequently use. Now, I can just refer to file attributes like oFile.cName, oFile.nSize, or oFile.lHidden instead of using a cryptic array reference.

Simply create the "file properties" object, passing the name of the file as a parameter in the Create Object() command, and voila, the object is created and filled at the same time! Too easy.

*************************************************************************
* FileProps class definition
*************************************************************************
Define Class FileProps As Custom
	cName		= Space(0)
	nSize		= 0
	dDate		= {}
	cTime		= Space(0)
	cFlags		= Space(0)
	
	dtDateTime	= {}
	nHour		= 0
	nMin		= 0
	nSec		= 0

	lArchive	= .F.
	lHidden		= .F.
	lReadOnly	= .F.
	lSystem		= .F.
	lDir		= .F.

	Procedure Init
		Lparameters tcFile, tcFlags
		if ! vartype(m.tcFile)=="C" or EMpty(m.tcFile)
                    tcFile = "*.*"
                endif
		if ! vartype(m.tcFlags)=="C"
                    tcFlags = "HS"
                endif
		Return this.GetFileProps(m.tcFile, m.tcFlags)
	EndProc

	
	Function GetFileProps(tcFile, tcFlags)
		Release aFile
		Dimension aFile[1]
		=ADir(afile, m.tcFile, m.tcFlags)  &&1=Name(c), 2=Size(n), 3=Date(d), 4=Time(c), 5=Attr(c)
		
		If vartype(afile[1]) <> 'C'
			=MessageBox("Could not locate file: " + cFile,48,"FileProps Error")
			Return .F.
		Endif
	
		this.cName	=	afile[1]
		this.nSize	=	afile[2]
		this.dDate	=	afile[3]
		this.cTime	=	afile[4]
		this.cFlags	=	afile[5]

		this.dtDateTime	= Ctot(Dtoc(this.dDate)+this.cTime)
		this.nHour	= Hour(this.dtDateTime)
		this.nMin	= Minute(this.dtDateTime)
		this.nSec	= Sec(this.dtDateTime)

		this.lArchive	= "A" $ this.cFlags
		this.lHidden	= "H" $ this.cFlags
		this.lReadOnly	= "R" $ this.cFlags
		this.lSystem	= "S" $ this.cFlags
		this.lDir	= "D" $ this.cFlags
	EndFunc
 EndDefine



********************************************************************************
*** Sample Code
********************************************************************************
Local oMyFile as Object, cMyFile as String
oMyFile = .NULL.
cMyFile = Home() + "vfp" + Left(Transform(Version(5)),1) + ".exe"
Clear

oMyFile = CreateObject("FileProps", cMyFile)

If Type('oMyFile') = 'O' and !IsNull(oMyFile)
	? "Name:   " + oMyFile.cName
	? "Size:   " + Transform(oMyFile.nSize)
	? "Date:   " + Transform(oMyFile.dDate)
	? "cTime:  " + oMyFile.cTime
	? "cFlags: " + oMyFile.cFlags
	? "DT:     " + Transform(oMyFile.dtDateTime)
	? "Hour:   " + Transform(oMyFile.nHour)
	? "Min:    " + Transform(oMyFile.nMin)
	? "Sec:    " + Transform(oMyFile.nSec)
	? "Arch?:  " + Transform(oMyFile.lArchive)
	? "Hidden?:" + Transform(oMyFile.lHidden)
	? "RO?:    " + Transform(oMyFile.lReadOnly)
	? "System?:" + Transform(oMyFile.lSystem)
	? "Dir?:   " + Transform(oMyFile.lDir)
Else
	? "Error Getting File Props"
Endif
Return


Contributors:
Paul James (Find some VFP goodies at: ftp://66.132.144.112/VFP/)
Thomas Ganss (added flag handling and defaults for init-parameters)

Categories:
Category Code Samples
( Topic last updated: 2007.01.19 08:34:31 AM )