ChatGPT解决这个技术问题 Extra ChatGPT

Windows shell 命令获取当前目录的完整路径?

是否有可用于获取当前工作目录的完整路径的 Windows 命令行命令?

另外,如何将此路径存储在批处理文件中使用的变量中?

find /dir/to/start/from -type f -ls 将日期格式化为数字 find /dir/to/start/from -type f -exec ls -l --time-style="+ %Y %m % e %H:%M" {} \;

P
Peter Mortensen

如果您直接使用 shell,请使用不带参数的 cd,或者如果您想在批处理文件中使用它(它的行为类似于环境变量),请使用 %cd%


你是如何理解他想要表达的意思的?而且,在dos和windows cmd下,它通常只是“cd”
老实说,正如问题所述,我想不出他们可能试图问的任何其他问题。
我可以将此路径存储在 .bat 文件中的变量中吗?
@unknown - 首先描述原始问题可能会更好。
g
gmaran23

您可以按如下方式设置批处理/环境变量:

SET var=%cd%
ECHO %var%

来自 Windows 7 x64 cmd.exe 的示例屏幕截图。

https://i.stack.imgur.com/6s6Xi.jpg

更新:如果您执行 SET var = %cd% 而不是 SET var=%cd% ,会发生以下情况。感谢杰布。

https://i.stack.imgur.com/S2Kmp.jpg

Capturing the current directory from a batch file


根据问题,这个答案实际上应该是公认的。
但它不起作用,因为 SET var = %cd% 将变量 var<space> 中的值而不是 var。您应该避免在 SET 命令中使用空格
根据问题,这个答案实际上应该是公认的。
P
Patrick Cuff

引用 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.

注意 %CD% - expands to the current directory string. 部分。


S
Stephen Curial

在 Unix 上?

密码


OP 最初要求一个“命令”并且没有指定操作系统。现在已指定操作系统,因此此答案不再相关。
D
Danny Beckett

这一直对我有用:

SET CurrentDir="%~dp0"

ECHO The current file path this bat file is executing in is the following:

ECHO %CurrentDir%

Pause

这样做是错误的 - 找到批处理脚本的路径,而不是当前目录。
由于回答了 OP 的问题而被投票赞成......并为我提供了我正在寻找的确切解决方案。谢谢!
不错的收获。当前目录可以引用调用文件的命令,而不是文件的位置本身。
N
Nikunj K.

对于 Windows,我们可以使用

cd

对于 Linux

pwd

命令在那里。


V
Vishrant

System32 下创建一个 .bat 文件,我们将其命名为 copypath.bat 复制当前路径的命令可以是

echo %cd% | clip

说明:

%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.

现在 copypath 随处可用。


您只需“cd | clip”即可将当前路径复制到剪贴板
P
Peter Mortensen

在 Windows 上:

CHDIR 显示或更改当前目录的名称。

在 Linux 中:

PWD 显示当前目录的名称。


M
Michael Trausch

对于 Windows,cd 本身会显示当前工作目录。

对于 UNIX 和类似系统,pwd 将执行相同的任务。您还可以在某些 shell 下使用 $PWD shell 变量。我不确定 Windows 是否支持通过 shell 变量获取当前工作目录。


但是我不明白为什么他需要“cd”来查看他当前的目录。默认情况下,它显示为一天。如果他改变了它,那么他当然知道“cd”是做什么的。
很旧,但只是为了完整性:是的,windows 确实为此提供了一个变量。它被命名为(猜...)%cd%
M
Mark

基于对 chdir 帖子的评论中的后续问题(将数据存储在变量中),我打赌他想存储当前路径以在更改目录后恢复它。

原始用户应该查看“pushd”,它会更改目录并将当前目录推送到可以使用“popd”恢复的堆栈上。在任何现代 Windows cmd shell 上,这是制作批处理文件的方式。

如果您真的需要获取当前路径,那么现代 cmd shell 也有一个 %CD% 变量,您可以轻松地将其塞入另一个变量中以供参考。


P
Peter Mortensen

在 Windows 命令提示符中,chdircd 将在控制台中打印当前工作目录的完整路径。

如果我们想复制路径,那么我们可以使用:cd | clip


R
René Nyffenegger
@echo off
for /f "usebackq tokens=*" %%x in (`chdir`) do set var=%%x
echo The currenct directory is: %var%

但是,当然,gmaran23 的答案要简单得多。


根据定义,他的答案更好,因为您的答案是错误的。它不适用于带有空格的路径。
添加 tokens=* 解决了这个问题。
G
Garric

作为可能的代码之一

    echo off
    for /f "usebackq tokens=* delims= " %%x in (`chdir`) do set var=%var% %%x
    echo The current directory is: "%var:~1%"

P
Peter Mortensen

在 Windows 上,键入 cd 作为当前工作路径。

在 Linux 上,pwd 表示当前工作路径。


已经有10个答案相同的解决方案,但更多的解释