Jump to content

FileType By Associated Application


YellowLab
 Share

Recommended Posts

I was looking for a way to display icons for a file based on how they are associated and found this posting:

http://www.autoitscript.com/forum/index.ph...mp;#entry388516

Unfortunately, it didn't work as I intended:

post-34148-1221070134_thumb.jpg

There are many blank areas and some confusing icons.

So, I mucked around the registry - and mucking is the right verb. I rewrote the association call to look for the application that is associated with the file type and show that programs icon. The results were much better:

post-34148-1221070261_thumb.jpg

There are still some funny icon associations that maybe could be written as exceptions, but I felt it suited my purposes more and thought I would share.

CODE
#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <ButtonConstants.au3>

#include <File.au3>

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("AForm1", 633, 454, 193, 115)

$ListView1 = GUICtrlCreateListView("Filename |Size|Filetype ", 20, 22, 540, 373)

;~ GUICtrlSetImage($ListView1, "shell32.dll", 3)

$ListView1_0 = GUICtrlCreateListViewItem("test item", $ListView1)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

loadfiles("C:\")

GUICtrlSetImage($ListView1_0, "shell32.dll", 3)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $ListView1

EndSwitch

WEnd

Func loadfiles($folder)

$files = _FileListToArray($folder, "*.*", 1)

local $szIconFile1, $nIcon1=0,$ListViewItem[uBound($files)]

For $i = 1 To UBound($files) - 1

$ListViewItem[$i]=GUICtrlCreateListViewItem($files[$i] & "|" & Int(FileGetSize($files[$i]) / 1024) & "|" & StringRight($files[$i], 3), $ListView1)

GUICtrlSetImage(-1, _FileGetIcon($folder&'\'&$files[$i]), 1)

Next

EndFunc ;==>loadfiles

Func _FileGetIcon($szFile)

Local $arTemp,$sExt,$arPath

$arTemp = StringSplit($szFile,'.')

$sExt='.'&$arTemp[$arTemp[0]]

If StringLower($sExt) = '.exe' Then Return $szFile

$arPath = StringRegExp(RegRead("HKCR\"&RegRead("HKCR\"&$sExt,"")&"\shell\open\command",""),"(.*)(?i)exe",2)

If IsArray($arPath) Then

Return StringReplace($arPath[0],'"','')

Else

;unknown...

Return "shell32.dll"

EndIf

EndFunc ;==>_FileGetIcon

There must be some handling of exe files. This function will return the argument if an exe is passed. In this example it is handled by passing the entire path as the argument. If only filenames and extensions are passed, there must be a way to append the path to an exe file.

Hope someone finds this useful!

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

You could also use the System ImageList like I made it in ShellListView :)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Looks good, but ultimately I need to associate an icon with a group of files that are not necessarily in the same directory. For example my list may be:

C:\Temp\mydoc.doc

C:\Program\runfile.exe

F:\Help\readme.txt

I can't figure out enough to pull the shell information from your script. But that is exactly what I am looking for.

I have pulled the functions _GUIImageList_GetFileIconIndex and _WinAPI_SHGetFileInfo out. Shouldn't a call to _GUIImageList return an icon that can be used with GUICtrlSetImage or am I way off base?

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

Try this one :) i modified it a while ago for a shell script (seems to work perfectly on my machine .. well almoast because there are a few files that don't have the exact same icon as the windows shell)

Func FileGetIcon(ByRef $szIconFile, ByRef $nIcon, $szFile)
    $props=FileGetAttrib($szFile)
    If StringInStr($props,"D") Then
        $szIconFile=@SystemDir&"\shell32.dll"
        $nIcon=3
        Return 1
    EndIf
    Dim $szRegDefault = "", $szDefIcon = ""
    $szExt = StringMid($szFile, StringInStr($szFile, '.', 0, -1))
    If $szExt = '.lnk' Then
        $details = FileGetShortcut($szIconFile)
        $szIconFile = $details[0]
        $szExt = StringMid($details[0], StringInStr($details[0], '.', 0, -1))
    EndIf
    If $szExt=".inc" Or $szExt=".sql" Then
        $szIconFile=@SystemDir&"\shell32.dll"
        $nIcon=70
        Return 1
    EndIf
    $szRegDefault = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & $szExt, "ProgID")
    If $szRegDefault = "" Then $szRegDefault = RegRead("HKCR\" & $szExt, "")
    ;If $szExt=".txt" Then
    ;   $szIconFile=RegRead("HKEY_CLASSES_ROOT\SystemFileAssociations\.rtf\DefaultIcon","")
    ;EndIf
    If $szRegDefault <> "" Then $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\DefaultIcon", "")
    If StringInStr($szDefIcon,",") Then
        $split=StringSplit($szDefIcon,",")
        $szIconFile=StringReplace($split[1],'"',"")
        $nIcon=$split[2]
        Return 1
    EndIf
    If $szDefIcon = "" Then $szRegDefault = RegRead("HKCR\" & $szRegDefault & "\CurVer", "")
    If $szRegDefault <> "" Then $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\DefaultIcon", "")
    If $szRegDefault = "" Then $szRegDefault = RegEnumVal("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & $szExt & "\OpenWithProgids", 1)
    If $szDefIcon = "" Then
        $szIconFile = "shell32.dll"
    ElseIf $szDefIcon <> "%1" Then
        $arSplit = StringSplit($szDefIcon, ",")
        If IsArray($arSplit) Then
            $szIconFile = $arSplit[1]
            If $arSplit[0] > 1 Then $nIcon = $arSplit[2]
        Else
            Return 0
        EndIf
    EndIf
    Return 1
EndFunc   ;==>FileGetIcon

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

@TheMadman

I used your function call and got similar results to the first one I posted - blanks and obscure icons.

I tested it at work and so there may be some funny associations. I will try it at home on a system I know.

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

@TheMadman

I used your function call and got similar results to the first one I posted - blanks and obscure icons.

I tested it at work and so there may be some funny associations. I will try it at home on a system I know.

Bob

I'm having the same issue. I assumed it was just because I was using 'Windows XP Media Center Edition'. :)

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

I used WinXP sp2 (at work) and sp3 at home and have the missing icons similar to my first post. Delving into the registry it looks like not all applications work the same way - not a surprise. There isn't consistency and that leads to exceptions that cannot be accounted for.

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

I used WinXP sp2 (at work) and sp3 at home and have the missing icons similar to my first post. Delving into the registry it looks like not all applications work the same way - not a surprise. There isn't consistency and that leads to exceptions that cannot be accounted for.

Bob

Hmm... I'm kind of stumped at this point. I just took a dive into the registry at the .db file extension value and it is "shell32.dll,-154" but according to "Icon Browser" (by lazycat & thesaint) the value is incorrect.

Posted Image

The correct value should be shell32.dll,-72, or something along those lines.

Ok.. So I'm thinking of a work around, I found that offsetting the values by adding 81 could be a temporary fix.

Tell me if it works for you.. ok.

[updated 09/12/2008]

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("AForm1", 633, 454, 193, 115)
$ListView1 = GUICtrlCreateListView("Filename |Size|Filetype ", 20, 22, 540, 373)
;~ GUICtrlSetImage($ListView1, "shell32.dll", 3)
$ListView1_0 = GUICtrlCreateListViewItem("test item", $ListView1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
loadfiles(@ScriptDir)
GUICtrlSetImage($ListView1_0, "shell32.dll", 3)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ListView1
    EndSwitch
WEnd
Func loadfiles($folder)
    $files = _FileListToArray($folder, "*.*", 1)
    Local $szIconFile1, $nIcon1 = 0, $ListViewItem[UBound($files)]
    For $i = 1 To UBound($files) - 1
        $ListViewItem[$i] = GUICtrlCreateListViewItem( _
        $files[$i] & "|" & _
        Int(FileGetSize($files[$i]) / 1024) & "|" & _
        StringRight($files[$i], 3) _
        , $ListView1)
       
        Local $sFile = $files[$i]
        
        $azShortInfo = FileGetShortcut($sFile)
        If IsArray($azShortInfo) Then $sFile = $azShortInfo[0]
        
        If GUICtrlSetImage($ListViewItem[$i], $sFile) = 0 Then
            Local $sIconFile, $nIcon = 0
            If FileGetIcon($sIconFile, $nIcon, $sFile) Then
                ConsoleWrite($sFile &"="& $sIconFile &","& $nIcon & @lf)
                $nIcon = GUICtrlSetImage($ListViewItem[$i], $sIconFile, $nIcon)
                If $nIcon = 0 Then GUICtrlSetImage($ListViewItem[$i], @SystemDir & '\shell32.dll', 0) ; whatever icon you want
            Else
                GUICtrlSetImage($ListViewItem[$i], @SystemDir & '\shell32.dll', 0)
            EndIf
        EndIf
       
    Next
EndFunc   ;==>loadfiles


;Author - Holger Kotsch, modifed by mrRevoked
Func FileGetIcon(ByRef $szIconFile, ByRef $nIcon, $szFile)
    Dim $szRegDefault = "", $szDefIcon = ""
    $nIcon = 0
    $szExt = StringTrimLeft($szFile, StringInStr($szFile, ".", 0, -1))
    If $szRegDefault = "" Then $szRegDefault = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & $szExt, "ProgID")
    If $szRegDefault = "" Then $szRegDefault = RegRead("HKCR\." & $szExt, "")
    If $szRegDefault <> "" Then $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\DefaultIcon", "")
    If $szDefIcon = "" Then
        $szIconFile = "shell32.dll"
    ElseIf $szDefIcon <> "%1" Then
        $arSplit = StringSplit($szDefIcon, ",")
        If IsArray($arSplit) Then
            $szIconFile = FileGetLongName(StringReplace($arSplit[1], '"', ''))
            If $arSplit[0] > 1 Then $nIcon = $arSplit[2]
            If StringInStr($arSplit[1], "shell32.dll") Then $nIcon += 81
        Else
            Return 0
        EndIf
    EndIf
    Return 1
EndFunc   ;==>FileGetIcon
Edited by mrRevoked
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

It does seem to capture the zip, txt, ini and sys files properly. However, there are still issues, most notably exe files. This should merely pass through their own icon. Also, I don't think Irfanview plays fair. All my image (png, bmp, jpg) files show nothing. In fairness, my Irfanview association is on a portable drive. If the portable drive is installed, Irfanview will open the files.

While my original post doesn't show the "explorer" icon, it does find the shell executable that is associated with the file type and displays that program's icon - i.e. txt files shows the icon for notepad and images show QuickTime viewer (not Irfanview). I may write and exception for zip files and the few others that may show something odd.

Thanks,

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

Hello again,

I've updated the code. Blanks icons are no longer created, and some minor changes. It's funny how the image icons aren't displaying for me either. Even though I'm using a portable XNView I have it set as my default image viewer.. As a matter of fact, the values returned from the registry for some images is "C:\Program Files\Microsoft Office\OFFICE11\OIS.EXE". :)

Anyways good luck with that.

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

  • 1 year later...

Tell me if it works for you.. ok.

[updated 09/12/2008]

Dude very cool code.

Do u have one that lists directories and its contents ?

Maybe something like normal windows tree view with all files listed in sub-directories kinda like task bar "tool bar" does if pointed to dir with subdir's and files ?

Thanks

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