|
-
Dec 29th, 2005, 11:29 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Checking if file has correct extension
Hello, I have a small problem with file extensions.
My application only works with one filetype (avi). When I take a file with a .mpg, .wmv, .wav, .mp3, .ac3 or other extension, rename it to avi and use my application with the file, then my application freezes, because it can't read the file.
I need to have some error checking for this. Is there a way to check if a file has the correct extension?
Thanks.
-
Dec 29th, 2005, 11:34 AM
#2
Re: Checking if file has correct extension
Here's a example that will give you the file extension. There are other ways of doing it.
VB Code:
Private Const TheFilename As String = "myfile.mpg"
Private Sub Form_Load()
Dim strParts() As String
strParts = Split(TheFilename, ".")
MsgBox strParts(UBound(strParts))
End Sub
-
Dec 29th, 2005, 11:45 AM
#3
Thread Starter
Frenzied Member
Re: Checking if file has correct extension
Thanks, but that's not what I'm looking for. I need to know if a certain file has the extension that belongs to the file.
For example, I have a file "SomeFile.mp3". If I rename it to "SomeFile.avi" and use it with my application, then it freezes, because "SomeFile.avi" is not a real AVI file.
Is there a way to check if a file with an avi extension really is an avi?
-
Dec 29th, 2005, 12:00 PM
#4
Re: Checking if file has correct extension
Who renames it? only you, or the users of your program?
If you rename files in your app, check the real extension before doing it.
If you say the user can rename files and try to play them in your app, then you could let your app fail, and trap the error, showing a messagebox "Wrong file format".
I think It would be hard to detect if the file is valid, you should check the content of the whole file, besides, it could also be a real AVI file, and be corrupted.
-
Dec 29th, 2005, 12:05 PM
#5
Re: Checking if file has correct extension
i think there are some codecs which can do this job for you, like the one used by Win Media Player or by the AVI editor (in MS Visual Studio Tools). you may try searching for one at MSDN, or google it. if i can find one, i will post it.
-
Dec 29th, 2005, 12:05 PM
#6
Re: Checking if file has correct extension
you have to know the format of an avi file, open it in binary mode and check to see if the beginning of the file corresponds to the avi format. There is probably a required header in the file somewhere that you can check.
-
Dec 29th, 2005, 12:10 PM
#7
Re: Checking if file has correct extension
see these if they are any use to you:
Link 1
Link 2
Main Page
-
Dec 29th, 2005, 12:10 PM
#8
Re: Checking if file has correct extension
 Originally Posted by moeur
you have to know the format of an avi file, open it in binary mode and check to see if the beginning of the file corresponds to the avi format. There is probably a required header in the file somewhere that you can check.
Actually, I was thinking along these lines, but I would have no idea what to tell him to look for. Any idea what an avi header, in binary format, would be formatted like?
-
Dec 29th, 2005, 12:13 PM
#9
Re: Checking if file has correct extension
Found this, might be useful.
VB Code:
Public Function GetFileType(xFile As String) As String
On Error Resume Next
Dim ID As String * 300
Open xFile For Binary Access Read As #1
Get #1, 1, ID
Close #1
If Left(ID, 2) = "MZ" Or Left(ID, 2) = "ZM" Then
GetFileType = "PE Executable"
Exit Function
ElseIf Left(ID, 1) = "[" And InStr(1, Left(ID, 100), "]") > 0 Then
GetFileType = "INI File"
Exit Function
ElseIf Mid(ID, 9, 8) = "AVI LIST" Then
GetFileType = "AVI Movie File"
Exit Function
ElseIf Left(ID, 4) = "RIFF" Then
GetFileType = "WAV Audio File"
Exit Function
ElseIf Left(ID, 4) = Chr(208) & Chr(207) & Chr(17) & Chr(224) Then
GetFileType = "Microsoft Word Document"
Exit Function
ElseIf Mid(ID, 5, 15) = "Standard Jet DB" Then
GetFileType = "Microsoft Access Database"
Exit Function
ElseIf Left(ID, 3) = "GIF" Or InStr(1, ID, "GIF89") > 0 Then
GetFileType = "GIF Image File"
Exit Function
ElseIf Left(ID, 1) = Chr(255) And Mid(ID, 5, 1) = Chr(0) Then
GetFileType = "MP3 Audio File"
Exit Function
ElseIf Left(ID, 2) = "BM" Then
GetFileType = "BMP (Bitmap) Image File"
Exit Function
ElseIf Left(ID, 3) = "II*" Then
GetFileType = "TIFF Image File"
Exit Function
ElseIf Left(ID, 2) = "PK" Then
GetFileType = "ZIP Archive File"
Exit Function
ElseIf InStr(1, LCase(ID), "<html>") > 0 Or InStr(1, LCase(ID), "<!doctype") > 0 Then
GetFileType = "HTML Document File"
Exit Function
ElseIf UCase(Left(ID, 3)) = "RAR" Then
GetFileType = "RAR Archive File"
Exit Function
ElseIf Left(ID, 2) = Chr(96) & Chr(234) Then
GetFileType = "ARJ Archive File"
Exit Function
ElseIf Left(ID, 3) = Chr(255) & Chr(216) & Chr(255) Then
GetFileType = "JPEG Image File"
Exit Function
ElseIf InStr(1, ID, "Type=") > 0 And InStr(1, ID, "Reference=") > 0 Then
GetFileType = "Visual Basic Project File"
Exit Function
ElseIf Left(ID, 8) = "VBGROUP " Then
GetFileType = "Visual Basic Group Project File"
Exit Function
ElseIf Left(ID, 8) = "VERSION " & InStr(1, ID, vbCrLf & "Begin") > 0 Then
GetFileType = "Visual Basic Form File"
Exit Function
Else
'Unknown file... make a weak attempt to
' determine if the file is text or binary
If InStr(1, ID, Chr$(255)) > 0 Or InStr(1, ID, Chr$(1)) > 0 Or InStr(1, ID, Chr$(2)) > 0 Or InStr(1, ID, Chr$(3)) > 0 Then
GetFileType = "Unknown binary file"
Else
GetFileType = "Unknown text file"
End If
Exit Function
End If
End Function
-
Dec 29th, 2005, 01:23 PM
#10
Thread Starter
Frenzied Member
Re: Checking if file has correct extension
This last piece of code seems to do the trick.
I renamed a mp3 file to avi. Then I used the GetFileType function and it returned as a mp3 file.
Thank you very much
-
Dec 30th, 2005, 03:15 PM
#11
Thread Starter
Frenzied Member
Re: [RESOLVED] Checking if file has correct extension
I have another question about this. It's probably better to aks it here than to make a new thread about it.
This is the code I'm using now. It's a modified version of the code above to only check if the file is a valid AVI file:
Globals.SelectedFile is a filepath.
VB Code:
Public Function GetFileType(xFile As String) As String
On Error Resume Next
Dim ID As String * 300
Open xFile For Binary Access Read As #1
Get #1, 1, ID
Close #1
If Mid(ID, 9, 8) = "AVI LIST" Then
MsgBox "This is an AVI file"
Else
MsgBox "This is NOT an AVI file"
Exit Function
End If
End Function
Private Sub Form_Load()
GetFileType (Globals.SelectedFile)
End Sub
When I right-click on one AVI and run the application (from the Explorer right-click menu), then it works fine. But when I highlight multiple AVI files, right-click on them and run the application, then it always returns "This is NOT an AVI file", because it can only read one AVI file at a time.
Is there a way to check file 1 first, then file 2, then file 3, etc?
Like a batch process.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|