Ever wondered if you needed the FileType and Executable File Path of a particular file? Well, I had to. So, I started to look into my famous, all time (24 X 7) library – internet. And found out that I can use the FileSystemObject and an API declaration to finish my work. So, here it goes.
For FileType, I used FileSystemObject and File object. And, for Executable File Path, I declared an API Function, FindExecutable using shell.dll library.
Here goes the code:
Private Declare Function FindExecutable Lib “shell32.dll” Alias “FindExecutableA” _ (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long Sub Check() Dim strExecutableProgramPath As String Dim strFilePathAndName As String, strFileType As String Dim objFileSystem As Object, objFile As Object 'Create FileSystemObject Set objFileSystem = CreateObject(“Scripting.FileSystemObject”) strFilePathAndName = Worksheets(“Sheet1”).Range(“B1”).Value 'Create File object and get FileType Set objFile = objFileSystem.GetFile(strFilePathAndName) strFileType = objFile.Type strExecutableProgramPath = FindExecutableProgram(strFilePathAndName) Worksheets(“Sheet1”).Range(“B2”).Value = strFileType Worksheets(“Sheet1”).Range(“B3”).Value = strExecutableProgramPath Set objFile = Nothing Set objFileSystem = Nothing End Sub Private Function FindExecutableProgram(FilePathAndNameWithExtension As String) As String Dim lngReturnValue As Long Dim strBuffer As String strBuffer = Space(260) 'FindExecutable: 'Retrieve the name and handle of the executable, associated with this file 'Returns a value greater than 32 if successful. 'Or a value less than or equal to 32 representing an error. 'Return Value/Code Description '2 SE_ERR_FNF The specified file was not found. '3 SE_ERR_PNF The specified path is invalid. '5 SE_ERR_ACCESSDENIED The specified file cannot be accessed. '8 SE_ERR_OOM The system is out of memory or resources. '31 SE_ERR_NOASSOC There is no association for the specified file type with an executable file. lngReturnValue = FindExecutable(FilePathAndNameWithExtension, vbNullString, strBuffer) If lngReturnValue > 32 Then FindExecutableProgram = Left$(strBuffer, InStr(strBuffer, Chr$(0)) – 1) Else FindExecutableProgram = “No association found!” End If End Function
Reblogged this on Dinesh Ram Kali..