@perl -Swx %0 %* @c:\a_cd.bat @goto :eof #!perl # # Utility for quickly changing directory # Named to "a.bat" in homage to Pr1mOS's change working directory comm +and *a* (attach) ;-] # # Syntax: # a ,wi,ja,pa ===> cd \Windows\java\Packages # a ...\me ===> cd \Windows\Media # a ===> cd \Windows\Media\Microsoft Office 2000 *OR* a se +lection of subdirectories # a / ===> cd \ # a mys,d,* # [a] /mysql/data/mysql/ # [b] /mysql/data/tast/ # [c] /mysql/data/test/ # [d] /mysql/Docs/Flags/ # select: # # if ambigous the correct target is selected with alpha keys /a .. zz/ # # Versions: # 0.2 2003-02-22 Cleanup # 0.1 2003-01-31 First working # 0.0 2003-01-15 Start of Coding # use strict; use warnings; use Cwd; my $DEBUG = 0; my $DELIM = qr{,|\\|\/}; ## Either "," or "/" or \" my @to = @ARGV ? split(/$DELIM/, $ARGV[0] ) : (); my @cwd = split(/$DELIM/, cwd); shift @cwd; # Remove disk unless ( @ARGV ) { push @to, @cwd, '*'; } elsif ( $ARGV[0] =~ /^$DELIM/ and not @to ){ } elsif ( $to[0] eq '' ) { shift @to; } else { unshift @to, @cwd; } ## Lazy-dots foreach my $part (0 .. $#to) { if ( $to[$part] =~ /\.(\.+)/ ) { $to[$part] = join('/', ('..') x length($1)); } } @to = split(/\//, join('/', @to)); ## Rel2Abs my @fixed; for (@to) { if ($_ eq '..') { pop @fixed if @fixed; } elsif($_ eq '.') { ## Skip } else { push @fixed, $_ } } my @choices = expand('/', @fixed); my %hash; my $enum = 'a'; my $choice = ''; if (1 == @choices) { # Autoselect if only one item to choose $hash{a} = $choices[0]; $choice = $enum; } elsif(1 < @choices) { foreach my $c (@choices) { ($hash{$enum} = $c) ; print '[', $enum++, "] $c\n"; } print "select: "; $choice = <STDIN>; chomp $choice; } # Create a batch file to change the directory in the shell. open (CD, '>', 'C:/A_CD.BAT') or die "Failed to create CD bat file $!\ +n"; if ( defined $hash{$choice} ) { $hash{$choice} =~ s/\//\\/g; # Make windows happy print CD "\@CD \"$hash{$choice}\"\n"; } else { print CD "\@echo No match\n"; } close CD; sub expand { my ($bough, @twigs) = @_; return $bough unless @twigs; ## Looked it up in the thesaurus ;-] opendir(my $dh, $bough) || die "Can not check $bough for twigs due + to $!\n"; my $regexp = shift @twigs; $regexp =~ s/\*/.*/; my @found; foreach my $f ( grep { $_ =~ /^$regexp/i and $_ !~ /^\./} readdir +($dh) ) { push @found, expand("$bough$f/", @twigs) if (-d "$bough$f"); } closedir($dh) || die "Bad close for $bough $!\n"; return @found; } __END__

In reply to Accelerator for Changing Directory by guha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.