Results 1 to 11 of 11

Thread: [RESOLVED] Checking if file has correct extension

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Resolved [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.

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    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:
    1. Private Const TheFilename    As String = "myfile.mpg"
    2.  
    3. Private Sub Form_Load()
    4.  
    5. Dim strParts() As String
    6.  
    7.     strParts = Split(TheFilename, ".")
    8.     MsgBox strParts(UBound(strParts))
    9.    
    10.  
    11. End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    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?

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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.

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Checking if file has correct extension

    see these if they are any use to you:

    Link 1
    Link 2
    Main Page
    Show Appreciation. Rate Posts.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checking if file has correct extension

    Quote 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?

  9. #9
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Checking if file has correct extension

    Found this, might be useful.

    VB Code:
    1. Public Function GetFileType(xFile As String) As String
    2.     On Error Resume Next
    3.     Dim ID As String * 300
    4.  
    5.  
    6.    
    7.     Open xFile For Binary Access Read As #1
    8.     Get #1, 1, ID
    9.     Close #1
    10.  
    11.  
    12.     If Left(ID, 2) = "MZ" Or Left(ID, 2) = "ZM" Then
    13.         GetFileType = "PE Executable"
    14.         Exit Function
    15.     ElseIf Left(ID, 1) = "[" And InStr(1, Left(ID, 100), "]") > 0 Then
    16.         GetFileType = "INI File"
    17.         Exit Function
    18.     ElseIf Mid(ID, 9, 8) = "AVI LIST" Then
    19.         GetFileType = "AVI Movie File"
    20.         Exit Function
    21.     ElseIf Left(ID, 4) = "RIFF" Then
    22.         GetFileType = "WAV Audio File"
    23.         Exit Function
    24.     ElseIf Left(ID, 4) = Chr(208) & Chr(207) & Chr(17) & Chr(224) Then
    25.         GetFileType = "Microsoft Word Document"
    26.         Exit Function
    27.     ElseIf Mid(ID, 5, 15) = "Standard Jet DB" Then
    28.         GetFileType = "Microsoft Access Database"
    29.         Exit Function
    30.     ElseIf Left(ID, 3) = "GIF" Or InStr(1, ID, "GIF89") > 0 Then
    31.         GetFileType = "GIF Image File"
    32.         Exit Function
    33.     ElseIf Left(ID, 1) = Chr(255) And Mid(ID, 5, 1) = Chr(0) Then
    34.         GetFileType = "MP3 Audio File"
    35.         Exit Function
    36.     ElseIf Left(ID, 2) = "BM" Then
    37.         GetFileType = "BMP (Bitmap) Image File"
    38.         Exit Function
    39.     ElseIf Left(ID, 3) = "II*" Then
    40.         GetFileType = "TIFF Image File"
    41.         Exit Function
    42.     ElseIf Left(ID, 2) = "PK" Then
    43.         GetFileType = "ZIP Archive File"
    44.         Exit Function
    45.     ElseIf InStr(1, LCase(ID), "<html>") > 0 Or InStr(1, LCase(ID), "<!doctype") > 0 Then
    46.         GetFileType = "HTML Document File"
    47.         Exit Function
    48.     ElseIf UCase(Left(ID, 3)) = "RAR" Then
    49.         GetFileType = "RAR Archive File"
    50.         Exit Function
    51.     ElseIf Left(ID, 2) = Chr(96) & Chr(234) Then
    52.         GetFileType = "ARJ Archive File"
    53.         Exit Function
    54.     ElseIf Left(ID, 3) = Chr(255) & Chr(216) & Chr(255) Then
    55.         GetFileType = "JPEG Image File"
    56.         Exit Function
    57.     ElseIf InStr(1, ID, "Type=") > 0 And InStr(1, ID, "Reference=") > 0 Then
    58.         GetFileType = "Visual Basic Project File"
    59.         Exit Function
    60.     ElseIf Left(ID, 8) = "VBGROUP " Then
    61.         GetFileType = "Visual Basic Group Project File"
    62.         Exit Function
    63.     ElseIf Left(ID, 8) = "VERSION " & InStr(1, ID, vbCrLf & "Begin") > 0 Then
    64.         GetFileType = "Visual Basic Form File"
    65.         Exit Function
    66.     Else
    67.         'Unknown file... make a weak attempt to
    68.         '     determine if the file is text or binary
    69.  
    70.  
    71.         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
    72.             GetFileType = "Unknown binary file"
    73.         Else
    74.             GetFileType = "Unknown text file"
    75.         End If
    76.         Exit Function
    77.     End If
    78. End Function

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    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

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    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:
    1. Public Function GetFileType(xFile As String) As String
    2.     On Error Resume Next
    3.     Dim ID As String * 300
    4.  
    5.     Open xFile For Binary Access Read As #1
    6.     Get #1, 1, ID
    7.     Close #1
    8.  
    9.     If Mid(ID, 9, 8) = "AVI LIST" Then
    10.             MsgBox "This is an AVI file"
    11.         Else
    12.             MsgBox "This is NOT an AVI file"
    13.         Exit Function
    14.     End If
    15. End Function
    16.  
    17. Private Sub Form_Load()
    18. GetFileType (Globals.SelectedFile)
    19. 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
  •  



Click Here to Expand Forum to Full Width