如何在 Windows 命令行中快速有效地更改目录?

如何在 Windows 命令行中快速有效地更改目录?

例如,我在C:\test1,并且我有另一个目录C:\test2\sub2

sub2我想要一个命令行工具可以像这样快速切换:

quickchange sub2

...该程序可能会为整个磁盘设置目录数据库并手动更新数据库。

那么,Windows 命令行中是否有适合我的要求的工具?

(ps:我不想使用cygwin)

编辑:我希望程序能够搜索数据库以找到sub2驻留位置并进行更改。如果有多个目录匹配,它应该要求我选择。

我知道一个小工具叫量子色散可以完成这项工作,但是QCD很久没有更新了,而且有一些错误。

答案1

对于 Windows Cmd 或 Power Shell 来说,绝对可以使用lua 吧

cd 到包含 foo 的目录:

z foo

cd 到以 foo 结尾的目录:

z foo$

使用多个参数:假设以下数据库:

10   /home/user/work/inbox
30   /home/user/mail/inbox

“z in” 会将 cd 切换到 /home/user/mail/inbox,因为该条目具有更高的权重。但是,您可以将多个参数传递给 z.lua,以选择不同的条目。在上面的示例中,“zw in” 会将目录更改为 /home/user/work/inbox。

当找到多个匹配项时,使用 z -i 将显示一个列表:

$ z -i soft
3:  0.25        /home/data/software
2:  3.75        /home/skywind/tmp/comma/software
1:  21          /home/skywind/software
> {CURSOR}

然后,您可以在实际 cd 之前输入数字并选择要转到的位置。例如,输入 3 即可 cd 到 /home/data/software。如果您只按 ENTER 而不输入任何内容,它就会退出并停留在您所在的位置。

答案2

cd十多年前,我用 Perl 编写了一个替代品,它为你提供了类似 ksh 的功能(例如 CDPATH、cd -、$OLDPWD),可在 DOS 命令行中使用。除了制表符补全之外,你可以用它做任何事情,但它不能做制表符补全,因为它不是 shell 替代品,而是一个cd替代品,所以当你点击时,<TAB>你仍然在命令 shell 下。我必须稍微清理一下代码(这样我就不会感到尴尬;它在错误方面非常可靠),但如果你感兴趣的话,我可以发布它。

这是它的内置帮助:

=============================================================================
cdp - provides UNIX-style cd capabilities under Win32
Copyright (C) 1997-8, Joseph L. Casadonte Jr. All rights reserved.
Version: 2.12

Usage: cdp [-h | -help]
       cdp -
       cdp [-x] [-unix | -dos]
       cdp [-x] -quote <AbsolutePathName>
       cdp [-x] -quote [-look <dir> | -exc <dir>] <Target>
       cdp [-x] <oldpart> <newpart>
       cdp [-x] [-noquote] <AbsolutePathName>
       cdp [-x] [-noquote] [-look <dir> | -exc <dir>] <Target>

Commands are interpretted in the order given above.  Note: the presence of
the -[no]quote option affects the order.

options:
    -h      Get help (this message) and exit
    -help   Get help (this message) and exit

    -x      suppresses the evaluation of $CDP.

    -dos    cdp with no arguments echos current working directory
    -unix   cdp with no arguments changes to root of current directory
            [default: -unix]

    -quote  will assume multiple words in <Target> or <AbsolutePathName>
            are quoted together (i.e. program files will be interpreted
            as 'program files').  May be negated (-noquote).  Used mainly with
            $CDP (see below).  [default: -noquote]

    -look <dir>  will attempt to find a matching directory by appending <dir>
                 to each element of $CDPATH\<Target>, or failing that, by
                 matching an element of $CDPATH\<Target> itself.  May be
                 specified more than once:

                    cdp -look foo -look bar perl5

                 Cannot be used in conjunction with -exc.  Used mainly with
                 $CDP (see below).

    -exc <dir>   will look exclusively for a matching directory by appending
                 <dir> to each element of $CDPATH\<Target>.  If no exact
                 match is found it will *not* match to an element of
                 $CDPATH\<Target> alone.  Cannot be used in conjunction with
                 -look.  Used mainly with $CDP (see below).

