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
)