Dir() question

New to FreeBASIC? Post your questions here.
Post Reply
MajorDill
Posts: 29
Joined: Sep 07, 2009 6:03
Location: Augusta, Kentucky

Dir() question

Post by MajorDill »

Does DIR() only work in the current directory?

I have tried DIR("c:\prog\data\*.*") but it doesn't seem to work (at least in FreeDOS)

Thanks
fxm
Moderator
Posts: 12159
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dir() question

Post by fxm »

DIR("c:\prog\data\*.*") should return the first file alphabetically (if exists) of the "c:\prog\data\" directory.
If there are only other sub-directories (no files), it returns an empty string.
badidea
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Dir() question

Post by badidea »

If you try DIR("c:\prog\data\*.*", &h37) it should display all items.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Dir() question

Post by MrSwiss »

A more complex Dir() Example, with different *relative* path (than ExePath).
For more details, check comments in code:

Code: Select all

' SearchDirFile.bas -- 2018-04-29, MrSwiss
'
' compile: -s console
'
#Include "file.bi"

Type DirEntry   ' single file's: Size/Attributes/Name (without path)
    As LongInt  fSize
    As ULong    fAttr
    As String   fName
End Type

' test/demo code
Dim As DirEntry FileArr(Any)            ' dynamic array of type<DirEntry>
Dim As String   FileMask, FileN, cDir   ' local variables
Dim As ULong    cntr = 1, attr = &h37, f_attr   ' &h37 = all attributes

ReDim   FileArr(1 To 20)                ' array start size = 20 elements
cDir = CurDir                           ' save original dir
If ChDir(".\testdata\") <> 0 Then       ' relative path (sub-dir)
    Print "ERROR couln't change to source folder" : Sleep : End 1
End If
FileMask = "*.*"                        ' define file search mask

' find all files, matching FileMask
FileN = Dir(FileMask, attr, f_attr)     ' get first file
While Len(FileN) > 0                    ' run until FileN is empty
    With FileArr(cntr)                  ' write results to DirEntry array
        .fSize = FileLen(FileN)         ' size
        .fAttr = f_attr                 ' attributes
        .fName = FileN                  ' name (without path)
    End With
    cntr += 1                           ' increment counter
    ' if array grows to upper bound, increase its size (only every 20th run)
    If cntr Mod 20 = 0 Then ReDim Preserve FileArr(1 To UBound(FileArr) + 20)
    FileN = Dir(f_attr)                 ' attempt to get next file
Wend

' chop array down to useful size (keep only filled part)
If (cntr - 1) < UBound(FileArr) Then ReDim Preserve FileArr(1 To cntr - 1)
Print "FileArr(1 To"; UBound(FileArr); ")"  ' show current array size

For i As UInteger = LBound(FileArr) To UBound(FileArr)
    With FileArr(i)                     ' show arrays content
        Print .fName; Tab(38); .fSize; " Byte(s)"; Tab(60); _
              "&h"; Hex(.fAttr); " Attribute(s)"
    End With
Next
ChDir(cDir)                             ' change back to original dir
Print : Print "press a key, to EXIT ... ";  ' inform user

Sleep                                   ' wait for user action, then EXIT
' test/demo code - end  ' ----- EOF -----
Post Reply