RekursiveFileSearch (Windows >= Vista)

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

RekursiveFileSearch (Windows >= Vista)

Post by ts-soft »

Only for Vista and higher! Works in ascii and unicode mode, x86 and x64.

Code: Select all

;==================================================================
;
; Library:           RekursiveFileSearch
; Author:            Thomas Schulz (ts-soft)
; based on example by ampsoft (Andreas Miethe) written in XProfan
; Date:              Feb 06, 2012
; Target OS:         Microsoft Windows Vista or higher
; Target Compiler:   PureBasic 4.31 and later
; license:           Free, unrestricted, no warranty whatsoever
;                    credit appreciated but not required
;
; Info at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms679314%28v=vs.85%29.aspx
;
;==================================================================

EnableExplicit

Prototype EnumDirTree(hProcess, RootPath.s, InputPathName.s, *OutputPathBuffer, *Callback, List Files.s())

Define Dbghelp_DLL
Define EnumDirTree.EnumDirTree

Procedure RekursiveFileSearch_Init()
  Shared Dbghelp_DLL
  Shared EnumDirTree
  Dbghelp_DLL = OpenLibrary(#PB_Any, "Dbghelp.dll")
  If Dbghelp_DLL
    CompilerIf #PB_Compiler_Unicode
      EnumDirTree = GetFunction(Dbghelp_DLL, "EnumDirTreeW")
    CompilerElse
      EnumDirTree = GetFunction(Dbghelp_DLL, "EnumDirTree")
    CompilerEndIf
    If EnumDirTree <> 0
      ProcedureReturn SymInitialize_(GetCurrentProcess_(), 0, 0)
    EndIf
  EndIf
EndProcedure

Procedure RekursiveFileSearch_End()
    Shared Dbghelp_DLL
    CloseLibrary(Dbghelp_DLL)
    SymCleanup_(GetCurrentProcess_())
EndProcedure

Procedure RekursiveFileSearch_Callback(FilePath.s, List Files.s())
  AddElement(Files())
  Files() = FilePath
EndProcedure

Procedure RekursiveFileSearch(Path.s, Pattern.s, List Files.s())
  Shared EnumDirTree
  If EnumDirTree
    EnumDirTree(GetCurrentProcess_(), Path, Pattern, 0, @RekursiveFileSearch_Callback(), Files.s())
  EndIf
EndProcedure

; example
NewList MyFiles.s()

If RekursiveFileSearch_Init()
  RekursiveFileSearch(GetHomeDirectory(), "*.txt", MyFiles.s())
  RekursiveFileSearch_End()
EndIf

ForEach MyFiles()
  Debug MyFiles()
Next
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2808
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: RekursiveFileSearch (Windows >= Vista)

Post by Michael Vogel »

··K······ :lol:
Post Reply