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条回答
一夜七次
2楼-- · 2019-01-13 07:25

Update 19th Oct 2018.
In case anyone else tries my approach, my original answer below doesnt handle spaces, eg, the following fails.

> cd "c:\Program Files"
Files""]==["~"] was unexpected at this time.

I think there must be a way to solve that. Will post again if I can improve my answer.


My Original Answer, still needs work... 7th Oct 2018.
I was just trying to do it today, and I think I got it, this is what I think works well;

First, some doskey macros;

DOSKEY cd=cdtilde.bat $* 
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..

and then then a bat file in my path;

cdtilde.bat

@echo off
if ["%1"]==["~"] ( 
    chdir /D "%USERPROFILE%"
) else ( 
    chdir /D %* 
)

All these seem to work fine;

cd ~ (traditional habit)
cd~  (shorthand version)
cd.. (shorthand for going up..)
查看更多
看我几分像从前
3楼-- · 2019-01-13 07:25

Just wrote a script to do this without too much typing while maintaining portability as setting ~ to be %userprofile% needs a manual setup on each Windows PC while cloning and setting the directory as part of the PATH is mechanical.

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

查看更多
Anthone
4楼-- · 2019-01-13 07:32

You can also do cd ......\ as many times as there are folders that takes you to home directory. For example, if you are in cd:\windows\syatem32, then cd ....\ takes you to the home, that is c:\

查看更多
登录 后发表回答