是否可以使用Excel vba检查文件内容类型(mime)?

时间:2015-04-24 08:50:24

标签: excel vba excel-vba

准确地说,是否可以使用Excel VBA检查文件是否实际上是mp3文件(不是扩展名,而是内容)?

编辑:为我做的两次尝试添加代码:

尝试1 - 基于微软社区的输入。奇怪的是,这会将所有文件验证为mp3,不知道我遗漏了什么。

Sub mp3_test()
Dim rng As Range
Dim c As clsMP3Info ' name of the class module
Dim nSecs As Long

book1 = ThisWorkbook.Name

MsgBox "Please select the folder that contains files to be checked.", vbExclamation

'FILE_EXT = "xlsx"
strFolderName = Get_Folder_Path() & "\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_FOLDER = FSO.GetFolder(strFolderName)

i = 2
If FSO_FOLDER.Files.Count > 0 Then

For Each FSO_FILE In FSO_FOLDER.Files
'pq = q
sFile = strFolderName & FSO_FILE.Name

      Set c = New clsMP3Info
      With c
             If Len(sFile) Then c.Filename = sFile
             If Not c.ValidMP3 Then
                     Range("B" & i).Value = "Invalid"
                     Range("A" & i).Value = FSO_FILE.Name
                     i = i + 1
             Else
                     Range("B" & i).Value = "Valid"
                     Range("A" & i).Value = FSO_FILE.Name
                     i = i + 1
                     End If
       End With
Next
End If

s = MsgBox("Done!", vbOKOnly)    
End Sub

尝试2 - 基于@Steven在此帖子/帖子中共享的代码。这会产生溢出错误,因为变量似乎不接受超过32767字节的大小。

Sub mp3c()

Dim intFileNum As Integer, bytTemp As Byte
Dim bytes() As Byte
intFileNum = FreeFile

Open "D:\Users\adminx\Desktop\Test\Audioslave - Revelations - 07 - Somedays.mp3" For Binary Access Read As intFileNum

Dim i As Long

i = 0

Do While Not EOF(intFileNum)
    Get intFileNum, , bytTemp
    ReDim Preserve bytes(i + 1)
    bytes(i) = bytTemp
    i = i + 1
Loop

Close intFileNum

Dim headerSize, width, height As Long

width = BytesToInt(bytes(WIDTH_OFFSET + 0), bytes(WIDTH_OFFSET + 1), bytes(WIDTH_OFFSET + 2), bytes(WIDTH_OFFSET + 3))
height = BytesToInt(bytes(HEIGHT_OFFSET + 0), bytes(HEIGHT_OFFSET + 1), bytes(HEIGHT_OFFSET + 2), bytes(HEIGHT_OFFSET + 3))
headerSize = BytesToInt(bytes(HEADERSIZE_OFFSET + 0), bytes(HEADERSIZE_OFFSET + 1), bytes(HEADERSIZE_OFFSET + 2), bytes(HEADERSIZE_OFFSET + 3))
End Sub

1 个答案:

答案 0 :(得分:1)

当然,您可以打开文件进行二进制访问并读取文件头。下面是我读取位图标题的项目片段,显然你需要更改偏移量来从MP3文件中获取所需的信息。

Dim intFileNum As Integer, bytTemp As Byte
Dim bytes() As Byte
intFileNum = FreeFile

Open filePath For Binary Access Read As intFileNum

Dim i As Integer

i = 0

Do While Not EOF(intFileNum)
    Get intFileNum, , bytTemp

    ReDim Preserve bytes(i + 1)

    bytes(i) = bytTemp

    i = i + 1
Loop

Close intFileNum

Dim headerSize, width, height As Long

width = BytesToInt(bytes(WIDTH_OFFSET + 0), bytes(WIDTH_OFFSET + 1), bytes(WIDTH_OFFSET + 2), bytes(WIDTH_OFFSET + 3))
height = BytesToInt(bytes(HEIGHT_OFFSET + 0), bytes(HEIGHT_OFFSET + 1), bytes(HEIGHT_OFFSET + 2), bytes(HEIGHT_OFFSET + 3))
headerSize = BytesToInt(bytes(HEADERSIZE_OFFSET + 0), bytes(HEADERSIZE_OFFSET + 1), bytes(HEADERSIZE_OFFSET + 2), bytes(HEADERSIZE_OFFSET + 3))

编辑:可能应该包含我的BytesToInt函数,它将dword转换为整数:

Function BytesToInt(a As Byte, b As Byte, c As Byte, d As Byte) As Double

    BytesToInt = (d * 256 ^ 3) + (c * 256 ^ 2) + (b * 256) + a

End Function