【问题标题】:CD -- Setting the /d switch on by defaultCD -- 默认打开 /d 开关
【发布时间】:2021-08-10 23:53:33
【问题描述】:

问题

有没有办法改变CD,所以它总是假定 /d 开关 [更改驱动器和路径] 已设置?


进一步的想法/考虑

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

【问题讨论】:

  • 使用pushd / popd?
  • 没有秘密的 CD 相关环境变量,这很好。原因是如果每个cdcd /D 替换会导致批处理文件上的更改目录命令不起作用,例如包含cd /D "%TEMP%",因为cd /D /D "%TEMP%" 会导致错误消息The syntax of the command is incorrect.。跨度>
  • 您可以定义一个宏:doskeycd=cd /D $*(尽管随后指定 cd /D 失败,原因由 Mofi 解释)...
  • @JosefZ -- 谢谢...我没有使用 PUSHD,但看过其他脚本(有时用它做聪明/复杂的事情!)...我检查了 ss64.com/nt/pushd.html 虽然它似乎比我想象的要简单...我假设在基本层面上,它的工作原理与 CD 完全一样(但也有更换驱动器的优势)?...如果是这样,那么我肯定会考虑是否,但理想情况下会理想情况下喜欢使用/覆盖 CD(通常是在我的脚本中使用 CD 时,但忘记了 /d 我被卡住了(所以某种方式可以“使用 cd 但永远不必记住再次执行 /d”会很棒)
  • 谢谢@Mofi -- 如果没有等效的“COPYCMD”环境。变量那么很高兴知道,并且至少可以关闭考虑的选项......注意:我明白你关于多个 /D 开关的可能性的观点,以及这将如何破坏 CD......虽然,我想办法避免这你可以检查 /d 是否已经设置,如果没有设置,则只设置(惰性到字符串),对吧? (因为它必须是第一个选项,这将有助于简化检查,因为您只需要检查前 5 个字符(小写)是否 = "cd /d")

标签: cmd parameters default-value cd


【解决方案1】:

感谢@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
)

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多