cdp -
-----
Will change to the immediately previous directory ala UNIX by using the
$OLDPWD environment variable.

cdp [-unix | -dos]
------------------
Will either change to the root directory (-unix option) or echo the current
directory (-dos option).

cdp [-quote] <AbsolutePathName>
-------------------------------
Will attempt to change into the directory <AbsolutePathName>.  Note:
<AbsolutePathName> is anything that begins with a drive letter (e:\foo), a
front- or back-slash (\temp) or a period (../foo).

cdp [-quote -look <dir> -exc <dir>] <Target>
--------------------------------------------
cdp will use the environment variable $CDPATH to find a valid directory to
change into by appending <Target> to each element in $CDPATH until a match is
found.  $CDPATH (or %CDPATH% for the dos-heads) is a semi-colon delimited
list of directories.  Directories with spaces in them are fine
(i.e. c:\program files) but UNC names are not (i.e. \\machine\share).  The
order of the directories in $CDPATH is important, as the first match wins.
Once a valid directory is found, cdp will change to that directory,
changing drives if necessary.  cdp will not match the current working
directory.

You may include the current directory in the CDPATH environment variable by
using the '.' (i.e. set CDPATH=.;c:\my documents;d:\source).  Most people
will put this at the very beginning of their CDPATH.  It is also extremely
useful if using aliasing (see below).

The two options, -look and -exc, provide ways of extending the match criteria.
If you always change into the source directory of a project (xxx\src), then
you can put the option "-look src" in your $CDP variable, to always look for
a "src" directory when a match on <Target> is found.  Multiple "-look <dir>"
options are resolved in the order given on the command line.  If none are
found, the match to <Target> alone stands.  The "-exc <dir>" option is used to
look exclusively for a particular match to <Target>\<dir>.  If one is not
found, no match is returned (i.e. <Target> alone will not match).  See the
Examples below for more info.

cdp <oldpart> <newpart>
-----------------------
You can substitute a piece of the current directory name specified by <oldpart>
with <newpart>.  For example, if you were in the directory:

   c:\release\really\long\directory\structure\src

you could type:

   cd release debug

and find yourself in:

   c:\debug\really\long\directory\structure\src

assuming it exists.  See examples below.

=============================================================================
Examples
========

Given the following directory structure:

    e:\program files
    e:\program files\perl5
    e:\program files\perl5\docs
    e:\program files\perl4

    e:\program
    e:\program\perl5
    e:\program\perl5\docs

    e:\files
    e:\files\perl5
    e:\files\perl5\docs

and the following Environment Variables:

    set CDPATH=.;e:;e:\program files
    set CDP=-quote
    set OLDPWD=e:\files\perl5

this command                 in this directory       will result in this
-----------------            ---------------------   -------------------------
cdp perl5                    c:\                     e:\program files\perl5
cdp perl5                    e:\program files        e:\program files\perl5
cdp perl5                    e:\program              e:\program\perl5

cdp e:\files\perl5           <anywhere>              e:\files\perl5
cdp \program                 e:\<anywhere>           e:\program
cdp ..\perl5                 e:\program files\perl4  e:\program files\perl5

cdp -unix                    e:\program files\perl5  e:\
cdp -dos                     e:\program files\perl5  e:\program files\perl5

cdp -                        <anywhere>              e:\files\perl5

cdp program files            e:\program\perl5        e:\program files
cdp -x program files         e:\program\perl5        e:\files\perl5
cdp 'program files'          e:\program\perl5        e:\program files
cdp -noquote 'program files' e:\program\perl5        e:\program files
cdp -noquote program files   e:\program\perl5        e:\files\perl5
cdp -x -quote program files  e:\program\perl5        e:\program files

cdp -look docs perl5         c:\                     e:\program files\perl5\docs
cdp -look docs perl4         c:\                     e:\program files\perl4

cdp -exc docs perl5          c:\                     e:\program files\perl5\docs
cdp -exc docs perl4          c:\                     cdp: path not found

