Jump to content

FileGetType UDF


 Share

Recommended Posts

I needed this func in my TreeViewBrowse, so i though someone may find it useful..

Func _FileGetType($hFile) 
    Local $hGetExt = StringRight($hFile,3)
    Local $hGetAttrib = FileGetAttrib($hFile)

    If (StringInStr($hGetAttrib,"D")) Then
        Return "Directory"
;/// Program files
    ElseIf $hGetExt = "exe" Then 
        Return "Program" 
    ElseIf $hGetExt = "bin" Then 
        Return "BIN-file"
    ElseIf $hGetExt = "dll" Then 
        Return "Program extension"
    
;/// Text files
    ElseIf $hGetExt = "txt" Then 
        Return "Text-document"
    ElseIf $hGetExt = "au3" Then 
        Return "AutoIt V3 Script"
    ElseIf $hGetExt = "RTF" Then 
        Return "RTF-document"
    ElseIf $hGetExt = "htm" Then 
        Return "HTML-document"
    ElseIf $hGetExt = "html" Then 
        Return "HTML-document"
    
;/// Picture files
    ElseIf $hGetExt = "jpg" Then 
        Return "jpg-picture"
    ElseIf $hGetExt = "jpeg" Then 
        Return "jpeg-picture"
    ElseIf $hGetExt = "gif" Then 
        Return "GIF-picture"
    ElseIf $hGetExt = "bmp" Then 
        Return "BMP-picture"
    ElseIf $hGetExt = "png" Then 
        Return "PNG-picture"
    
;/// Sound files
    ElseIf $hGetExt = "wav" Then 
        Return "Wav-Sound"
    ElseIf $hGetExt = "mp3" Then 
        Return "mp3-Sound"
    
;/// Packed files
    ElseIf $hGetExt = "rar" Then 
        Return "RAR Archive"
    ElseIf $hGetExt = "zip" Then 
        Return "ZIP Archive"
    
;/// Other files
    ElseIf $hGetExt = "url" Then 
        Return "Internet shortcut"
    ElseIf $hGetExt = "chm" Then 
        Return "Compiled HTML Help-file"
    ElseIf $hGetExt = "hlp" Then 
        Return "Help-file"
    ElseIf $hGetExt = "pdf" Then 
        Return "Acrobat Reader file"
    
;/// No match, Set Default Ext
    Else 
        Return $hGetExt & "-file"
    EndIf
EndFunc;FileGetType End
Edited by Wb-FreeKill
Link to comment
Share on other sites

Cool, but you know that those returns look similar to rhe default classes in registry.

MsgBox(0, '', RegRead('HKEY_CLASSES_ROOT\.url', ''))

This maybe an option to use also? The extention could be a variable in the regread() line?

Link to comment
Share on other sites

Cool, but you know that those returns look similar to rhe default classes in registry.

MsgBox(0, '', RegRead('HKEY_CLASSES_ROOT\.url', ''))

This maybe an option to use also? The extention could be a variable in the regread() line?

<{POST_SNAPBACK}>

thx, but only very few has an actual descryption, like the url.. most of them just returns "exefile,dllfile,txtfile" and so on, think mine is better :( but you have to code em your self :(
Link to comment
Share on other sites

Firstly, you're assuming that all files have an extension, and that it's always exactly 3 characters. Secondly, if you look up an extension in HCR and then look up that value, you'll usually get a better description.

Observe:

CODE

Func _FileGetExt($sFileName)

Dim $DotPos, $Other

$DotPos = StringInStr($sFileName, '.', 1, -1)

If $DotPos = 0 Then Return ''

$Other = StringInStr($sFileName, '\', 1, -1)

If $Other > $DotPos Then Return ''

$Other = StringInStr($sFileName, ':', 1, -1)

If $Other > $DotPos Then Return ''

Return StringTrimLeft($sFileName, $DotPos)

EndFunc

Func _FileGetType($sFileName)

Dim $Ext = _FileGetExt($sFileName), $Type, $Type2

$Type = RegRead('HKEY_CLASSES_ROOT\.' & $Ext, '')

If $Type = '' Then

$Type = $Ext & ' file'

Else

$Type2 = RegRead('HKEY_CLASSES_ROOT\' & $Type, '')

If $Type2 <> '' Then $Type = $Type2

EndIf

Return $Type

EndFunc

Link to comment
Share on other sites

Func _FileGetExt($sFileName)

Dim $DotPos, $Other

$DotPos = StringInStr($sFileName, '.', 1, -1)

If $DotPos = 0 Then Return ''

$Other = StringInStr($sFileName, '\', 1, -1)

If $Other > $DotPos Then Return ''

$Other = StringInStr($sFileName, ':', 1, -1)

If $Other > $DotPos Then Return ''

Return StringTrimLeft($sFileName, $DotPos)

EndFunc

Func _FileGetType($sFileName)

Dim $Ext = _FileGetExt($sFileName), $Type, $Type2

$Type = RegRead('HKEY_CLASSES_ROOT\.' & $Ext, '')

If $Type = '' Then

  $Type = $Ext & ' file'

Else

  $Type2 = RegRead('HKEY_CLASSES_ROOT\' & $Type, '')

  If $Type2 <> '' Then $Type = $Type2

EndIf

Return $Type

EndFunc

slightly edited, now calls directories for........ directory :(

Func _FileGetExt($sFileName)
    Dim $DotPos, $Other
    $DotPos = StringInStr($sFileName, '.', 1, -1)
    If $DotPos = 0 Then Return ''
    $Other = StringInStr($sFileName, '\', 1, -1)
    If $Other > $DotPos Then Return ''
    $Other = StringInStr($sFileName, ':', 1, -1)
    If $Other > $DotPos Then Return ''
    Return StringTrimLeft($sFileName, $DotPos)
EndFunc

Func _FileGetType($sFileName)
    Dim $Ext = _FileGetExt($sFileName), $Type, $Type2
    $Type = RegRead('HKEY_CLASSES_ROOT\.' & $Ext, '')
    If $Type = '' Then 
        If StringInStr(FileGetAttrib($sFileName),"D") Then 
            $Type = "Directory"
        Else    
            $Type = $Ext & '-file'
        EndIf
    Else
        $Type2 = RegRead('HKEY_CLASSES_ROOT\' & $Type, '')
        If $Type2 <> '' Then $Type = $Type2
    EndIf
    Return $Type
EndFunc
Link to comment
Share on other sites

I think you put that IF in a bad place - if you have a folder named C:\MyFolder.Txt for example, the whole directory check will be skipped. Try this instead:

CODE

Func _FileGetType($sFileName)

Dim $Type

If StringInStr(FileGetAttrib($sFileName),"D") Then

$Type = RegRead('HKEY_CLASSES_ROOT\Folder', '')

Else

Dim $Ext = _FileGetExt($sFileName)

$Type = RegRead('HKEY_CLASSES_ROOT\.' & $Ext, '')

If $Type = '' Then

$Type = $Ext & '-file'

Else

Dim $Type2 = RegRead('HKEY_CLASSES_ROOT\' & $Type, '')

If $Type2 <> '' Then $Type = $Type2

EndIf

EndIf

Return $Type

EndFunc

Edited by blindwig
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...