ADir() Function

/*
ADIR()*

Fill a series of arrays with directory information
——————————————————————————
Syntax :

ADIR( [<cFilespec>],
      [<aFilenames>],
      [<aSizes>],
      [<aDates>],
      [<aTimes>],
      [<aAttributes>]) --> nFiles

Arguments :

<cFilespec> is the path specification of files to include in the scan of the DEFAULT directory. It is a standard file specification that can include the wildcard characters * and ?, as well as a drive and path reference. If omitted, the default specification is *.*.

<aFilenames> is the array to fill with the file names matching <cFilespec>. Each element contains the file name and extension as a character string in all uppercase letters.

<aSizes> is the array to fill with the sizes of the corresponding files in the <aFilenames> array. Each element is a numeric data type.

<aDates> is the array to fill with the dates of the corresponding files in the <aFilenames> array. Each element is a date data type.

<aTimes> is the array to fill with the times of the corresponding files in the <aFilenames> array. Each element filled contains a character string of the form: hh:mm:ss.

<aAttributes> is the array to fill with attributes of the corresponding files in the <aFilenames> array. Each element is a character string. If <aAttributes> is specified, hidden, system, and directory files are included as well as normal files. If <aAttributes> is not specified, only normal files are included.

Returns :

ADIR() returns the number of files matching the directory skeleton described in <cFilespec>.

Description :

ADIR() is an array function that performs two basic operations. First, it returns the number of files matching the file specification. Second, it fills a series of arrays with file names, sizes, dates, times, and attributes.

ADIR() is a compatibility function and therefore not recommended. It is superseded by the DIRECTORY() function which returns all file information in a multidimensional array.

Notes :

Directories: If you specify the <aAttributes> argument and <cFilespec> is *.*, directories will be included in <aFilenames>. In the <aAttributes> array, directories are indicated with an attribute value of “D”. If ADIR() is executed within a subdirectory, the first two entries of the <aFilenames> array are “.” and “..”, the parent and current directory aliases. The date and time of last update are reported for directories, but the size of a directory is always zero.

Examples :

This example creates an array to hold the names of all .txt files in the current DEFAULT directory, then uses AEVAL() to list them to the console:

 LOCAL aFiles[ADIR("*.TXT")]
 ADIR("*.TXT", aFiles)
 AEVAL(aFiles, { |element| QOUT(element) } )

Remarks :

ADIR() is an obsolete function with some drawbacks.

– Requires five separate arrays for each file specification : name, size, date, time and attrribute.

– Each of these array must be length of exact file count. Return value of function is number of files, so you need call this function twice : first for obtain number of files and second for actual file info.

– Including directories is depend of existence of <aAttributes>, and <aAttributes> require been length of exact file count.

So if you want to see directories you need call this function triple ( see below sample ).

As a result, as indicated in “Description” above, using DIRECTORY() instead of ADIR() may be more convenient.

*/

PROC Main()
LOCAL cFileSpec := "C:\hb32\*.*"
LOCAL aFileNames := ARRAY( ADIR( cFileSpec ) )
LOCAL nFileCount := LEN( aFileNames )
LOCAL aFileSizes := ARRAY( nFileCount ),;
      aFileDates := ARRAY( nFileCount ),;
      aFileTimes := ARRAY( nFileCount ),;
      aFileAttrb := ARRAY( nFileCount )
LOCAL nMaxFNmLen := 0
SET CENT ON
SET DATE GERM
nFileCount := ADIR( cFileSpec, aFileNames, aFileSizes,;
                    aFileDates, aFileTimes, aFileAttrb )
ASIZE( aFileNames, nFileCount )
ASIZE( aFileSizes, nFileCount )
ASIZE( aFileDates, nFileCount )
ASIZE( aFileTimes, nFileCount )
ASIZE( aFileAttrb, nFileCount )
ADIR( cFileSpec, aFileNames, aFileSizes, aFileDates,;
                 aFileTimes, aFileAttrb )
AEVAL( aFileNames, { | c1FName | nMaxFNmLen := ;
                      MAX( nMaxFNmLen, LEN( c1FName ) ) } )
AEVAL( aFileNames, { | c1FName, i1 | ;
                       QOUT( PADR( c1FName, nMaxFNmLen + 1 ),;
                       TRAN( aFileSizes[ i1 ], "999,999,999" ),;
                             aFileDates[ i1 ],;
                             aFileTimes[ i1 ],;
                             aFileAttrb[ i1] ) } )
?
WAIT "End of ADIR.prg"
RETU // ADir.Main()

/*

Result of ADIR.prg :
 .                   0  13.09.2012 02:07:15 D
 ..                  0  13.09.2012 02:07:15 D
 addons              0  13.09.2012 02:07:07 D
 bin                 0  13.09.2012 02:07:12 D
 ChangeLog   8,848,829  12.09.2012 01:59:44 A
 comp                0  13.09.2012 02:07:12 D
 contrib             0  13.09.2012 02:07:16 D
 COPYING        47,456  12.09.2012 01:59:44 A
 doc                 0  13.09.2012 02:07:07 D
 extras              0  13.09.2012 02:07:15 D
 include             0  13.09.2012 02:07:07 D
 INSTALL        79,968  12.09.2012 01:59:44 A
 lib                 0  13.09.2012 02:07:14 D
 NEWS          108,058  12.09.2012 01:59:44 A
 RELNOTES        9,832  18.04.2012 04:00:10 A
 tests               0  13.09.2012 02:07:15 D
 TODO            2,924  12.09.2012 01:59:44 A
 uninstall.exe 104,646  13.09.2012 02:07:12 A
End of ADIR.prg

*/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.