简体   繁体   English

如何从windows批处理文件中获取当前目录相关路径

[英]How to get current directory related path from windows batch file

I want to know how can we get current directory of batch file and parent directories of batch file.我想知道如何获取批处理文件的当前目录和批处理文件的父目录。 It will be easy to explain by example.通过示例将很容易解释。 Lets assume my batch file location D:\\Ashish\\Gupta\\abc\\xyz.bat How to get below locations from xyz.bat让我们假设我的批处理文件位置 D:\\Ashish\\Gupta\\abc\\xyz.bat 如何从 xyz.bat 获取以下位置

  1. D:\\Ashish\\Gupta\\abc -> D:\\Ashish\\Gupta\\abc ->
  2. D:\\Ashish\\Gupta -> D:\\Ashish\\Gupta ->
  3. D:\\Ashish -> D:\\Ashish ->
  4. D: -> D:->

I tried to search on internet and get couple of answers but still confused.我试图在互联网上搜索并得到几个答案,但仍然感到困惑。

you can use PUSHD .. with a for /L loop and If conditional statements until you reach the root directory, like so:您可以将PUSHD ..for /L loopIf条件语句一起使用,直到到达根目录,如下所示:

@Echo off & CD "%~dp0"
 Set "Path[0]=%~dp0"
  Setlocal EnableDelayedExpansion
  PUSHD ..
  For /L %%i in (1 1 20)Do (
    If /I not "!CD!" == "!LastDir!" (
    Set "Path[%%i]=!CD!"
    Set "Root{i}=%%i"
    Set "LastDir=!Path[%%i]!"
    PUSHD ..
   )
  )
 Set Path[
 Set Root{
 For /L %%i in (0 1 %Root{i}%) Do POPD
 ECHO %CD%
 Endlocal

The above creates a zero indexed array of the parent folders right back to the root drive.以上创建了一个零索引的父文件夹数组,回到根驱动器。 the 20 value used in the for /L loop iterations is a fairly arbitrary number - I can't see many situations where you'll be nesting that many subfolders, however if for some reason you have deeply nested subfolders, you may need to increase the number of iterations. for /L 循环迭代中使用的 20 值是一个相当任意的数字 - 我看不到很多情况下你会嵌套那么多子文件夹,但是如果由于某种原因你嵌套了很深的子文件夹,你可能需要增加迭代次数。

That is very simple to achieve:这很容易实现:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FilePath=%~dp0"
:ParsePath
for %%I in ("%FilePath%.") do (
    echo %%~fI
    set "FilePath=%%~dpI"
    if not "%%~dI\" == "%%~dpI" if not "%%~dpI" == "%%~fI" goto ParsePath
    if not "%%~dpI" == "%FilePath%" echo %%~dI\
)
endlocal
pause

The batch file defines first the execution environment with the first two command lines.批处理文件首先使用前两个命令行定义执行环境。

Then it assigns the full path of the batch file always ending with a backslash to the environment variable FilePath .然后,它将始终以反斜杠结尾的批处理文件的完整路径分配给环境变量FilePath

The command for is used to get the string left to last backslash in environment variable FilePath (drive and path) and the string right to last backslash in environment variable FilePath (current folder name). for命令for获取环境变量FilePath (驱动器和路径)中最后一个反斜杠的字符串和环境变量FilePath (当前文件夹名称)中最后一个反斜杠的字符串。

The . . is appended to string assigned to FilePath to force command for to access the file system to get the current file path without a backslash at end.附加到指定字符串FilePath ,以武力命令for在结束访问文件系统来获得当前的文件路径,而不反斜杠。 The dot means current directory.点表示当前目录。 So on first iteration of the for loop D:\\Ashish\\Gupta\\abc\\ is extended to D:\\Ashish\\Gupta\\abc\\.因此,在for循环的第一次迭代中for D:\\Ashish\\Gupta\\abc\\被扩展为D:\\Ashish\\Gupta\\abc\\. and for that reason %%~fI expands to D:\\Ashish\\Gupta\\abc while %%~dpI expands to D:\\Ashish\\Gupta\\ for the next loop run.因此%%~fI扩展到D:\\Ashish\\Gupta\\abc%%~dpI扩展到D:\\Ashish\\Gupta\\以进行下一次循环运行。

%%~fI and %%~dpI would expand on first loop run both to D:\\Ashish\\Gupta\\abc\\ if just "%FilePath%" without . %%~fI%%~dpI将在第一圈扩大运行既D:\\Ashish\\Gupta\\abc\\如果只是"%FilePath%"没有. would be used because of in this case the file/folder string defines already an absolute path not containing anymore any relative part and so for would not access the file system to determine full file/folder name and full path of file/folder.因为在这种情况下,文件/文件夹字符串定义已经是绝对路径不包含任何了相对一部分,因此将被用来for不访问文件系统,以确定完整的文件/文件夹名称和文件/文件夹的完整路径。

It is possible to use command for to split up a file/folder string of a file or folder which does not exist at all.可以使用命令for拆分根本不存在的文件或文件夹的文件/文件夹字符串。 There is if exist to check if a file/folder exists. if exist检查文件/文件夹是否存在。 The command for is not designed for file/folder existence checks. for命令不是为文件/文件夹存在性检查设计的。 There are lots of uses cases on which for is used to split up a full qualified file name into file path and file name + extension without file already existing yet.在许多用例中, for用于将完全限定的文件名拆分为文件路径和文件名 + 扩展名,而文件尚未存在。

One directory name after the other is removed from FilePath until there is left only either drive letter + colon + backslash like C:\\ or \\\\ComputerName\\ in case of batch file is stored on a network resource accessed using UNC path.FilePath删除一个接一个的目录名称,直到只剩下驱动器号 + 冒号 + 反斜杠,如C:\\\\\\ComputerName\\ ,如果批处理文件存储在使用 UNC 路径访问的网络资源上。

The condition if not "%%~dI\\" == "%%~dpI" is not true on root directory of the drive is the remaining file path.条件if not "%%~dI\\" == "%%~dpI"在驱动器的根目录上不为真,就是剩余的文件路径。

The condition if not "%%~dpI" == "%%~fI" is not true on \\\\ComputerName\\ is the remaining file path. \\\\ComputerName\\上的条件if not "%%~dpI" == "%%~fI" is not true 是剩余的文件路径。

So the top level is reached if one of these two conditions is not true anymore and for that reason there is no more goto ParsePath executed to parse the remaining file path once more.因此,如果这两个条件之一不再为真,则达到顶层,因此goto ParsePath执行goto ParsePath来再次解析剩余的文件路径。 There is instead output the root directory path of the drive if the batch file was not called using a UNC path and the batch file is not stored in root directory of a drive.如果批处理文件不是使用 UNC 路径调用的,并且批处理文件未存储在驱动器的根目录中,则会输出驱动器的根目录路径。

The batch file outputs on running D:\\Ashish\\Gupta\\abc\\ParsePath.bat :运行D:\\Ashish\\Gupta\\abc\\ParsePath.bat的批处理文件输出:

D:\Ashish\Gupta\abc
D:\Ashish\Gupta
D:\Ashish
D:\

The batch file outputs on running D:\\ParsePath.bat :运行D:\\ParsePath.bat的批处理文件输出:

D:\

The batch file outputs on running \\\\GuptaMachine\\D$\\Ashish\\Gupta\\abc\\ParsePath.bat :运行\\\\GuptaMachine\\D$\\Ashish\\Gupta\\abc\\ParsePath.bat的批处理文件输出:

\\GuptaMachine\D$\Ashish\Gupta\abc
\\GuptaMachine\D$\Ashish\Gupta
\\GuptaMachine\D$\Ashish\
\\GuptaMachine\D$
\\GuptaMachine\

See also the output of following command lines in batch file:另请参阅批处理文件中以下命令行的输出:

for %%I in ("%~dp0.") do (
    echo Batch file folder name: %%~nxI
    echo Batch file folder path: %%~fI
)

I recommend to read also the Microsoft documentation about Naming Files, Paths, and Namespaces as it is also possible to use .. or ..\\.. with %~dp0 to get the folder path of one of the parent folders on being sure there are parent folders as the batch file is never stored in root folder of a drive.我还建议阅读有关命名文件、路径和命名空间的 Microsoft 文档因为也可以使用....\\..%~dp0来获取父文件夹之一的文件夹路径,以确保存在是父文件夹,因为批处理文件永远不会存储在驱动器的根文件夹中。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • call /? ... explains how to reference batch file arguments like argument 0 which is the batch file itself. ... 解释了如何引用批处理文件参数,如参数 0,它是批处理文件本身。
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取当前目录执行的文件是使用批量的窗口? - How to get the current directory the file executed is in using batch of windows? 如何从 Windows 批处理脚本中的文件路径获取父目录名称? - How to get parent directory name from path of a file in a Windows batch script? 如何将批处理文件的目录添加到 Windows PATH? - How to add the directory of a batch file to Windows PATH? 从Windows批处理文件将当前目录传递给可执行文件 - Pass current directory to exectuable from windows batch file 如何测试路径是Windows批处理文件中的文件还是目录? - How to test if a path is a file or directory in Windows batch file? Windows Batch-将目录和文件名与完整文件路径字符串分开 - Windows Batch - separate directory and filename from full file path string 如何在Windows中的Python2.7中获取当前目录的完整路径? - How to get full path of current directory in Python2.7 in Windows? 从子文件夹调用批处理文件时,如何获取当前目录名称? - how to get current directory name while the batch file is called from sub folder? 如何在Windows的bash脚本的变量中存储当前目录路径? - How to store current directory path in variable from bash script for windows? 如何在Windows批处理文件中使用FOR获取文本文件中的路径变量? - How to get a path variable in a text file by using FOR in a Windows batch file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM