What is the alternative for ~ (user's home dir

2019-01-13 06:30发布

I'm trying to use the command prompt to move some files, I am used to the linux terminal where I use ~ to specify the my home directory I've looked everywhere but I couldn't seem to find it for windows command prompt (Documents and Settings\[user])

9条回答
\"骚年 ilove
2楼-- · 2019-01-13 07:13

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

查看更多
\"骚年 ilove
3楼-- · 2019-01-13 07:13

You can do almost the same yourself. Open Environment Variables and click "New" Button in the "User Variables for ..." .
Variable Name: ~
Variable Value: Click "Browse Directory..." button and choose a directory which you want.

And after this, open cmd and type this:
cd %~%
. It works.

查看更多
forever°为你锁心
4楼-- · 2019-01-13 07:13

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

You can then use %~% instead.

查看更多
虎瘦雄心在
5楼-- · 2019-01-13 07:14

You're going to be disappointed: %userprofile%

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

查看更多
在下西门庆
6楼-- · 2019-01-13 07:17

You can use %systemdrive%%homepath% environment variable to accomplish this.

The two command variables when concatenated gives you the desired user's home directory path as below:

Running echo %systemdrive% on command prompt gives:

C:

Running echo %homepath% on command prompt gives:

\Users\<CurrentUserName>

When used together it becomes:

C:\Users\<CurrentUserName>
查看更多
甜甜的少女心
7楼-- · 2019-01-13 07:22

If you want a shorter version of Jay's you could try

    set usr=%userprofile%
    cd %usr%

Or you could even use %u% if you wanted to. It saves some keystrokes anyway.

查看更多
登录 后发表回答