Announcement

Collapse
No announcement yet.

Directory Code Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Directory Code Question

    Greetings All!

    I'm working on a crazy program and I've run into a problem. I'm using Hans Lunsing's PBLFN32 package and PowerBasic 3.5 for DOS. Whenever I bring the directory list into the array "DirList" I get some useless files that I don't need. I've extracted the code segment from the program and edited it for your perusal:

    Code:
    $INCLUDE "lngfnam2.bi"
    $LINK "lngfnam2.pbu"
    
    'Preinitialize data array
    File$ = LfnDirFirst$( "*.*", &h77 )
    I& = 0
    WHILE len( File$ ) > 0
      I& = I& + 1
      File$ = LfnDirNext$
    WEND
    
    LI& = I& - 2
    DIM DirList$( I& )
    
    OPEN "dir.lst" for OUTPUT as #1
    
    'Enter data into the array
    I& = 0
    File$ = LfnDirFirst$( "*.*", &h77 )
    WHILE len( File$ ) > 0
      DirList$( I& ) = File$
      PRINT #1, File$
      File$ = LfnDirNext$
      I& = I& + 1
    WEND
    
    CLEAR
    CLOSE #1
    END
    ...as you can see, it has two entries of "." And ".." that I can easily parse out; but, is there any easy way to "touch" a file to see if it's accessible?

    Also, on another note, is there a better way to create and fill the array than the one that I used? I use this method twice and the second time takes a bit longer as I'm inputting a large delimited file of variable records. I'll probably delve into some more questions as they crop up.

    Don Ewald
    mailto:[email protected][email protected]</A>
    Donnie Ewald
    [email protected]

  • #2
    Donald, why do you need to go through the list twice?

    Try this method (untested, written on-line):
    Code:
    ...
    OPEN "dir.lst" for OUTPUT as #1
    DIM DYNAMIC DirList$(0) 
    i& = 0 
    File$ = LfnDirFirst$("*.*", &h77)
    WHILE LEN(File$)
      PRINT #1, File$
      IF i& MOD 100 = 0 THEN REDIM PRESERVE DirList$(i& + 100)
      DirList$(i%) = File$
      INCR i&
      File$ = LfnDirNext$
    WEND
    DECR i& ' valid subscripts will be (0) to (i& - 1)
    ' If i& < 0 then there were no files matching the spec
    REDIM PRESERVE DirList$(MAX(0,i&))
    CLOSE #1
    ...

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      The "." and ".." entries are pseudo-directories. You'll get them back any time
      you ask for a list of files including directories, except in the root directory.
      The "." indicates the current directory, and ".." the directory above. If you
      don't want to see these entries, you need to filter them out. Be sure to compare
      specifically with "." and "..", though-- it's quite legal for a long filename to
      begin with a ".".

      You may not want to use the search mask "*.*". With the 8.3 search mechanism,
      this returns all files. With LFN search, though, this may return only files with
      "." in the name-- the "." is not a special-case character in quite the way it
      used to be. To return all files using an LFN search pattern, just "*" is fine.


      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment


      • #4
        Greetings Lance and Tom!

        Lance:
        why do you need to go through the list twice?
        I don't. I just wasn't aware of a better way. Thankfully, you pointed out a much easier method. Your code compiled pretty nicely and only had one error that I saw:

        Code:
        DirList$(i%) = File$
        'should be:
        DirList$(i&) = File$
        ...I've been able to get a fragile grasp on your code; but, what does this line do:

        Code:
        IF i& MOD 100 = 0 THEN REDIM PRESERVE DirList$(i& + 100)
        Now the suggestion of Tom to change the wildcard from "*.*" to "*" was a great boon. I wasn't aware of the difference and I doubt the problem would ever crop up; but, I'm glad to find it now than later.

        Many thanks for the suggestions.

        Don Ewald
        mailto:[email protected][email protected]less.com</A>

        Donnie Ewald
        [email protected]

        Comment


        • #5
          Lance is preallocating array space in blocks of 100 elements, for efficiency
          reasons. That's what the IF i& MOD 100 line takes care of.


          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment

          Working...
          X