Page 1 of 1

RekursiveFileSearch (Windows >= Vista)

Posted: Mon Feb 06, 2012 12:32 am
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

Re: RekursiveFileSearch (Windows >= Vista)

Posted: Mon Feb 06, 2012 8:18 am
by Michael Vogel
··K······ :lol: