CD -- 默认情况下设置 /d 开关 问题

问题

有没有办法更改 CD 以便它始终假定 /d 开关 [更改驱动器和路径] 已设置?


进一步的想法/考虑

  1. 我想不出任何我不想包含 /d 的情况(即这实际上会导致问题),但是如果有人知道您可能想要这样做的情况(或可以解释原因)在每个 CD 语句上设置 /d 是个坏主意,请大声喊出来)
  2. 我过去做过类似的事情,通过创建一个简单地调用 CDIR.cmd 的包装 cmd 文件(例如 CD /d %*)...但实际上不确定这种方法(通常)是否推荐,所以我想我不妨先在这里问一下,然后立即以这种方式解决它(?)
  3. 根据 COPY /? -- COPY 有一个环境变量 "COPYCMD",您可以设置以更改默认覆盖行为(以及 COPY 中 /Y 开关的行为)--有没有办法做类似的事情?
icoast 回答:CD -- 默认情况下设置 /d 开关 问题

感谢@Mofi 和@aschipfl 的投入...尽管我之前使用 DOSKEY 取得了喜忧参半的成功,但出于某种原因,我又回去了。安全地测试字符串(参数)比我认为的要困难得多(由于 ",: 和其他字符干扰了 IF 语句),@Mofi 确实试图警告我!我很高兴地说虽然我(最终)成功获得一个可以处理所有 3 种 CD 组合的脚本(1:无参数,2:仅路径,3:/d 和路径),并始终切换驱动器和路径(如有必要)。请参阅 CDD .cmd 下面...

CDD.cmd
-------

@ECHO OFF
SETLOCAL

REM ---------------------------------------------------------------------------
REM This script is intended to be used as a replacement/override to the internal
REM command CD,except that this script will effectively always apply the /d
REM swtich (change drive).

REM Original CD behaviour --------      This script behaviour (imply /d) ------
REM C:\Windows> CD "D:\Files"           C:\Windows> CDD "D:\Files"
REM C:\Windows> (dir not changed!)      D:\Files>   (drive and dir changed)

REM Use at your own discretion,initial tests show it works but can't guarantee.
REM To deploy,copy this script to your %Path% and add [DOSKEY cd=CDD.cmd $*]
REM ---------------------------------------------------------------------------

REM Clone parameters and remove special characters (otherwise strings unsafe)
REM Disclosure: This section may need to be tweaked.
SET Args=%*
SET Args=%Args::=%
SET Args=%Args:"=%
SET "Pre=%Args:~0,2%"

IF "%Pre%"==":=" (
    REM no string supplied,print current dir
    CHDIR
    GOTO:eof
) ELSE IF "%Pre%"=="/d" (
    REM /d parameter was passed,the dir is in %2
    CHDIR /d %2
    GOTO:eof
) ELSE (
    REM /d was not passed - add this to the command,the dir is in %1
    CHDIR /d %1
    GOTO:eof
)

IF %ERRORLEVEL% NEQ 0 (
    ECHO [36mReminder: Calls to CD are being redirected to CDD [%~dpnx0]. ^
It appears there was an error in CDD,please check the path is correct - if it is,^
try using CHDIR/PUSHD this time instead. If you continue to get this message,^
consider de-activating the DOSKEY mapping[0m
    EXIT /B 1
)
本文链接:https://www.f2er.com/378578.html

大家都在问