簡體   English   中英

Windows shell 命令獲取當前目錄的完整路徑?

[英]Windows shell command to get the full path to the current directory?

是否有 Windows 命令行命令可用於獲取當前工作目錄的完整路徑?

另外,如何將此路徑存儲在批處理文件中使用的變量中?

使用cd不帶參數,如果你直接使用的外殼,或%cd%如果你想在一個批處理文件中使用它(它像一個環境變量)。

您可以按如下方式設置批處理/環境變量:

SET var=%cd%
ECHO %var%

來自 Windows 7 x64 cmd.exe 的示例屏幕截圖。

在此處輸入圖片說明

更新:如果你執行SET var = %cd%而不是SET var=%cd% ,下面是會發生什么。 感謝傑布。

在此處輸入圖片說明

從批處理文件中捕獲當前目錄

引用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. 部分。

在 Unix 上?

密碼

這一直對我有用:

SET CurrentDir="%~dp0"

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

ECHO %CurrentDir%

Pause

對於 Windows,我們可以使用

cd

對於 Linux

pwd

命令在那里。

在 Windows 上:

CHDIR顯示當前目錄的名稱或更改當前目錄。

在 Linux 中:

PWD顯示當前目錄的名稱。

對於 Windows, cd本身會顯示當前工作目錄。

對於 UNIX 和類似工作的系統, pwd將執行相同的任務。 您還可以在某些 shell 下使用$PWD shell 變量。 我不確定 Windows 是否支持通過 shell 變量獲取當前工作目錄。

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可以從任何地方獲得。

根據 chdir 帖子的評論中的后續問題(將數據存儲在變量中),我敢打賭他想存儲當前路徑以在更改目錄后恢復它。

原始用戶應該查看“pushd”,它更改目錄並將當前目錄推送到可以用“popd”恢復的堆棧上。 在任何現代 Windows cmd shell 上,這是制作批處理文件時要走的路。

如果您確實需要獲取當前路徑,那么現代 cmd shell 也有一個 %CD% 變量,您可以輕松地將其塞入另一個變量中以供參考。

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

但是,當然,gmaran23 的答案要容易得多。

在 Windows 命令提示符下, chdircd將在控制台中打印當前工作目錄的完整路徑。

如果我們想復制路徑,那么我們可以使用: cd | clip cd | clip

作為可能的代碼之一

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

在 Windows 上,輸入cd作為當前工作路徑。

在 Linux 上, pwd表示當前工作路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM