Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Accelerator for Changing Directory

by guha (Priest)
on Mar 27, 2003 at 23:37 UTC ( [id://246366]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility / Win32
Author/Contact Info /msg guha
Description: Since I moved to Win XP my old and trusted companion for easily jumping between different spots in the directory tree, NCD (Norton Change Directory) has ceased to work.
NCD worked by building a database and by using NCD to make and remove directories, the database could be kept in sync, well almost anyway.
If the directory you wanted to go to had a unique name all you had to type was NCD dir_name but if it was a common name like test you might have had to recall the commandline one or more times to get to the destination.

Another approach would be to note that if you consider part of or the whole branch, from root to the destination, the situation will be less ambigous or even unique
Using that idea I came up with this simple solution which IMHO works quite well. The user interface may be rendered idiosyncratic by some, but I welcome you to suggest improvements.
The documentation is nothing fancy just a few examples included in the code.
The program is tested under Win XP, but I think it could work on other OS's with some minor tweaks.
PS. As directory delimiter "\", "/" or even "," can be used. The comma is due to laziness, because on the Swedish keyboard one has to use Shift or Alt Gr key to get slashes!!

So instead of typing
cd "C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\Machin eKeys"
at the DOS prompt, typing the more dense command of
a ,d,a,a,m,c,r,m
will suffice.

Or even horrors a ,d,,,,,r,m ;-), but that one is tougher on the filesystem.
@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__
Replies are listed 'Best First'.
Re: Accelerator for Changing Directory
by Anonymous Monk on Mar 28, 2003 at 00:57 UTC

    The windows command prompt has filename/folder completion (not as good as some other shells). I'm not sure if it's enabled by default - otherwise it's a quick registry hack.

    See also "cmd /?"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://246366]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found