Uncover every file with the FindFirstFile and FindNextFile functions 📂

Started by Theo Gottwald, February 03, 2024, 09:38:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

🔍 Searching through folders on your computer? 👩�💻 Here's a #ThrowbackPost to a detailed guide from 2006! 🗓�

Uncover every file with the FindFirstFile and FindNextFile functions 📂, coupled with the versatile PathMatchSpec wildcard filter. 🌟 You'll get all the deets like filenames 📄, attributes, file sizes 📏, and timestamps ⏲️ of creation, last access, and last modification times. Happy file hunting! 🕵��♂️

#TechTips #FileSearching #Coding #Programming #Developer #ThrowbackThursday

📁🔎👨�💻📅📈🛠�💾⏳📚🎯


#COMPILE EXE '#Win 8.02#
#DIM ALL
#INCLUDE "WIN32API.INC" '#2005/01/27#
'#INCLUDE "SHLWAPI.INC" '#2005/01/14#
DECLARE FUNCTION PathMatchSpec LIB "SHLWAPI.DLL" ALIAS "PathMatchSpecA"(pszFile AS ASCIIZ, pszSpec AS ASCIIZ) AS LONG
 
%ListBox = 101
'______________________________________________________________________________
 
FUNCTION FileTimeToInternationalDate(File_Time AS FILETIME) AS STRING
 LOCAL Sys_Time AS SystemTime
 
 FileTimeToSystemTime File_Time, Sys_Time   
 
 FUNCTION = _
   FORMAT$(Sys_Time.wyear, "0000") & "/" & FORMAT$(Sys_Time.wMonth,  "00") & "/" & _
   FORMAT$(Sys_Time.wDay,    "00") & " " & FORMAT$(Sys_Time.wHour,   "00") & ":" & _
   FORMAT$(Sys_Time.wMinute, "00") & ":" & FORMAT$(Sys_Time.wSecond, "00") & " " & _
   CHOOSE$(Sys_Time.wDayOfWeek + 1, "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday")
 
END FUNCTION
'______________________________________________________________________________
 
FUNCTION FileEnum(Root AS STRING, File() AS WIN32_FIND_DATA, Filter AS STRING) AS LONG
 LOCAL  FileData    AS WIN32_FIND_DATA
 LOCAL  Looper      AS LONG
 LOCAL  hDir        AS DWORD
 LOCAL  FolderCount AS DWORD
 STATIC FileCount   AS DWORD
 STATIC FolderDeep  AS DWORD
 
 INCR FolderDeep
 DIM SubFolder(0 TO 0) AS STRING
 IF ASC(Root, - 1) <> 92 THEN Root = Root & "\" 'Must end with a backlash
 SubFolder(0) = Root
 
 hDir = FindFirstFile(SubFolder(0) & "*", FileData)
 IF hDir <> %INVALID_HANDLE_VALUE THEN
   DO
     IF (FileData.dwFileAttributes AND %FILE_ATTRIBUTE_DIRECTORY) THEN 'It's a folder
       IF ASC(FileData.cFilename) <> 46 THEN 'Refuse "." and  ".."       
         INCR FolderCount
         REDIM PRESERVE SubFolder(0 TO FolderCount) AS STRING
         SubFolder(FolderCount) = SubFolder(0) & FileData.cFilename & "\"
       END IF
     ELSE 'Its a file ...
       IF PathMatchSpec(FileData.cFilename, BYVAL STRPTR(Filter)) THEN  'Check against Filter
         REDIM PRESERVE File(0 TO FileCount)
         File(FileCount) = FileData   
         File(FileCount).cFilename = SubFolder(0) & File(FileCount).cFilename 'Set to full path 
         INCR FileCount 'Zero based array
       END IF
     END IF 
     DIALOG DOEVENTS
   LOOP WHILE FindNextFile(hDir, FileData)
 END IF           
 
 FOR Looper = 1 TO FolderCount               
   FileEnum SubFolder(Looper), File(), Filter 'Function call itself to get sub-folder files
 NEXT                                           
 
 FindClose hDir
 
 DECR FolderDeep
 IF FolderDeep = 0 THEN 'Exiting from first call, function is not in recursive state
   FUNCTION = FileCount
   FileCount = 0  'Reset for subsequent call
 END IF 
 