=============================================================================
Backslash ('\') vs. Frontslash ('/')
====================================
cdp allows you to use, and even mix, backslashes and frontslashes together.
The following are identical:

   cdp c:\program files\perl5
   cdp c:/program files/perl5
   cdp c:\program files/perl5
   cdp c:/program files\perl5

=============================================================================
Aliasing
========

It is possible to alias the DOS command 'cd' to be cdp.  This way, you
use the features of cdp without having to remember to type a seperate
command.  To do this, you need to use a DOSKEY macro.  For example:

    doskey cd=cdp $*

The "$*" tells DOSKEY to pass all the arguments it received to the new program.

To remove the alias, type:

    doskey cd=

If you ever need to invoke the regular DOS 'cd' command, you can always use
'chdir'.

For more information, type:

    doskey /?

=============================================================================
Environment Variables
=====================
Note: Environment Variables are case sensitive for Perl.

CDPATH - semi-colon delimited list of directories to search thru
         [default: "."]
         [$CDPATH: ".;c:\my documents;l:\gc3\glog_deploy\web\xsl;l:\gc3\glog_deploy\web;l:\gc3\glog\server;l:\gc3\glog\oracle;c:\sandbox;c:\my documents\home_page;\;"]

CDP - a set of default options to be used each time cdp is invoked. This
      is so you do not have to specify the same options every time you use
      cdp.

      These options are processed *before* any options on the command line, and
      can be overridden (i.e. if -quote is set in $CDP, -noquote on the command
      line overrides it).  The use of $CDP can be suppressed by specifying
      the -x option on the command line.
      [$CDP: "-quote"]

CDPFNAME - filename to be used by the Perl script to communicate with the
           surrounding batch script (this is a serious hack!).  Can be changed
           at the top of the script.
           [$CDPFNAME: "c:\temp\cdpath.bat"]

TEMP - used by the scripts to create $CDPFNAME.
       [$TEMP: "c:\temp"]

OLDPWD - used to store the previous directory (see "cdp -" invocation)

=============================================================================
Files used
==========
%TEMP%\cdpath.bat - file used by the Perl script to communicate with the
                    surrounding batch script.  Can be changed to whatever
                    is needed at the top of the script.

=============================================================================
Contacting the Author/Latest Version
====================================
Please send questions, bug reports and comments to: [email protected]
You can get the latest version at: http://www.netaxs.com/~joc/perl.html

=============================================================================
=============================================================================

答案3

事实上你做不到。从技术角度来说,这几乎是不可能的。

假设你有一个如下的文件夹结构:

School Work/
    English/
        Assignments/
        Class Work/
        Documents/

    Science/
        Assignments/
        Class Work/
        Documents/

    Math/
        Assignments/
        Class Work/
        Documents/

现在,如果你quickchange Documents从 School Work 文件夹内运行,你希望转到哪个文件夹?有三个匹配项,无法知道你真正想要转到哪一个。当然,它可能会提示你正在查找哪个文件夹,但这种做法的扩展性不是很好

想象一下学校服务器上每个学生的文件夹结构都相同,它看起来像这样:

Josh Hunt\
    School Work\
        [etc]

Cathy Wells\
    School Work\
        [etc]

Jack Thompson\
    School Work\
        [etc]

James Smith\
    School Work\
        [etc]

[repeat for all 1300 students]

显然,这种做法扩展性不佳。现在有 3900 个文档文件夹。

使文件夹导航更简单的唯一可能方法是使用 Tab Completion。您可以像这样使用它:

/Schoolwork/$ dir E<tab>[nglish/]A<tab>[ssignments/]

只是为了澄清一下,<tab>表示您按下了键盘上的 Tab 键,而[nglish/]表示 Tab 补全已经为您填写了内容。

答案4

您可以使用色彩控制台,除其他好东西外,你还将获得带有多个 CMD 的选项卡式界面最喜欢的命令,这使得生活变得轻松很多。

替代文本

ColorConsole 是免费软件且可移植(不需要安装)。

相关内容