How to get windows batch's parent folder

后端 未结 2 1867
你的背包
你的背包 2020-12-01 20:59

I\'m writing a batch file, I need to get the parent folder of this bat file. Is it possibile? NB I mean the parent folder of the batch file not of the current directory of t

相关标签:
2条回答
  • 2020-12-01 21:33

    The parent folder of a Batch is in the Variable %~dp0 located. Example:

    @echo off&setlocal
    for %%i in ("%~dp0.") do set "folder=%%~fi"
    echo %folder%
    
    0 讨论(0)
  • 2020-12-01 21:38

    Endoro's answer doesn't work for me either but this does. This is what I use myself to do this.

    V3

    One of these should do the right thing

    for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~nxJ
    echo %ParentFolderName%
    
    for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~dpnxJ
    echo %ParentFolderName%
    

    V2:

    for %%I in ("%~dp0\.") do set ParentFolderName=%%~nxI
    echo %ParentFolderName%
    

    V1:

    This one gets the parent directory of the current working directory

    for %%I in (..) do set ParentFolderName=%%~nI%%~xI
    echo %ParentFolderName%
    

    Reference: For | Microsoft Docs

    0 讨论(0)
提交回复
热议问题