The update is the best version on July 18, 2019.
The final summary, although I still switched to powershell for most tasks dealing with the Windows console, but I decided to complete this old problem with cmd, I had to switch to the cmd console today, and the lack of this function struck me. Finally, this one works with spaces too, where my previous answer will fail.
In addition, now it can also use ~ as a prefix for other home subfolders, and also changes forward and backward slashes. So here it is;
Step 1. Create these doskey macros, wherever they appear each time cmd starts.
DOSKEY cd=cdtilde.bat $* DOSKEY cd~=chdir /D "%USERPROFILE%" DOSKEY cd..=chdir ..
Step 2. Create the cdtilde.bat file and place it somewhere in your PATH
@echo off set dirname="" set dirname=%* set orig_dirname=%* :: remove quotes - will re-attach later. set dirname=%dirname:\"=% set dirname=%dirname:/"=% set dirname=%dirname:"=% :: restore dirnames that contained only "/" if "%dirname%"=="" set dirname=%orig_dirname:"=% :: strip trailing slash, if longer than 3 if defined dirname if NOT "%dirname:~3%"=="" ( if "%dirname:~-1%"=="\" set dirname="%dirname:~0,-1%" if "%dirname:~-1%"=="/" set dirname="%dirname:~0,-1%" ) set dirname=%dirname:"=% :: if starts with ~, then replace ~ with userprofile path if %dirname:~0,1%==~ ( set dirname="%USERPROFILE%%dirname:~1%" ) set dirname=%dirname:"=% :: replace forward-slashes with back-slashes set dirname="%dirname:/=\%" set dirname=%dirname:"=% chdir /D "%dirname%"
Checked in order with;
cd ~ (traditional habit) cd~ (shorthand version) cd.. (shorthand for going up..) cd / (eg, root of C:) cd ~/.config (eg, the .config folder under my home folder) cd /Program Files (eg, "C:\Program Files") cd C:/Program Files (eg, "C:\Program Files") cd \Program Files (eg, "C:\Program Files") cd C:\Program Files (eg, "C:\Program Files") cd "C:\Program Files (eg, "C:\Program Files") cd "C:\Program Files" (eg, "C:\Program Files")
Oh, it also allows lazy quotes, which I found useful even when there are spaces in the folder names, as it wraps all arguments as if it were a single long line. This means that only the initial quote also works, or completely without quotes also works.
All other things below can now be ignored, they are left for historical reasons, so I do not repeat the same mistakes
old update October 19, 2018.
In case someone tried my approach, my original answer below did not handle spaces, for example, the following failed.
> cd "c:\Program Files" Files""]==["~"] was unexpected at this time.
I think there must be a way to solve this. I will post it again if I can improve my answer. (see above, I finally earned everything as I wanted.)
My original answer, work is still needed ... October 7, 2018.
I was just trying to do it today, and I think I got it, this is what I think works fine;
First, a few doskey macros;
DOSKEY cd=cdtilde.bat $* DOSKEY cd~=chdir /D "%USERPROFILE%" DOSKEY cd..=chdir ..
and then a bat file in my path;
cdtilde.bat
@echo off if ["%1"]==["~"] ( chdir /D "%USERPROFILE%" ) else ( chdir /D %* )
All of this seems to work fine;
cd ~ (traditional habit) cd~ (shorthand version) cd.. (shorthand for going up..)