在 Windows CMD Prompt 中仅显示当前目录名称(而不是完整路径)

lag*_*lex 7 windows command-line prompt cmd.exe

在 Windows CMD.EXE 中,我将提示字符串设置为$P$G,因此,如果我当前的工作目录是

C:\Some\long\folder\inner
Run Code Online (Sandbox Code Playgroud)

我的提示是这样的:

C:\Some\long\folder\inner>
Run Code Online (Sandbox Code Playgroud)

我希望它只显示最后一个(最低级别)文件夹名称,如下所示:

inner>
Run Code Online (Sandbox Code Playgroud)

其中“inner”只是最内层文件夹的名称,它应该自动更改为我当前所在目录的最内层文件夹 - 相当于仅显示当前目录名称(不是完整路径)中讨论的功能 ) 在 bash 提示符下。我怎样才能做到这一点?

Sco*_*ott 4

如果您愿意稍微改变一下习惯,这里有一个解决方法。

\n

首先,在 PATH 中选择一个可以写入\xc2\xa0的目录。\xc2\xa0\n假设你在自己的计算机上,并且拥有管理员权限,\n你可以使用 或 .\ \\Windowsxc2 \xa0 \n但使用类似.\xc2\xa0\n 的东西可能会更好\n(如果你还没有,请将\xc2\xa0it添加到你的路径中。)\\Program\xc2\xa0Files\\something\\Users\\username\\bin

\n

然后创建一个名为CH.BAT或类似名称的文件,\n其中包含以下内容:

\n
@echo off\nREM Pass the argument(s) to the real "cd".  If it fails, don't do anything else.\ncd %*  ||  exit /b\nREM Get our current directory name.\nset "dirname=%CD%"\n:loop\nREM Strip off one directory level -- remove everything through the first \\.\nset "remove_one=%dirname:*\\=%"\nREM If there's nothing left, that means dirname ENDS with a \\.\nREM The only (?) way this can happen is if it is the root directory\nREM of a filesystem (partition), e.g., C:\\.\nREM In this case, set the prompt to the normal thing, C:\\>.\nif "%remove_one%" == ""          goto exit_loop\nREM If "%remove_one%" == "%dirname%", that means we are down to\nREM the last directory name (i.e., there are no backslashes left).\nif "%remove_one%" == "%dirname%" goto exit_loop\nset "dirname=%remove_one%"\nREM Keep on removing levels until we get to the bottom.\ngoto loop\n\n:exit_loop\nREM To handle the case where a directory name contains dollar sign(s)\nREM (e.g., $P$G), replace each $ with $$ to remove its special meaning\nREM in the prompt, and just display a $.\nset "dirname=%dirname:$=$$%"\nprompt %dirname%$G\n
Run Code Online (Sandbox Code Playgroud)\n

然后养成打字ch而不是cd\n(或chdir,如果您足够大,可以记住它的 \xc2\xa0 原始名称\n(仍然受支持))。\xc2\xa0\n我 \xc2\xa0 相信评论解释得相当好,但是,概括一下:

\n
    \n
  • 调用cd命令行参数。\xc2\xa0\n如果\xc2\xa0it\xc2\xa0失败,则退出脚本。
  • \n
  • 使用变量替换\n( )\n去除目录级别。\xc2\xa0\n循环直到\xc2\xa0do 没有任何内容。\xc2\xa0\n如果\xc2\xa0当前目录是根目录(例如,\xc2 \xa0 ),\n那么提示符将是\n(因为你没有指定如何处理这种情况)。\xc2\xa0\n如果\xc2\xa0it 类似于,\n你会得到%varname:old=new%C:\\C:\\>C:\\top\\outer\\middle\\innerinner>, 。
  • \n
  • 我们知道,美元符号在提示符中是特殊的。\xc2\xa0\n但它们在目录名称中是合法的。\xc2\xa0\n您可以通过将提示符中的美元符号加倍来转义;\ni.e,\xc2 \xa0如果输入$$提示符,它将显示为\xc2\xa0 $。\xc2\xa0\n所以\xc2\xa0我们将目录名称中的所有美元符号\n替换为$$以使它们正确显示。
  • \n
  • 该脚本将当前目录名称硬编码到提示字符串中,\n因此,如果您随后意外键入cd而不是 \xc2\xa0 ch,\n您的提示将不会更改。\xc2\xa0\n如果发生\xc2\xa0,只需键入\xc2\xa0 ch(或 \xc2\xa0 )\n到 \xc2\xa0 根据新的当前目录设置提示符。ch\xc2\xa0.
  • \n
  • 如果您想查看当前目录的完整路径名,请键入\xc2\xa0 cd(或\xc2\xa0 echo\xc2\xa0%CD%)。
  • \n
  • 我已经使用包含空格(和美元符号)的目录名称对此进行了测试,但我尚未测试每个合法字符。\xc2\xa0\n如果您发现\xc2\xa0问题,请告诉我。
  • \n
\n

CD.BAT当然,将脚本命名为或并没有什么好处CHDIR.BAT,\n因为 CMD.EXE 总是解释cd\xc2\xa0 chdir\nas \xe2\x80\x9cchange 目录\xe2\x80\x9d 内置命令。\xc2\xa0\你\xc2\xa0可以通过\xc2\xa0输入\xc2\xa0路径名来运行这样的\xc2\xa0脚本,\n但\n\nxc2\xa0显然\xe2\x80\x99s不可行(从\xc2\xa0工作流程的角度来看)\nas an\ xc2\xa0 覆盖/替换 \xc2\xa0 cd

\n