What is the alternative for ~ (custom home directory) on a windows command prompt?

I'm trying to use the command line to move some files, I'm used to the linux terminal, where I use ~ to specify my home directory I looked everywhere, but I could not find it for the Windows command line ( Documents and Settings\[user] )

+165
command command-prompt home-directory
Feb 10 2018-12-12T00:
source share
9 answers

You will be disappointed: %userprofile%

However, you can use other terminals. Powershell, which I believe you can get on XP and later (and comes preinstalled with Win7), allows you to use ~ for your home directory.

+255
Feb 10 2018-12-12T00:
source share

You can %HOMEDRIVE%%HOMEPATH% for drive + \docs settings\username or \users\username .

+34
Feb 10 2018-12-12T00:
source share

You can use the %systemdrive%%homepath% environment variable for this.

When combining the two command variables, you will get the desired path to the user's home directory, as shown below:

  1. Running echo %systemdrive% on the command line gives:

     C: 
  2. Running echo %homepath% on the command line gives:

     \Users\<CurrentUserName> 

When used together, it becomes:

 C:\Users\<CurrentUserName> 
+11
Mar 23 '17 at 2:19 on
source share

You can do almost the same thing. Open the environment variables and click the "Create" button in "Custom Variables for ...".
Variable Name: ~
Variable value: Click the "Browse Directory ..." button and select the directory you want.

And after that open cmd and enter this:
cd% ~%
. He works.

+3
May 9 '17 at 6:38
source share

If you want a shorter version of Jay, you can try

  set usr=%userprofile% cd %usr% 

Or you could even use% u% if you want. In any case, this saves some keystrokes.

+2
Jan 10 '16 at 23:13
source share

I just tried set ~=%userprofile% and it works too if you want to keep using the same habit

You can use %~% instead.

+2
Oct 24 '16 at 20:11
source share

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..) 
+2
Oct 07 '18 at 12:48
source share

I just wrote a script to do this without typing too much, while maintaining portability, since installing ~ for %userprofile% requires manual configuration on every Windows PC when cloning and installing the directory as part of PATH mechanically.

https://github.com/yxliang01/Snippets/blob/master/windows/

0
Mar 24 '17 at 20:01
source share

You can also execute cd ...... \ as many times as many folders will take you to your home directory. For example, if you are in cd: \ windows \ syatem32, then cd .... \ will bring you to the house, that is, c: \

-four
Sep 21 '18 at 5:24
source share



All Articles