2015-04-24 78 views
-1

準確地說,是否可以使用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 
+0

我想是的。如果你正在討論帶有'.mp3'擴展名的文件,但實際上命名了'something.mp3.exe',並且文件擴展名是隱藏的,那麼是的,你可以使用VBA來檢查它是否真的是mp3文件。但是,你的問題仍然缺乏背景,可能在這裏脫離主題。 – L42

+0

謝謝!讓我知道我可以如何添加更多的上下文。和/或我可以添加哪些其他主題。儘管基於這裏的回答,我正在回答這個問題,並且一旦我擁有了答案,我就會添加完整答案。 – dsauce

回答

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 
+0

謝謝!我正在修改代碼來檢查mp3,將添加答案一旦我有它 – dsauce

+0

嗨@steven我一直在嘗試幾種方法,因爲過去3天,但不幸的是還沒有任何運氣。能否請您多指導一些投入。 – dsauce

+0

你能編輯你的問題併發布你的代碼嗎? – Steven

相關問題