Launch Windows Explorer from Windows DOS prompt
jimbojones
created: 2006-04-07 09:59:21
Even though I'm on Windows, I like to use the command line as much as possible. For example, I like to be able to pop a DOS box from right-clicking on a directory in Windows Explorer (see this registry hack).

The reverse of this is to open Windows Explorer from the command line of the DOS box, and have it set to the current directory of the DOS box. The following script formats the call to Explorer's command line arguments to do that. It attempts to highlight the first file or subdirectory in the directory specified. The only bug I have so far is that if you are in an empty directory, Explorer doesn't open the directory for you.

use warnings;
use strict;
use Cwd;

my $cwd = cwd() ;

#-- get the subdirs and files
opendir ( DIR, $cwd ) || die "$!: dir $cwd doesn't exist!\n";
my @dirs  = grep { -d && $_ ne "." && $_ ne ".." } readdir DIR;
rewinddir DIR;
my @files = grep { -f } readdir DIR;
close DIR;

$cwd =~ s{/}{\\}g;
if ( @dirs )
{
 $cwd .= '\\'. shift @dirs;
}
elsif ( @files )
{
 $cwd .= '\\' . shift @files;
}
else
{
 $cwd .= '\\.';
}

#-- pop the explorer window
exec "explorer.exe /e,/select," . $cwd;
Extract the file to expl.pl in your PATH and invoke with

>expl
Re: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-07 11:16:43
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;
    
Re^2: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-14 17:14:56

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

Re^3: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-14 17:57:39
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).
Re: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-07 12:33:51
Why not just:
    explorer .
?

[id://247834|...every application I have ever worked on is a glorified munger...]

Re^2: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-07 12:39:58
explorer /e,. to explore instead of just open the folder (at least on this XP install)
Re^2: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-07 13:16:45
Hmm,

I tried the "." for the working directory, but it didn't work when I was playing around with it. Operator error, I guess, since it now appears to work fine, as you suggest. I did want the folders, so that makes the /e switch necessary. Starting to get more typing, so I made it a script. Probably overkill.

Thanks, jim

Re^3: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-08 03:32:23
"Folders" is one of the buttons you can add to the toolbar if it's not there by default. It allows you to toggle between the view with the folder tree and the view without the folder tree. Very useful.
Re^4: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-08 16:20:53
Hi

I don't like the Explorer default of not showing the folder structure. So I want the folders on when Explorer launches. Hence the /e flag when launching Explorer.

- j

Re^5: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-08 17:51:44
To change the default, set HKCR\Folder\shell\(Default) to explore. Underfining said value or setting it to open will restore the default behaviour.
Re^6: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-10 04:46:09
I did not knew that, thank you.

But as soon as you use a directory as parameter, you have to use the /e to make it work (on W2K at least)

And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
(Terry Pratchett, Small Gods)

Re: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-10 20:20:18
Try this:
> start .
Re^2: Launch Windows Explorer from Windows DOS prompt
created: 2006-04-10 22:24:55
Hi

Yes, I googled that too. It's what led me to go down this route. The main deficiency with this approach is that by default, it doesn't show the folders. It in "open" mode instead of "explore" mode. Perhaps the registry hack above addresses this problem, but so does the script.

thanks, J

Re: Launch Windows Explorer from Windows DOS prompt
created: 2006-05-07 16:06:50
Is there a way to open explorer with tree in the left
window having a specified state?  

For example, open explorer and manually play with +
and - to create a certain layout.  Then call a perl 
script to record this layout to a text file.  Later on, 
have perl script read the saved layout file and open 
explorer with the tree in the left window branched out
as specified in the layout file.

perlmonks.org content © perlmonks.org and Anonymous Monk, Brutha, davidrw, ikegami, jimbojones, larryl, Malevolyn, t'mo

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03