Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

>expl

Comment on Launch Windows Explorer from Windows DOS prompt
Select or Download Code
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
      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).
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)
      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

        "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
      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
by Anonymous Monk on May 07, 2006 at 20:06 UTC
    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.
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2014-07-24 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls