Obtaining a Directory Listing


Use the Dir function to obtain a directory listing. The first argument is a file specification. Although a file or directory may be uniquely identified, file specs (also called wildcards) are allowed. For example, the command Dir ("C:\temp\*.txt") returns a list of all files that have the extension TXT. The second argument specifies attributes, for which there are two valid values: 0 (the default) returns files; set the second argument to 16 to retrieve a list of directories.

Warning  

Most operating systems contain two special directory names , represented by a single period () and a double period (..). A single period references the current directory, and two periods reference the parent directory. These special directories are included in the directory list as returned by the Dir function. If you write a macro that looks at each directory recursively but you don't take these two into consideration, your macro will erroneously run forever.

The first call to Dir starts reading a directory and returns the first file that matches. Each additional call, which takes no arguments, returns the next file that matches.

 sFileName = Dir(path, attribute)      'Get the first one Do While (sFileName <> "")            'While something found   sFileName = Dir()                   'Get the next one Loop 

If the path uniquely identifies a file or directory, only one entry is returned. For example, the command Dir ("C:\tmp\autoexec.bat") returns the single file "autoexec.bat". Less obviously, the command Dir("C:\tmp") returns the single directory "tmp". To determine what a directory contains, the path must either contain a file specifier (C:\tmp\*.*) or the path must contain a trailing path separator (C:\tmp\). The code Listing 7 performs a simple listing of the current directory; it uses the function GetPathSeparator to obtain the path separator in an operating-system-independent way.

Listing 7: ExampleDir is found in the File module in this chapter's source code files as SC07.sxw.
start example
 Sub ExampleDir   Dim s As String                     'Temporary string   Dim sFileName As String             'Last name returned from DIR   Dim i As Integer                    'Count number of dirs and files   Dim sPath                           'Current path with path separator at end   sPath = CurDir & GetPathSeparator() 'With no separator, DIR returns the   sFileName = Dir(sPath, 16)          'directory rather than what it contains   i = 0                               'Initialize the variable   Do While (sFileName <> "")          'While something returned     i = i + 1                         'Count the directories     s = s & "Dir " & CStr(i) &_         " = " & sFileName & CHR$(10)  'Store in string for later printing     sFileName = Dir()                 'Get the next directory name   Loop   i = 0                               'Start counting over for files   sFileName = Dir(sPath, 0)           'Get files this time!   Do While (sFileName <> "")     i = i + 1     s = s & "File " & CStr(i) & " = " & sFileName & " " &_         PrettyFileLen(sPath & sFileName) & CHR$(10)     sFileName = Dir()   Loop   MsgBox s, 0, ConvertToURL(sPath) End Sub 
end example
 

Sample output from Listing 7 is shown in Figure 3 . First, the directories are listed. The first two directories, "." and "..", represent the following:

 file:///home/andy/My%20Documents/OpenOffice/ file:///home/andy/My%20Documents/ 
click to expand
Figure 3: Directory listing of the current directory.

The inclusion of "." and ".." is a common source of problems. A listing of directories contains these two directories, which should usually be ignored.

I have stored a copy of each figure used in this book in one directory. Each figure is named based on the chapter and figure number. For example, OOME02_03.tif identifies the third figure in Chapter 2. The chapter number and the figure number each use two digits. I wrote some macros to manipulate these figures on the disk. For example, one macro determines the number of figures in a chapter by finding the largest-numbered figure (see Listing 8 ).

Listing 8: LargestFigureForChapter is found in the File module in this chapter's source code files as SC07.sxw.
start example
 Function LargestFigureForChapter(sPath$, sBookNm$, iChapter%) As Integer   Dim CurrentMax%      'What is the largest figure number found?   Dim CurrentNum%      'What is the figure number of the current file?   Dim idxOfNum%        'Where does the figure number start in the file name?   Dim sFileName$       'Current file name Dim sWildCard$   REM Assume that the path contains a trailing file separator   REM use something like ...graphics\OOME02_* for the wildcard.   sWildCard = sPath & sBookNm & Format(iChapter, "00") & "_*"   REM Where the figure number starts in the file name   idxOfNum = Len(sBookNm) + 4   sFileName = Dir(sWildCard, 0)                      'First matching file   Do While(sFileName <> "")     CurrentNum = Int(Mid(sFileName, idxOfNum, 2))    'Extract the figure number     IF CurrentNum > CurrentMax Then                  'If the largest seen so far       CurrentMax = CurrentNum                        'Then save it!     End If     sFileName = Dir()                                'Read the next file   Loop   LargestFigureForChapter = CurrentMax               'Return the largest found  End Function 
end example
 
Compatibility  

In Visual Basic for Applications (VBA), if the directory flag (16) is set, directories are returned and files are not. In OOo Basic, both directories and files are returned. The statement CompatibilityMode(True) will fix this when it is supported by OOo Basic in the near future.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net