Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Launch Windows Explorer from Windows DOS prompt

by davidrw (Prior)
on Apr 07, 2006 at 15:16 UTC ( #541882=note: print w/ replies, xml ) Need Help??


in reply to Launch Windows Explorer from Windows DOS prompt

no need to read the directory twice .. few options:

  • read into a @listing array, and grep that to make @dirs and @files
  • read as little as possible w/a loop like:
    my ($dir, $file); while( readdir DIR ){ if( -d ){ $dir = $_; last; } $file ||= $_; } close DIR; $cwd .= "/" . ($dir || $file || '.'); $cwd =~ s#/#\\#g; warn $cwd;
  • read whole thing and sort:
    my @listing = sort { -d $b <=> -d $a || $a cmp $b } grep { ! /^\.\.?$/ + } readdir DIR; $cwd .= "/" . ($listing[0] || '.'); $cwd =~ s#/#\\#g;


Comment on Re: Launch Windows Explorer from Windows DOS prompt
Select or Download Code
Replies are listed 'Best First'.
Re^2: Launch Windows Explorer from Windows DOS prompt
by larryl (Scribe) on Apr 14, 2006 at 21:14 UTC

    I combined jimbojones' original script with the ideas from davidrw, and then added a command-line option so you could specify a path. So for example you can set up desktop shortcuts to open an explorer window pointing to things like "C:\Apache\conf".

    I had to change the -d test in the sort specification from
       -d $b <=> -d $a
    to
       (-d $b ? 1 : 0) <=> (-d $a ? 1 : 0)
    because I was getting errors messages about invalid numeric comparison...

    use warnings; use strict; use Cwd; use File::Spec (); use IO::Dir; my $path = shift || cwd(); $path = File::Spec->canonpath($path); if (-d $path) { chdir $path or die "Can't chdir to \"$path\"\n"; my $dir_handle = IO::Dir->new($path) or die "Can't open \"$path\"\n"; my @items = sort { (-d $b ? 1 : 0) <=> (-d $a ? 1 : 0) || $a cmp $ +b } grep { ! /^[.][.]?$/ } $dir_handle->read; undef $dir_handle; $path = File::Spec->catfile($path, $items[0] || '.'); } elsif (! -f $path) { die "Can't find \"$path\"\n"; } exec 'explorer.exe /e,/select,' . $path; # launch Explorer exit;


    Larry

      nice ..

      yeah, i guess that would generate warnings since -X returns 1 or undef ..could save a few characters with -d $b || 0 <=> -d $a || 0 Another alternative would be to do a Schwartzian Transform (especially if there might be a large number of directories).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://541882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2015-07-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls