Date: 02-16-2022
Return to Index
created by gbSnippets
'The PowerBASIC Dir$ statement can get file or folders listings, it can
'select which file type to return, and it can retrieve the entire directory
'entry (more than just name) and place the results in a variable or an array.
'Primary code
'Syntax: 'item$ = Dir$(mask$ [, [ONLY] attribute&, TO DirDataVar])
'item$ = Dir$([Next] [TO DirDataVar])
'First call to Dir$ establishes the file/folder pattern
'Successive calls use the same pattern. Returns "" when no more items are found
'Examples:
'To call the first matching file:
NextFile$ = Dir$ ("c:\*.*") 'by default, Dir$ calls %Normal files
NextFile$ = Dir$ ("c:\*.*, %Hidden") '%Hidden + %Normal
NextFile$ = Dir$ ("c:\*.*, ONLY %Hidden ") 'hidden files only
'To call folders:
NextFolder$ = Dir$ ("c:\, %SubDir ") 'subdirectories + %Normal files
NextFolder$ = Dir$ ("c:\, ONLY %SubDir ") 'subdirectories only
'Successive calls
NextFile$ = Dir$ 'uses last pattern
NextFolder$ = Dir$ 'uses last pattern
'To include all file types, as well as folders, use this:
NextFile$ = Dir$ ("c:\*.*, %Normal+%Hidden+%System+%SubDir ") 'normal files
'To save results into a variable or array element:
Local D as DirData 'variable must be a DirData type
Dir$ "*.*" To D
'DirDate Data Type
'The Dir$ function may optionally assign the complete directory entry to
'a DirData type variable using the TO clause as a parameter.
Type DirData
FileAttributes AS DWord
CreationTime AS QUAD
LastAccessTime AS QUAD
LastWriteTime AS QUAD
FileSizeHigh AS DWord
FileSizeLow AS DWord
Reserved0 AS DWord
Reserved1 AS DWord
FileName As AsciiZ * 260
ShortName As AsciiZ * 14
End Type
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10 Put filenames in an array of type DirData
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Get File List", 50,10,100,20
Control Add Button, hDlg, 200,"Get Folder List", 50,40,100,20
Control Add Button, hDlg, 300,"Get Both", 50,70,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local StartPath$, i As Long, EmptyFlag&, sReturn$, file$, folder$, temp$
Dim Results(1000) As DirData '1000 is a reasonable max # of entries
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
'files
StartPath$ = "c:\"
file$ = Dir$(StartPath$, To Results(i)) : Incr i
temp$ = file$
If file$ = "" Then EmptyFlag& = 1 : Exit If
While Len(file$)
file$ = Dir$( Next, To Results(i)) : Incr i
temp$ = temp$ + $crlf + file$
Wend
MsgBox temp$
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
'folders
StartPath$ = "c:\"
folder$ = Dir$(StartPath$, ONLY %SubDir, To Results(i)): Incr i
temp$ = file$
If folder$ = "" Then EmptyFlag& = 1 : Exit If
While Len(folder$)
folder$ = Dir$( Next, To Results(i)) : Incr i
temp$ = temp$ + $crlf + folder$
Wend
MsgBox temp$
End If
If CB.Msg = %WM_Command AND CB.Ctl = 300 AND CB.Ctlmsg = %BN_Clicked Then
'both
StartPath$ = "c:\"
sReturn$ = Dir$(StartPath$, %SubDir, To Results(i)) : Incr i
temp$ = sReturn$
If sReturn$ = "" Then EmptyFlag& = 1 : Exit If
While Len(sReturn$)
sReturn$ = Dir$( Next, To Results(i)) : Incr i
temp$ = temp$ + $crlf + sReturn$
Wend
MsgBox temp$
End If
If EmptyFlag& Then MsgBox "No files found!", %MB_IconExclamation, "File Listing"
End Function
'gbs_00154
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm