Is there a way I can search for all .exe's and get there path names, and their icons ect?
Printable View
Is there a way I can search for all .exe's and get there path names, and their icons ect?
Here's something you could build on, here's Aarons Fastfindfile function which returns an array of files found in a folder + subfolders by a pattern:
Heres the icon extracting code, put this in the same module:Code:Option Explicit
'API Consts, Types and Functions
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private aFileList As Variant
Private nFileCount As Long
Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant
'Initialize the Private File Array and Count, then call the Fast
'File Recursive Function to populate the Array, then return it.
nFileCount = 0
aFileList = Array()
Screen.MousePointer = vbArrowHourglass
Call RecurseFindFiles(sFolder, sPattern)
Screen.MousePointer = vbDefault
FastFindFiles = aFileList
End Function
Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String)
Dim tFD As WIN32_FIND_DATA, lFile As Long, bFound As Long, aSubs() As String, nSubs As Long, sFilename As String
'Make sure the passed folder includes an ending "\"
If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
'Find the First File in the Specified Location
lFile = FindFirstFile(sFolder & "*", tFD)
bFound = lFile
'Loop while a File is found
While bFound
'Get the Filename
sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
'If it's a Folder, add it to the Sub Folders Array
If Left(sFilename, 1) <> "." Then
ReDim Preserve aSubs(nSubs)
aSubs(nSubs) = sFilename
nSubs = nSubs + 1
End If
Else
'If it's a File, compare it to the Pattern for a Match
If sFilename Like UCase(sPattern) Then
'If it matches, add it to the File Array
ReDim Preserve aFileList(nFileCount)
aFileList(nFileCount) = sFolder & sFilename
nFileCount = nFileCount + 1
End If
End If
'Find the Next File, (if there is one).
bFound = FindNextFile(lFile, tFD)
Wend
'Close the API Find Handle
Call FindClose(lFile)
'If there were Sub Folders found, Recurse them too..
If nSubs Then
For nSubs = 0 To UBound(aSubs)
Call RecurseFindFiles(sFolder & aSubs(nSubs), sPattern)
Next
End If
End Sub
And put this where you want to extract the icons, if you want you could add the icons into a imagelistCode:Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Code:Dim item as variant
For each item in FastFindFiles("C:\","*.exe")
DrawIcon Picture1.hdc, 0, 0, ExtractIcon(App.hInstance, item, 0)
Next item
This function does give the list of files(recursively too!) fast enough but it has given out plenty of Dr Watson errors on my client's PCs.
'just a wild thought..no icons though
Code:
Private Sub Form_Load()
File1.Path = "C:\"
File1.Pattern = "*.exe"
End Sub
Private Sub File1_Click()
Dim sFile As String
sFile = File1.FileName
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim msg As String
msg = "The file " & sFile & " was created on: " & fso.GetFile(sFile).DateCreated & vbCrLf
msg = msg & "Last modification to file: " & fso.GetFile(sFile).DateLastModified & vbCrLf
msg = msg & "Last date file was accessed: " & fso.GetFile(sFile).DateLastAccessed & cbcrlf
msg = msg & "Something to think about!"
MsgBox msg, vbInformation, "Just A Thought"
End Sub