Utility for recursive listing files
Posted: Tue Oct 31, 2006 7:44 am
I wanted a utility to recursively search through my windows directories and return the file names. I have it in linux, but not on windows so I built it myself. Nothing spectacular, just short sweet and to the point. I know many don't like globals, but I think they're nice for some apps.
I did some testing, but not thorough. (Added -nr to skip printing directories)
Enjoy!
I did some testing, but not thorough. (Added -nr to skip printing directories)
Enjoy!
Code: Select all
;{ Init
; dtecmeister@gmail.com
Global NewList Files$()
Global Head$ = " "
Global Recursive.b = 1
Global Filter.b = 0
Global FilterPattern$ = "*.*"
Global Sort.b = 0
Global Desc.b = 0
Global Full.b = 0
Global Number.l = 0
Global Level.l = 1
Global PrintDir.b = 1
#DirSeperator = "\"
;} EndInit
Procedure HandleDirectory(TopDir$)
NewList Directories$()
If Full = 1
Head$ = TopDir$
EndIf
If ExamineDirectory(0, TopDir$, "")
ExamineDirectory(1, TopDir$, FilterPattern$)
While NextDirectoryEntry(1)
If (DirectoryEntryName(1) <> ".") And (DirectoryEntryName(1) <> "..") And ((PrintDir = 1) Or (DirectoryEntryType(1) <> #PB_DirectoryEntry_Directory))
If AddElement(Files$())
Files$() = Head$ + DirectoryEntryName(1)
Else
PrintN("*** Add File Element Failed ***")
EndIf
EndIf
Wend
FinishDirectory(1)
While NextDirectoryEntry(0)
If ((DirectoryEntryName(0) <> ".") And (DirectoryEntryName(0) <> "..")) And (Recursive = 1) And (DirectoryEntryType(0) = #PB_DirectoryEntry_Directory)
If AddElement(Directories$())
Directories$() = TopDir$ + DirectoryEntryName(0) + #DirSeperator
Else
PrintN("*** Add Directory Element Failed ***")
EndIf
EndIf
Wend
FinishDirectory(0)
If Sort = 1
SortList(Files$(),Desc)
EndIf
ResetList(Files$())
If (Full = 0) And (PrintDir = 1)
PrintN(TopDir$)
EndIf
ForEach Files$()
PrintN(Files$())
Next
If (Recursive = 1) And ((Level < Number) Or (Number = 0))
Level = Level + 1
ResetList(Directories$())
ForEach Directories$()
ClearList(Files$())
HandleDirectory(Directories$())
Next
ClearList(Directories$())
EndIf
Else
PrintN("*** Invalid directory '" + TopDir$ + "' ***")
EndIf
EndProcedure
OpenConsole()
TopDir$ = ""
;} EndInit
ParamCount = CountProgramParameters()
;Output$ = ""
For X = 1 To ParamCount
; Output$ = Output$ + ProgramParameter() + #CRLF$
Parameter$ = ProgramParameter()
Select Parameter$
Case "-f"
Filter = 1
FilterPattern$ = ProgramParameter()
X = X + 1
Case "-s"
Sort = 1
Case "-sd"
Sort = 1
Desc = 1
Case "-d"
TopDir$ = ProgramParameter()
X = X + 1
Case "-l"
Full = 1
Case "-nr"
Recursive = 0
Case "-n"
Number = Val(ProgramParameter())
X = X + 1
Case "-nd"
PrintDir = 0
Case "-h"
PrintN("Dir_Walk walks through the directory and returns all the file names." + #CRLF$ + "It can filter and recursively parse through the subdirectories.")
PrintN(#CRLF$ + "Usage:")
PrintN("-f FilterPattern (Such as *.gif or *.*) Filter")
PrintN("-s Sort")
PrintN("-sd Sort descending")
PrintN("-d Use directory other than current")
PrintN("-l Full directory printed with every filename")
PrintN("-nr Nonrecursive call Default is Recursive")
PrintN("-n Number of levels to return (All is default)")
PrintN("nd Don't print directory names.")
PrintN("-h Help information")
CloseConsole()
End
Default
PrintN("*** Bad Parameter ***")
EndSelect
Next X
If TopDir$ = ""
TopDir$ = GetCurrentDirectory()
EndIf
If Full = 1
Head$ = TopDir$
EndIf
HandleDirectory(TopDir$)
CloseConsole()