Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Launch Windows Explorer from Windows DOS prompt

by jimbojones (Friar)
on Apr 07, 2006 at 13:59 UTC ( #541864=CUFP: print w/replies, xml ) Need Help??

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 in your PATH and invoke with


Replies are listed 'Best First'.
Re: Launch Windows Explorer from Windows DOS prompt
by davidrw (Prior) on Apr 07, 2006 at 15:16 UTC
    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;

      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
         (-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;


        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
by t'mo (Pilgrim) on Apr 07, 2006 at 16:33 UTC
      explorer /e,. to explore instead of just open the folder (at least on this XP install)

      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

        "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: Launch Windows Explorer from Windows DOS prompt
by Malevolyn (Novice) on Apr 11, 2006 at 00:20 UTC

      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

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://541864]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-05-16 04:34 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (62 votes). Check out past polls.