Windows 命令提示符上的 ~(用户主目录)的替代方案是什么?
我正在尝试使用命令提示符移动一些文件, 我习惯在linux终端中使用~
来指定我的主目录 我到处都找过了,但似乎找不到 Windows 命令提示符(Documents and Settings\[user]
)
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]
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您想要 Jay's 的较短版本,您可以尝试
或者如果您愿意,您甚至可以使用 %u%。无论如何,它可以节省一些击键次数。
If you want a shorter version of Jay's you could try
Or you could even use %u% if you wanted to. It saves some keystrokes anyway.
您自己也可以做几乎同样的事情。打开环境变量并单击“用户变量...”中的“新建”按钮。
变量名称: ~
变量值:单击“浏览目录...”按钮并选择您想要的目录。
之后,打开cmd并输入:
cd %~%
。有用。
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.
简单
Simply
使用
%systemdrive%%homepath%
。%systemdrive%
给出驱动器字符(主要是C:
),%homepath%
给出用户主目录(\Users\;
)。Use
%systemdrive%%homepath%
.%systemdrive%
gives drive character ( MostlyC:
) and%homepath%
gives user home directory (\Users\<USERNAME>
).只需编写一个脚本即可完成此操作,无需太多输入,同时保持可移植性,因为将
~
设置为%userprofile%
需要在克隆和设置目录时在每台 Windows PC 上进行手动设置作为PATH
的一部分是机械的。https://github.com/yxliang01/Snippets/blob/master/windows/< /a>
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 thePATH
is mechanical.https://github.com/yxliang01/Snippets/blob/master/windows/
cd ~ 在 Windows powershell 上可以正常工作,如果您遇到不受支持的命令,请确保您使用的是 powershell 而不是 CMD 或使用 Windows GIT bash。
cd ~ will work fine on windows powershell, if you experience unsupported commands make sure you are using powershell instead of CMD or use windows GIT bash.
您还可以执行 cd ......\ 多次,只要有文件夹即可将您带到主目录。例如,如果您位于 cd:\windows\syatem32,则 cd ....\ 将带您到家,即 c:\
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:\
您将会失望:
%userprofile%
不过,您可以使用其他终端。 Powershell,我相信您可以在 XP 及更高版本上使用(并且随 Win7 一起预装),它允许您使用
~
作为主目录。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.您可以
%HOMEDRIVE%%HOMEPATH%
驱动器 +\docs settings\username
或\users\username
。You can
%HOMEDRIVE%%HOMEPATH%
for the drive +\docs settings\username
or\users\username
.您可以使用 %homedrive%%homepath% 环境变量来完成此操作。
连接两个命令变量后,您将获得所需用户的主目录路径,如下所示:
在命令提示符下运行 echo %homedrive% 会给出:
<前><代码>C:
在命令提示符下运行 echo %homepath% 给出:
当与 %homedrive%%homepath% 一起使用时,它会给出:
You can use %homedrive%%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 %homedrive% on command prompt gives:
Running echo %homepath% on command prompt gives:
When used together %homedrive%%homepath% it gives:
更新 - 更好的版本 2019 年 7 月 18 日。
最终总结,尽管我已经转向 powershell 来完成大多数 Windows 控制台工作,但我决定解决这个旧的 cmd 问题,我必须继续今天有一个cmd控制台,缺乏这个功能确实让我震惊。这个最终也适用于空格,而我之前的答案将失败。
此外,现在还可以使用 ~ 作为其他主子文件夹的前缀,并且还将正斜杠交换为反斜杠。就是这样;
步骤 1. 创建这些 doskey 宏,每次 cmd 启动时都会在某个地方拾取它们。
步骤 2. 创建 cdtilde.bat 文件并将其放在 PATH 中的某个位置
进行测试;
哦,它还允许惰性引用,我发现这很有用,即使文件夹路径名中存在空格,因为它包装了所有参数,就像它是一个长字符串一样。这意味着只有初始引用也可以,或者完全没有引号也可以。
下面的所有其他内容现在可能会被忽略,它是由于历史原因而留下的 - 所以我不会再犯同样的错误
旧更新 2018 年 10 月 19 日。
如果其他人尝试了我的方法,我下面的原始答案没有处理空格,例如,以下失败。
我想一定有办法解决这个问题。如果我可以改进我的答案,我会再次发布。 (见上文,我终于按照我想要的方式一切工作了。)
我的原始答案,仍然需要工作...... 2018 年 10 月 7 日。
我今天只是想这么做,我想我做到了,这就是我认为效果很好的方法;
首先,一些doskey宏;
然后是我的路径中的一个bat文件;
cdtilde.bat
所有这些似乎都工作正常;
Update - better version 18th July 2019.
Final summary, even though I've moved on to powershell for most windows console work anyway, but I decided to wrap this old cmd issue up, I had to get on a cmd console today, and the lack of this feature really struck me. This one finally works with spaces as well, where my previous answer would fail.
In addition, this one now is also able to use ~ as a prefix for other home sub-folders too, and it swaps forward-slashes to back-slashes as well. So here it is;
Step 1. Create these doskey macros, somewhere they get picked up every time cmd starts up.
Step 2. Create the cdtilde.bat file and put it somewhere in your PATH
Tested fine with;
Oh, also it allows lazy quoting, which I found useful, even when spaces are in the folder path names, since it wraps all of the arguments as if it was one long string. Which means just an initial quote also works, or completely without quotes also works.
All other stuff below may be ignored now, it is left for historical reasons - so I dont make the same mistakes again
old update 19th Oct 2018.
In case anyone else tried my approach, my original answer below didn't handle spaces, eg, the following failed.
I think there must be a way to solve that. Will post again if I can improve my answer. (see above, I finally got it all working the way I wanted it to.)
My Original Answer, still needed 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;
and then then a bat file in my path;
cdtilde.bat
All these seem to work fine;
我刚刚尝试了
set ~=%userprofile%
,如果您想继续使用相同的习惯,这也有效,然后您可以使用
%~%
代替。I just tried
set ~=%userprofile%
and that works too if you want to keep using the same habitYou can then use
%~%
instead.