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
my ($dir, $file);
while( readdir DIR ){
if( -d ){
$dir = $_;
last;
}
$file ||= $_;
}
close DIR;
$cwd .= "/" . ($dir || $file || '.');
$cwd =~ s#/#\\#g;
warn $cwd;
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
explorer .
?
[id://247834|...every application I have ever worked on is a glorified munger...]
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)
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