User Tools

Site Tools


scanning_20a_20directory_20_28reading_20directory_20entries_29

Scanning a Directory (Reading Directory Entries)

by JGH, 03-Jan-2008

The Windows API functions FindFirstFile and FindFileNext can be used to scan a directory to find what files and subdirectories exist within it. The BBC BASIC for Windows manual contains some simple demonstration code to do this.

Tree Walking

It is a simple modification to the code to “tree walk”, that is to recursively descend into subdirectories from a specified starting point. The following pair of subroutines do this:

        DEF PROCScan_Dir(path$)
        LOCAL dir%, sh%, res%
        DIM dir% LOCAL 317                              :REM Reserve local space for file info
        IF RIGHT$(path$)<>"\" THEN path$+="\"           :REM Ensure path$ is a path
        SYS "FindFirstFile", path$+"*.*", dir% TO sh%   :REM Initialise search
        IF sh% <> -1 THEN
          REPEAT
            PROCScan_Object                             :REM Process the object we have found
            SYS "FindNextFile", sh%, dir% TO res%       :REM Step to next object
          UNTIL res% = 0                                :REM Loop until no more found
          SYS "FindClose", sh%
        ENDIF
        ENDPROC
 
        DEF PROCScan_Object
        LOCAL file$
        file$=$$(dir%+44)                               :REM Extract object name
        :
        IF file$="." OR file$=".." THEN ENDPROC         :REM Skip 'this' and 'parent' entries
        :
        REM Put code here that processes directories before descending into them
        :
        IF (dir%!0 AND 16) THEN PROCScan_Dir(path$+file$+"\") :REM Recurse into and return
                                                              :REM from subdirectories
        :
        REM Put code here that processes directories after returning up
        REM from them and code that processes files
        REM
        REM dir% contains the object's meta data:
        REM dir%!0  - Object attributes:
        REM          1 = read only
        REM          2 = hidden
        REM          4 = system
        REM         16 = directory
        REM         32 = archive
        REM        Other bits are used for specialised purposes. The values may be combined.
        REM dir%!4  - Time created (LS 32 bits)
        REM dir%!8  - Time created (MS 32 bits)
        REM dir%!12 - Time last accessed (LS 32 bits)
        REM dir%!16 - Time last accessed (MS 32 bits)
        REM dir%!20 - Time last modified (LS 32 bits)
        REM dir%!24 - Time last modified (MS 32 bits)
        REM dir%!28 - File size in bytes (MS 32 bits)
        REM dir%!32 - File size in bytes (LS 32 bits)
        REM $$(dir%+44) - object leaf name
        :
        :
        ENDPROC

For example, calling with PROCScan_Dir(“C:\Program Files”) will recursively scan all the subdirectories within C:\Program Files.

See TreeScan.bbc in the BB4W Yahoo! Group file area.

Technical Note

When scanning a directory you can never guarantee that the list you get is 100% accurate. Some other process may change the directory while your program is going through it. This is not a flaw in Windows, this is a fundamental problem encountered when multiple processes are able to have interleaved access to the same resource and more than one of them is able to change it. Do not worry too much about this, in almost 100% of cases this will not be a problem.

See Also

References

BB4W Manual
BB4W Yahoo! Group thread - note some posts contain errors.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
scanning_20a_20directory_20_28reading_20directory_20entries_29.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1