merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

With windows XP, Tools->Folder Options->File Types allows you to set the
full path to an application to associate that application with files with a particular extension.
How can I read this path?
As a bonus it would be good to know how I can write this path.

Replies are listed 'Best First'.
Re: Getting File Types data
by BrowserUk (Patriarch) on Aug 20, 2008 at 08:18 UTC

    Take a look at the assoc and ftype commands:

    c:\test>assoc .pl .pl=Perl c:\test>ftype Perl Perl="c:\perl\bin\perl.exe" -sw "%1" %*

    They can be used to both query and set the associations and are very easy to drive via backticks.

    The alternative is digging around in the registry, which is a pain to do and easy to get wrong.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This seems to give what I want. However (and I know that probably it is very simple
      but I am missing something) how do I use these within Perl itself to get
      the information I want about other applications?

        Do you know about backticks?

        Here's a silly example to get you started:

        #! perl -slw use strict; die "You must supply an extension" unless @ARGV; for ( @ARGV ) { my( $extention, $type ) = split '=', `assoc $_ 2>nul`; warn "No association was found for extension '$_'" and next unless defined $type; chomp $type; my( undef, $cmd ) = split '=', `ftype $type 2>nul`; warn "No command was associated with type '$type'" and next unless defined $cmd; chomp $cmd; print "Extension '$_' is associated with type '$type' " . "\n\tand the command: '$cmd'"; }

        And an example of using the above code:

        c:\test>AssocFtype.pl .pl .doc Extension '.pl' is associated with type 'Perl' and the command: '"c:\perl\bin\perl.exe" -sw "%1" %*' Extension '.doc' is associated with type 'OpenOffice.org.doc' and the command: '"C:\Program Files\OpenOffice.org 2.2\program +\soffice.exe" -o "%1"'

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Getting File Types data
by moritz (Cardinal) on Aug 20, 2008 at 08:06 UTC
    I'm pretty sure that this data lives in the registry, so try Win32::TieRegistry. But I can't tell you the registry path, either search for it or wait for an enlightened monk to tell you ;-)
Re: Getting File Types data
by dHarry (Abbot) on Aug 20, 2008 at 08:53 UTC

    In general HKEY_CLASSES_ROOT stores information about registered applications such as file associations.

    Win32::TieRegistry lets you fiddle with the registry so in theory no problem. However with the Windows registry it’s always tricky.

    I think it depends on the Windows version you run and also on the particular user logged in. Software can be installed for all users or specific user(s), different users can install the same software (or different versions of it) in different locations, etc. The registry follows a strategy for looking up and writing information. You better read Windows_Registry first (fairly good description).

    Make sure you BACKUP the registry! It is too easy to screw it up! Also it doesn’t harm to have a regedit/regcleaner nearby;-)

      I have looked in the HKEY_CLASSES_ROOT registry.
      This seems to have two sections. One for extensions and one for applicatons.
      I guessed the extension section would give the path. However I could not see it.
      Is this hidden in some way?
      The problem with the application section is that I could not see the name of the application I was looking for.

        The extensions are there but if you want the path where the application is installed you have to look elsewhere.

        An example

        On my system: HK_CURRENT_USER\Software\Adobe\Acrobat Reader\7.0\InstallPath

        Value: "C:\Program Files\Adobe\Acrobat 7.0\Reader"

        If this lookup fails look at HKEY_LOCAL_MACHINE\software\companyname\product. I think this is in general the lookup strategy, i.e. first check HKCU if that fails check HKLM. Like I said it depends on the user logged on. I also recall (on the top of my head) that the registry differs (slightly) between different Windows flavours.

        See also Windows registry information for advanced users.

        HTH