我正在尝试使用命令提示符移动一些文件,
我习惯于使用Linux终端,我使用~
指定我的主目录
我已经到处找了,但似乎在Windows命令提示符中找不到它(Documents and Settings\[user]
)
我正在尝试使用命令提示符移动一些文件,
我习惯于使用Linux终端,我使用~
指定我的主目录
我已经到处找了,但似乎在Windows命令提示符中找不到它(Documents and Settings\[user]
)
%userprofile%
。而你可以使用其他终端,比如PowerShell,我相信你可以在XP及更高版本上获得它(并且Win7预装了它),它允许你使用~
代表主目录。~
,但具有讽刺意味的是不允许使用%userprofile%:Set-Location: Cannot find path 'C:\%userprofile%' because it does not exist.
- mcalex$env:userprofile
。 cd $env:userprofile
可以正常工作。此外,C:\%userprofile%
将始终是无效路径,因为环境变量已经包括了驱动器号。 - binfordC:\%userprofile%
,而不是@mcalex键入的内容,此时的工作目录为C:\。 - craig65535%userprofile%
是 C:\Users\me
,但 %homedrive%%homepath%
指向映射的 U:\
驱动器的根目录。 - Aaron Christiansen您可以使用%homedrive%%homepath%环境变量来实现此操作。
将这两个命令变量连接在一起,即可得到所需用户的主目录路径,如下所示:
在命令提示符上运行echo %homedrive%,将会显示:
C:
在命令提示符下运行echo %homepath%,将显示:
\Users\<CurrentUserName>
将%homedrive%%homepath%组合在一起使用,它将产生:
C:\Users\<CurrentUserName>
%homedrive%
的等价物%systemdrive%
环境变量。如果您发现任何不正确的地方,请给我反馈。 - RBT更新 - 更好的版本 2019年7月18日。
最终总结,即使我已经转向使用PowerShell进行大多数Windows控制台工作,但我决定解决这个旧的cmd问题,因为今天我必须进入cmd控制台,缺少这个功能真的让我感到困扰。这个新版本也能够使用~作为其他主目录子文件夹的前缀,并将正斜杠替换为反斜杠。所以这就是它:
步骤1.创建这些doskey宏,在每次cmd启动时都会被捕获。
DOSKEY cd=cdtilde.bat $*
DOSKEY cd~=chdir /D "%USERPROFILE%"
DOSKEY cd..=chdir ..
@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 ..
然后将一个批处理文件放在我的路径中;
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的方法: https://superuser.com/questions/144347/is-there-windows-equivalent-to-the-bashrc-file-in-linux。将该答案中的第1步放入`bashrc.bat`文件中,就可以在启动时设置它。 - Brydenr我刚刚尝试了 set ~=%userprofile%
,如果你想继续保持相同的习惯,这也是可行的。
然后你可以使用 %~%
,代替原来的命令。
~
的值设置为%userprofile%
?我在命令提示符中运行了您帖子中提到的确切命令,但它会抛出错误 -“'~'不被识别为内部或外部命令,可执行程序或批处理文件。”。您能否详细说明一下您的答案? - RBTset ~=%userprofile%
,那么你就可以使用 %~%
来代表这个变量。 - forresthopkinsa${env:~}="$env:homedrive$env:homepath"
。 - Ken Williams# 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
set usr=%userprofile%
cd %usr%
如果你愿意的话,甚至可以使用%u%。这样能够省去一些按键的操作。
之后,在cmd中输入以下内容:
cd %~%
它会生效。
简单地说
首先定义路径
doskey ~=cd %homepath%
~
%systemdrive%%homepath%
。 %systemdrive%
给出驱动器字符(大多数情况下是C:
),而 %homepath%
给出用户主目录(\Users\<USERNAME>
)。