Visual Basic Classic
--
Questions
--
Followers
Top Experts
the following code retrieves the name and type of the file - ie "Text File" ... words of that ilk ...
Sub ShowFileSize(filespec)
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(filespec)
s = UCase(f.Name) & " is a " & f.Type
MsgBox s, 0, "File Size Info"
End Sub
How do I find the files type using API ?
MTIA
DWE
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpfilename As String) As Long
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Sub Form_Load()
MsgBox myFunc("c:\autoexec.bat") = FILE_ATTRIBUTE_ARCHIVE
End Sub
Public Function myFunc(sPathName) As Long
myFunc = GetFileAttributes(sPathName)
End Function
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpfilename As String) As Long
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Sub Form_Load()
Dim tmp As String, zzz As String, a As Long
tmp = "C:\autoexec.bat"
a = myFunc(tmp)
If (a And FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE Then zzz = zzz & "archive" & vbCrLf
If (a And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then zzz = zzz & "directory" & vbCrLf
If (a And FILE_ATTRIBUTE_HIDDEN) = FILE_ATTRIBUTE_HIDDEN Then zzz = zzz & "hidden" & vbCrLf
If (a And FILE_ATTRIBUTE_NORMAL) = FILE_ATTRIBUTE_NORMAL Then zzz = zzz & "normal" & vbCrLf
If (a And FILE_ATTRIBUTE_READONLY) = FILE_ATTRIBUTE_READONLY Then zzz = zzz & "readonly" & vbCrLf
If (a And FILE_ATTRIBUTE_SYSTEM) = FILE_ATTRIBUTE_SYSTEM Then zzz = zzz & "system" & vbCrLf
If (a And FILE_ATTRIBUTE_TEMPORARY) = FILE_ATTRIBUTE_TEMPORARY Then zzz = zzz & "temporary" & vbCrLf
MsgBox tmp & " is a Type :" & vbCrLf & zzz, 0, "File Size Info"
End Sub
Public Function myFunc(sPathName) As Long
myFunc = GetFileAttributes(sPathName)
End Function
Option Explicit
Dim strFile, strMsg
Dim objWMIService, strComputer, colFiles, objFile
strComputer = "."
strFile = "C:\\test.txt" ' Use \\ instead of \
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where name = '" & strFile & "'")
For Each objFile in colFiles
strMsg = "Access mask: " & objFile.AccessMask & vbCrlf
strMsg = strMsg & "Archive: " & objFile.Archive & vbCrlf
strMsg = strMsg & "Compressed: " & objFile.Compressed & vbCrlf
strMsg = strMsg & "Compression method: " & objFile.CompressionMethod & vbCrlf
strMsg = strMsg & "Creation date: " & objFile.CreationDate & vbCrlf
strMsg = strMsg & "Computer system name: " & objFile.CSName & vbCrlf
strMsg = strMsg & "Drive: " & objFile.Drive & vbCrlf
strMsg = strMsg & "8.3 file name: " & objFile.EightDotThreeFileName & vbCrlf
strMsg = strMsg & "Encrypted: " & objFile.Encrypted & vbCrlf
strMsg = strMsg & "Encryption method: " & objFile.EncryptionMethod & vbCrlf
strMsg = strMsg & "Extension: " & objFile.Extension & vbCrlf
strMsg = strMsg & "File name: " & objFile.FileName & vbCrlf
strMsg = strMsg & "File size: " & objFile.FileSize & vbCrlf
strMsg = strMsg & "File type: " & objFile.FileType & vbCrlf
strMsg = strMsg & "File system name: " & objFile.FSName & vbCrlf
strMsg = strMsg & "Hidden: " & objFile.Hidden & vbCrlf
strMsg = strMsg & "Last accessed: " & objFile.LastAccessed & vbCrlf
strMsg = strMsg & "Last modified: " & objFile.LastModified & vbCrlf
strMsg = strMsg & "Manufacturer: " & objFile.Manufacturer & vbCrlf
strMsg = strMsg & "Name: " & objFile.Name & vbCrlf
strMsg = strMsg & "Path: " & objFile.Path & vbCrlf
strMsg = strMsg & "Readable: " & objFile.Readable & vbCrlf
strMsg = strMsg & "System: " & objFile.System & vbCrlf
strMsg = strMsg & "Version: " & objFile.Version & vbCrlf
strMsg = strMsg & "Writeable: " & objFile.Writeable & vbCrlf
Next
Wscript.Echo strMsg






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Since FSO gives you what you need, why not use it?
Maybe I didnt ask the question very succinctly ... but what I was/am after is the API to retrieve the correct file type - similar to the fso object file type property ...
The answer to my question seems to be that the relevant API for retrieving the file type is SHGetFileInfo, which takes a SHFILEINFO structure and fills it with the relevant file information.
Please confirm my code example below is correct  ....
Private Const MAX_PATH = 260
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH ' out: display name (or path)
szTypeName As String * 80 ' out: type name
End Type
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Function returnFileType(lpsFile as string)
Dim sfi As SHFILEINFO
dim retVal as long
retval = SHGetFileInfo(Path & FileName, 0&, sfi, Len(sfi), SHGFI_TYPENAME)
msgbox "The file type is : " & sfi.szTypeName
end function
Visual Basic Classic
--
Questions
--
Followers
Top Experts
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.