END FUNCTION
'______________________________________________________________________________
 
CALLBACK FUNCTION DlgProc
 LOCAL Root          AS STRING
 LOCAL Filter        AS STRING
 DIM   File(0 TO 0)  AS WIN32_FIND_DATA
 LOCAL Looper        AS LONG
 LOCAL FileCount     AS DWORD
 LOCAL FileAttribute AS STRING 
 
 
 SELECT CASE CBMSG
 
   CASE %WM_INITDIALOG
     PostMessage CBHNDL, %WM_USER + 400, 0, 0
 
   CASE %WM_USER + 400
     Root = "C:\Program Files"
     Filter = "*.*"
     FileCount = FileEnum(Root, File(), Filter)     
 
     LISTBOX ADD CBHNDL, %ListBox, "Root = " & Root
     LISTBOX ADD CBHNDL, %ListBox, "Filter = " & Filter
     LISTBOX ADD CBHNDL, %ListBox, "Filecount =" & STR$(Filecount)
     LISTBOX ADD CBHNDL, %ListBox, ""
     FOR Looper = 0 TO FileCount - 1
       LISTBOX ADD CBHNDL, %ListBox, File(Looper).cFilename
       LISTBOX ADD CBHNDL, %ListBox, "Short name if different from long filename: " & File(Looper).cAlternateFileName 
       FileAttribute = "Attribute: "
       IF (File(Looper).dwFileAttributes AND %Normal)   THEN FileAttribute = FileAttribute & "Normal "
       IF (File(Looper).dwFileAttributes AND %ReadOnly) THEN FileAttribute = FileAttribute & "ReadOnly "
       IF (File(Looper).dwFileAttributes AND %Hidden)   THEN FileAttribute = FileAttribute & "Hidden "     
       IF (File(Looper).dwFileAttributes AND %System)   THEN FileAttribute = FileAttribute & "Systeml "         
       IF (File(Looper).dwFileAttributes AND %Archive)  THEN FileAttribute = FileAttribute & "Archive "
       LISTBOX ADD CBHNDL, %ListBox, FileAttribute
       LISTBOX ADD CBHNDL, %ListBox, "Size : " & FORMAT$(MAK(QUAD, _
                                   File(Looper).nFileSizeLow, File(Looper).nFileSizeHigh),"###,###") & " bytes"
       LISTBOX ADD CBHNDL, %ListBox, "CreationTime   = " & FileTimeToInternationalDate(File(Looper).ftCreationTime)
       LISTBOX ADD CBHNDL, %ListBox, "LastAccessTime = " & FileTimeToInternationalDate(File(Looper).ftLastAccessTime)
       LISTBOX ADD CBHNDL, %ListBox, "LastWriteTime  = " & FileTimeToInternationalDate(File(Looper).ftLastWriteTime) 
       LISTBOX ADD CBHNDL, %ListBox, "" 
     NEXT
     DIALOG SET TEXT CBHNDL, FORMAT$(FileCount, "#,") & " files found"
 
  END SELECT
 
END FUNCTION
'______________________________________________________________________________   
 
FUNCTION PBMAIN() AS LONG
 LOCAL hDlg AS DWORD
 
 DIALOG NEW %HWND_DESKTOP, "", , , 500, 300, _
        %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO hDlg
 SetClassLong hDlg, %GCL_HICON, LoadIcon(BYVAL %NULL, BYVAL %IDI_INFORMATION)
 
 CONTROL ADD LISTBOX, hDlg, %ListBox, , 5,  5, 490, 300, %LBS_NOTIFY OR _
        %WS_TABSTOP OR %WS_VSCROLL OR %WS_HSCROLL, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT
 CONTROL SEND hDlg, %ListBox, %LB_SETHORIZONTALEXTENT, 2000, 0
 
 DIALOG SHOW MODAL hDlg CALL DlgProc
 
END FUNCTION
'______________________________________________________________________________