我正在尝试使用命令提示符来移动一些文件,我已经习惯了使用 ~
指定我的主目录的 linux 终端,我到处查看,但我似乎无法为 windows 命令找到它提示 (Documents and Settings\[user]
)
你会失望的:%userprofile%
不过,您可以使用其他终端。 Powershell,我相信你可以在 XP 和更高版本上使用(并且预装在 Win7 中),它允许你使用 ~
作为主目录。
您可以为驱动器 %HOMEDRIVE%%HOMEPATH%
+ \docs settings\username
或 \users\username
。
您可以使用 %homedrive%%homepath%
环境变量来完成此操作。
连接时的两个命令变量为您提供所需的用户主目录路径,如下所示:
在命令提示符上运行 echo %homedrive% 给出: C: 在命令提示符下运行 echo %homepath% 给出: \Users\
当与 %homedrive%%homepath%
一起使用时,它给出:
C:\Users\<CurrentUserName>
%homedrive%
等效的 %systemdrive%
环境变量。如果您发现任何不正确的地方,请务必给我您的反馈。
%HOMEDRIVE%%HOMEPATH%
是正确答案。因此,%HOMEDRIVE%%HOMEPATH%
在所有情况下都是正确答案,我建议您相应地更改您的答案。
更新 - 2019 年 7 月 18 日更好的版本。
最后总结一下,尽管我已经为大多数 Windows 控制台工作转向了 powershell,但我决定解决这个旧的 cmd 问题,我今天不得不使用 cmd 控制台,并且缺少这个功能真的让我很震惊。这个最终也适用于空格,我之前的答案会失败。
此外,这个现在也可以使用 ~ 作为其他主子文件夹的前缀,并且它也将正斜杠交换为反斜杠。所以就在这里;
步骤 1. 创建这些 doskey 宏,每次 cmd 启动时都会在某个地方获取它们。
DOSKEY cd=cdtilde.bat $*
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..
步骤 2. 创建 cdtilde.bat 文件并将其放在 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%"
测试良好;
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")
哦,它还允许延迟引用,我发现这很有用,即使文件夹路径名中有空格,因为它包含所有参数,就好像它是一个长字符串一样。这意味着只有一个初始报价也可以,或者完全没有报价也可以。
下面所有其他的东西现在可能会被忽略,它是出于历史原因而留下的——所以我不会再犯同样的错误
旧更新 2018 年 10 月 19 日。如果其他人尝试了我的方法,我在下面的原始答案没有处理空格,例如,以下失败。
> cd "c:\Program Files"
Files""]==["~"] was unexpected at this time.
我认为必须有办法解决这个问题。如果我可以改进我的答案,将再次发布。 (见上文,我终于按照我想要的方式完成了所有工作。)
我的原始答案,仍然需要工作...... 2018 年 10 月 7 日。我今天只是想这样做,我想我明白了,这是我认为行之有效的方法;
首先是一些doskey宏;
DOSKEY cd=cdtilde.bat $*
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..
然后在我的路径中创建一个 bat 文件;
cdtilde.bat
@echo off
if ["%1"]==["~"] (
chdir /D "%USERPROFILE%"
) else (
chdir /D %*
)
所有这些似乎都可以正常工作;
cd ~ (traditional habit)
cd~ (shorthand version)
cd.. (shorthand for going up..)
.bashrc
类型的文件启动 CMD:superuser.com/questions/144347/…。将此答案中的步骤 1. 放入 bashrc.bat
文件将在启动时设置它。
我刚刚尝试了 set ~=%userprofile%
,如果您想继续使用相同的习惯,它也有效
然后,您可以改用 %~%
。
~
= %userprofile%
的值?我在命令提示符下运行了您帖子中提到的确切命令,但它引发错误 - '~' is not recognized as an internal or external command, operable program or batch file.
。你能详细说明你的答案吗?
set ~=%userprofile%
,那么您可以使用 %~%
作为变量
${env:~}="$env:homedrive$env:homepath"
。
如果你想要更短的 Jay's 版本,你可以试试
set usr=%userprofile%
cd %usr%
或者,如果您愿意,您甚至可以使用 %u%。无论如何,它可以节省一些击键。
你自己几乎可以做同样的事情。打开环境变量并单击“用户变量...”中的“新建”按钮。变量名称:~ 变量值:单击“浏览目录...”按钮并选择您想要的目录。
在此之后,打开 cmd 并输入: cd %~% 。有用。
简单地
首先定义路径
doskey ~=cd %homepath%
然后访问
~
使用 %systemdrive%%homepath%
。 %systemdrive%
提供驱动器字符(主要是 C:
)并且 %homepath%
提供用户主目录( \Users\<USERNAME>
)。
# cmd
# use: %USERPROFILE%
# example:
kubectl config --kubeconfig=%USERPROFILE%\.kube\config.xyz view
# powershell
# use: $env:USERPROFILE
# example
kubectl config --kubeconfig=$env:USERPROFILE\.kube\config.xyz view
只需编写一个脚本即可执行此操作而无需过多输入,同时保持可移植性,因为将 ~
设置为 %userprofile%
需要在每台 Windows PC 上手动设置,而将目录克隆和设置为 PATH
的一部分是机械的。
https://github.com/yxliang01/Snippets/blob/master/windows/
您也可以执行 cd ......\ 与将您带到主目录的文件夹一样多的次数。例如,如果你在 cd:\windows\syatem32,那么 cd ....\ 带你到家,即 c:\
C:\Users\username
文件夹(或早期 Windows 中的 c:\Documents and Settings\username
)
C:\
,建议使用 cd \
而不是未定义数量的 cd ..\..\..
~
->但讽刺的是,%userprofile%:Set-Location: Cannot find path 'C:\%userprofile%' because it does not exist.
$env:userprofile
。cd $env:userprofile
工作得很好。除此之外,C:\%userprofile%
将始终是无效路径,因为 environmnet 变量已经包含驱动器号。