in reply to Getting File Types data

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Getting File Types data
by merrymonk (Hermit) on Aug 20, 2008 at 09:33 UTC
    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.
        I have now used your example to give a subroutine to which I give an array of the
        extensions for which I want the path to the .exe.
        This is below in case it is of any use to another Monk.
        Although this works, it would be good if someone could explain how the follwing two
        Perl lines work since I do not understand them.
        my ( $extention, $type ) = split '=', `assoc $arg_item 2>nul`; my( undef, $cmd ) = split '=', `ftype $type 2>nul`;
        The sub and test code is below
        #! perl -slw use strict; # # sub get_file_association # # this gets the path for the given file extension # # argument # 1 $ref_arg reference to array holding the extensions to be tested # 2 $ref_file_path reference to the hash holding the .exe asscia +tions # strucutre # {extension}{exe} just to the end of the .exe # {extension}{all} the whole of the result # 3 $ref_path reference to the has holding the path for the exte +nsion # structure # {extension}{path<n>} gives all the assciations found # 4 $ref_error result of finding extensions one of 'none', 'some' a +nd 'all' # 5 $ref_error_message acucmulation of error messages (if there are + any) # sub get_file_association($$$$$) { my ($ref_arg, $ref_file_path, $ref_file_path_all, $ref_error, $ref_err +or_message) = @_; my ($arg_item); my ($cmd_length, $j, $ord_value, $str, $path_cou, $cur_path, $path_cou +_str, $add_chr, $path_item); my ($error_count, @path_split); $$ref_error = 'none'; $$ref_error_message = ''; $error_count = 0; foreach $arg_item ( @$ref_arg ) { my ( $extention, $type ) = split '=', `assoc $arg_item 2>nul`; if(defined($type) == 0) { print "No association was found for extension <$arg_item>\n"; $error_count +=1; $$ref_error_message .= "No association was found for extension + <$arg_item>\n"; } else { chomp $type; my( undef, $cmd ) = split '=', `ftype $type 2>nul`; if(defined($cmd) == 0) { print "No command was associated with type '$type'"; $error_count +=1; $$ref_error_message .= "No command was associated with typ +e '$type'"; chomp $cmd; } else { print "Extension '$arg_item' is associated with type '$typ +e' " . "\n\tand the command: <$cmd>\n\n"; $cur_path = ''; $path_cou = 0; $add_chr = 0; $cmd_length = length($cmd); for($j = 0; $j < $cmd_length; $j ++) { $str = substr($cmd, $j, 1); $ord_value = ord(substr($cmd, $j, 1)); if($ord_value ne 10) { $cur_path .= $str; $add_chr += 1; } else { $path_cou += 1; $path_cou_str = 'path' . $path_cou; $ref_file_path_all->{$arg_item}{$path_cou_str} = $ +cur_path; $cur_path = ''; $add_chr = 0; } } if($add_chr > 0) { $path_cou += 1; $path_cou_str = 'path' . $path_cou; $ref_file_path_all->{$arg_item}{$path_cou_str} = $cur_ +path; } foreach $path_item (keys %{$ref_file_path_all->{$arg_item} +}) { $cur_path = $ref_file_path_all->{$arg_item}{$path_item +}; if(lc($cur_path) =~ m/\.exe/) { $ref_file_path->{$arg_item}{all} = $cur_path; @path_split = split (/\.exe/, lc($cur_path)); $ref_file_path->{$arg_item}{exe} = substr($cur_pat +h, 0, length($path_split[0])) . '.exe'; } } } } } if($error_count > 0) { if($error_count == scalar(@$ref_arg)) { $$ref_error = 'all'; } else { $$ref_error = 'some'; } } } my (@file_ext, %file_path, %file_path_all, $k1, $k2, $error, $error_me +ssage); @file_ext = ( '.pl', '.doc', '.csv', '.xls' ); #@file_ext = ( '.pl' , 'xyz'); #@file_ext = ( 'xyz', 'pqr'); get_file_association(\@file_ext, \ %file_path, \%file_path_all, \$erro +r, \$error_message); print "\nafter get_file_association - error <$error> message \n$error_ +message\n\n"; foreach $k1 (sort {$a cmp $b} keys %file_path) { print "\nextension <$k1>\n"; print "exe <$file_path{$k1}{exe}>\nall <$file_path{$k1}{all}>\n"; foreach $k2 (sort {$a cmp $b} keys %{$file_path_all{$k1}}) { print "$k2 path <$file_path_all{$k1}{$k2}>\n"; } }
        This is the first time I have heard about backticks.
        However whatever they are they are great since what you have sent is just what I need!
        Many thanks.