首页 文章

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

提问于
浏览
140

是否有Windows命令行命令,我可以用它来获取当前工作目录的完整路径?

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

14 回答

  • 6

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

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

  • 1

    在Unix上?

    PWD

  • 23

    基于后续问题(将数据存储在变量中)到chdir帖子的注释中我打赌他想要在更改目录后存储当前路径以恢复它 .

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

    如果你真的需要 grab 当前路径,那么现代cmd shell也有一个%CD%变量,你可以很容易地把它放在另一个变量中以供参考 .

  • 10

    On Windows:

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

    In Linux:

    PWD 显示当前目录的名称 .

  • 2

    如果直接使用shell,则使用 cd ,不使用参数;如果要在批处理文件中使用_310601,则使用_3106001(它的行为类似于环境变量) .

  • 4

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

    echo %cd% | clip
    

    Explanation:

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

    现在 copyclip 可以从任何地方获得 .

  • 221

    这一直对我有用:

    SET CurrentDir="%~dp0"
    
    ECHO The current file path this bat file is executing in is the following:
    
    ECHO %CurrentDir%
    
    Pause
    
  • 6

    有两种简单的方法,它们都涉及“os”模块 . 你可以使用:

    os.system("cd")
    

    要么

    print(os.getcwd())
    
  • 1

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

    SET var=%cd%
    ECHO %var%
    

    从Windows 7 x64 cmd.exe截取屏幕截图 .

    enter image description here

    Update: 如果您执行 SET var = %cd% 而不是 SET var=%cd% ,则会发生以下情况 . 感谢jeb .

    enter image description here

    Capturing the current directory from a batch file

  • -2

    对于Windows,我们可以使用

    cd

    对于Linux

    pwd

    命令就在那里 .

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

    但是,当然,gmaran23的答案要容易得多 .

  • 40

    在Windows上,键入 cd 作为工作电流路径 .

    在Linux上, pwd 为当前工作路径 .

  • 12

    引用Windows帮助 set 命令( set /? ):

    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. 部分 .

  • -2

    对于Windows, cd 本身将显示当前的工作目录 .

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

相关问题