準確地說,是否可以使用Excel VBA檢查一個文件是否實際上是一個mp3文件(不是擴展名,而是內容)?是否可以使用Excel vba檢查文件內容類型(mime)?
編輯:兩個嘗試,我把代碼添加:
嘗試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
我想是的。如果你正在討論帶有'.mp3'擴展名的文件,但實際上命名了'something.mp3.exe',並且文件擴展名是隱藏的,那麼是的,你可以使用VBA來檢查它是否真的是mp3文件。但是,你的問題仍然缺乏背景,可能在這裏脫離主題。 – L42
謝謝!讓我知道我可以如何添加更多的上下文。和/或我可以添加哪些其他主題。儘管基於這裏的回答,我正在回答這個問題,並且一旦我擁有了答案,我就會添加完整答案。 – dsauce