-1

Question

Is there a way to change CD so it always assumes the /d switch [change drive as well as path] is set?


Further thoughts / considerations

  1. I can't think of any situation where I wouldn't want to include /d (i.e. where this would actually cause problems), but if anyone knows of a case where you might want to do this (or can explain why setting /d on every CD statement is a bad idea, please shout out)
  2. I've done similar things in the past by creating a wrapper cmd file (e.g. CDIR.cmd) that would simply call CD /d %*... but not actually sure if that approach (in general) is recommended, so thought I might as well ask here first before jumping to solve it this way straight away(?)
  3. According to COPY /? -- COPY has a environment variable "COPYCMD" you can set to change the default override behaviour (and so the behaviour of the /Y switch in COPY) -- is there an option to do something like that?
Martin
  • 280
  • 1
  • 10
  • Use [`pushd`](https://ss64.com/nt/pushd.html) / [`popd`](https://ss64.com/nt/popd.html)? – JosefZ May 21 '21 at 18:44
  • There is no secret __CD__ related environment variable and that is good so. The reason is that if every `cd` being replaced by `cd /D` would result in a not working change directory command on a batch file contains for example `cd /D "%TEMP%"` because of `cd /D /D "%TEMP%"` results in the error message `The syntax of the command is incorrect.`. – Mofi May 21 '21 at 19:39
  • You could define a macro: [`doskey`](https://ss64.com/nt/doskey.html)`cd=cd /D $*` (although then specifying `cd /D` failed for the reason explained by Mofi)… – aschipfl May 21 '21 at 21:05
  • @JosefZ -- Thanks... I haven't used PUSHD but seen it other scripts (sometimes doing clever / complicated things with it!)... I checked on https://ss64.com/nt/pushd.html though and it seems simpler than I thought... I assume at a basic level, it works exactly like CD right (but also has the advantage of changing the drive too)?... If so, then I will definitely consider if, but ideally would like to use/override CD ideally (as typically, it's when I use `CD` in my script, but forget about /d that I get stuck (so some way to "use cd but never have to remember to do /d again" would be great) – Martin May 21 '21 at 23:31
  • Thanks @Mofi -- If there's no equivalent "COPYCMD" env. variable then that's good to know, and can at least close that option of consideration... NB: I get your point about the potential for multiple /D switches, and how this would break CD... Although, I suppose way to avoid this you could check if /d is set already and then only set (inert into string) if not set, right? (and as it has to be the first option, this would help simplify the check as you just need to check if the first 5 characters (lowercase) = "cd /d") – Martin May 21 '21 at 23:32
  • @aschipfl -- Thanks for the suggestion! I'd actually forgotten that I'd tried this a few months ago (discovered DOSKEY and got a bit carried away, lol)... To share my experience with DOSKEY, I tried to use it like this (as a way to shorten existing cmds or pre-set params, etc but I found it didn't work like that and ultimately seemed that it did not pass any parameters / it only called the command string as it was saved in DOSKEY. (This is why I switched the wrapping .cmd files instead)... So, my advice would keep DOSKEY for simple, static shortcuts and not use it as a "partial command" tool – Martin May 21 '21 at 23:42
  • It is no good idea to replace an internal command of the Windows command processor with a DOSKEY macro. There must be too many different use cases taken into account to get it working. There are so many people using command __CD__ with an usual, but working syntax like ``cd\`` or `cd..` that the effort to replace every `cd` by `cd /D` in a safe and secure manner is very tricky. Even `cmd.exe` itself fails to find out what is meant by a user, for example if root of current drive contains the folder `Download` and a users uses `cd /Download` (not working) instead of `cd \Download` (working). – Mofi May 22 '21 at 09:09
  • Imagine a script (simplified; the paths could be much longer) like: `cd H:\MyBackup` followed by `copy * h:` (no safe syntax, but I've seen it often and use it sometimes myself (although not in a script). Defaulting `cd /d` would ruin that completely. (Impossible altogether with `pushd`). – Stephan Jun 04 '21 at 09:56
  • @Stephan - That's true... TBH, personally hadn't thought about combining CD with other commands (COPY, etc) but I realise that some people might want to do this / the safe operation of CD needs to be protected. In fairness, it was so tricky to get the attached script working, I have generally opted to _try_ and remember to use PUSHD instead (however is hard as have used CD for 20+ years, so is not easy to un-learn). NB: As a side note... This whole process has been quite surprising - injecting "/d" seemed like such a trivial change, but after hours of pain, I have to admit is definitely not! – Martin Jun 04 '21 at 10:09
  • expect [nothing to be trivial](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) when it comes to batch `;P` – Stephan Jun 04 '21 at 10:15

1 Answers1

0

Thanks to @Mofi and @aschipfl for their input... Despite my previous mixed-success with DOSKEY, for some reason I went back and had another go. Testing the strings (parameters) safely was much more difficult that I thought (due to ", : and other characters interfering with the IF statement), which @Mofi did try to warn me about! Am pleased to say though I was (eventually) successful it getting a script that would handle all 3 combinations of CD (1: no parameters, 2: just path, 3: /d and path), and always switch the drive as well as the path (if necessary). Please see CDD.cmd below...

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
)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Martin
  • 280
  • 1
  • 10