[英]Windows shell command to get the full path to the current directory?
Is there a Windows command line command that I can use to get the full path to the current working directory?是否有 Windows 命令行命令可用于获取当前工作目录的完整路径?
Also, how can I store this path inside a variable used in a batch file?另外,如何将此路径存储在批处理文件中使用的变量中?
使用cd
不带参数,如果你直接使用的外壳,或%cd%
如果你想在一个批处理文件中使用它(它像一个环境变量)。
You can set a batch/environment variable as follows:您可以按如下方式设置批处理/环境变量:
SET var=%cd%
ECHO %var%
sample screenshot from a Windows 7 x64 cmd.exe.来自 Windows 7 x64 cmd.exe 的示例屏幕截图。
Update: if you do a SET var = %cd%
instead of SET var=%cd%
, below is what happens.更新:如果你执行SET var = %cd%
而不是SET var=%cd%
,下面是会发生什么。 Thanks to jeb.感谢杰布。
Capturing the current directory from a batch file 从批处理文件中捕获当前目录
Quote the Windows help for the set
command ( set /?
):引用set
命令 ( set /?
) 的 Windows 帮助:
If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below: %CD% - expands to the current directory string. %DATE% - expands to current date using same format as DATE command. %TIME% - expands to current time using same format as TIME command. %RANDOM% - expands to a random decimal number between 0 and 32767. %ERRORLEVEL% - expands to the current ERRORLEVEL value %CMDEXTVERSION% - expands to the current Command Processor Extensions version number. %CMDCMDLINE% - expands to the original command line that invoked the Command Processor.
Note the %CD% - expands to the current directory string.
注意%CD% - expands to the current directory string.
part.部分。
On Unix?在 Unix 上?
pwd密码
This has always worked for me:这一直对我有用:
SET CurrentDir="%~dp0"
ECHO The current file path this bat file is executing in is the following:
ECHO %CurrentDir%
Pause
For Windows we can use对于 Windows,我们可以使用
cd
and for Linux对于 Linux
pwd
command is there.命令在那里。
On Windows:在 Windows 上:
CHDIR Displays the name of or changes the current directory. CHDIR显示当前目录的名称或更改当前目录。
In Linux:在 Linux 中:
PWD Displays the name of current directory. PWD显示当前目录的名称。
For Windows, cd
by itself will show you the current working directory.对于 Windows, cd
本身会显示当前工作目录。
For UNIX and workalike systems, pwd
will perform the same task.对于 UNIX 和类似工作的系统, pwd
将执行相同的任务。 You can also use the $PWD
shell variable under some shells.您还可以在某些 shell 下使用$PWD
shell 变量。 I am not sure if Windows supports getting the current working directory via a shell variable or not.我不确定 Windows 是否支持通过 shell 变量获取当前工作目录。
Create a .bat
file under System32
, let us name it copypath.bat
the command to copy current path could be :在System32
下创建一个.bat
文件,让我们将其命名为copypath.bat
复制当前路径的命令可以是:
echo %cd% | clip
Explanation:解释:
%cd%
will give you current path %cd%
会给你当前的路径
CLIP
Description:
Redirects output of command line tools to the Windows clipboard.
This text output can then be pasted into other programs.
Parameter List:
/? Displays this help message.
Examples:
DIR | CLIP Places a copy of the current directory
listing into the Windows clipboard.
CLIP < README.TXT Places a copy of the text from readme.txt
on to the Windows clipboard.
Now copypath
is available from everywhere.现在copypath
可以从任何地方获得。
Based on the follow up question (store the data in a variable) in the comments to the chdir post I'm betting he wants to store the current path to restore it after changeing directories.根据 chdir 帖子的评论中的后续问题(将数据存储在变量中),我敢打赌他想存储当前路径以在更改目录后恢复它。
The original user should look at "pushd", which changes directory and pushes the current one onto a stack that can be restored with a "popd".原始用户应该查看“pushd”,它更改目录并将当前目录推送到可以用“popd”恢复的堆栈上。 On any modern Windows cmd shell that is the way to go when making batch files.在任何现代 Windows cmd shell 上,这是制作批处理文件时要走的路。
If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.如果您确实需要获取当前路径,那么现代 cmd shell 也有一个 %CD% 变量,您可以轻松地将其塞入另一个变量中以供参考。
@echo off
for /f "usebackq tokens=*" %%x in (`chdir`) do set var=%%x
echo The currenct directory is: %var%
But, of course , gmaran23's answer is the much easier one.但是,当然,gmaran23 的答案要容易得多。
In a Windows command prompt, chdir
or cd
will print the full path of the current working directory in the console.在 Windows 命令提示符下, chdir
或cd
将在控制台中打印当前工作目录的完整路径。
If we want to copy the path then we can use: cd | clip
如果我们想复制路径,那么我们可以使用: cd | clip
cd | clip
. cd | clip
。
As one of the possible codes作为可能的代码之一
echo off
for /f "usebackq tokens=* delims= " %%x in (`chdir`) do set var=%var% %%x
echo The current directory is: "%var:~1%"
On Windows, type cd
for the working current path.在 Windows 上,输入cd
作为当前工作路径。
On Linux, pwd
for the current working path.在 Linux 上, pwd
表示当前工作路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.