Get parent directory name for a particular file using DOS Batch scripting

后端 未结 9 1278
無奈伤痛
無奈伤痛 2020-12-08 21:33

I need to find the name of the parent directory for a file in DOS

for ex.

Suppose this is the directory

C:\\test\\pack\\a.txt
相关标签:
9条回答
  • 2020-12-08 22:09

    I found this combination of approaches from the answers of djangofan and paranoid to be both, simple and perfectly sufficient, when looking up my script's parent directory:

    set FULL_PATH=%~dp0
    set FULL_PATH=%FULL_PATH:~1,-1%
    for %%i in ("%FULL_PATH%") do set "PARENT_FOLDER=%%~ni"
    echo %PARENT_FOLDER%
    

    Since you want to work on user input instead, you have to do some minimal additional work, to handle legal variations like C:\foo\bar\a.txt vs. C:\foo\bar\a.txt or c:/foo/bar/a.txt. This might then work for you:

    @setlocal
    @echo off
    
    call:GET_PARENT_FOLDER C:\foo\bar\a.txt
    echo %PARENT_FOLDER%
    call:GET_PARENT_FOLDER C:\foo\bar\\a.txt
    echo %PARENT_FOLDER%
    call:GET_PARENT_FOLDER c:/foo/bar/a.txt
    echo %PARENT_FOLDER%
    
    pause
    goto:EOF
    
    :GET_PARENT_FOLDER
    :: Strip the filename, so we get something like this: 'C:\foor\bar\'
    set "_FULL_PATH=%~dp1"
    
    :: Strips all dangling '\' and '/' in a loop, so the last folder name becomes accessible
    :_STRIP
    if not "%_FULL_PATH:~-1%"=="\" if not "%_FULL_PATH:~-1%"=="/" goto:_STRIP_END
    set "_FULL_PATH=%_FULL_PATH:~1,-1%"
    goto:_STRIP
    :_STRIP_END
    
    :: We need the context of a for-loop for the special path operators to be available
    for %%i in ("%_FULL_PATH%") do set "PARENT_FOLDER=%%~ni"
    
    goto:EOF
    
    0 讨论(0)
  • 2020-12-08 22:10

    First answer above does not work if parent directory name contains a space. The following works:

    @echo off
    setlocal
    
    set ParentDir=%~p1
    set ParentDir=%ParentDir: =:%
    set ParentDir=%ParentDir:\= %
    call :getparentdir %ParentDir%
    set ParentDir=%ParentDir::= %
    
    echo ParentDir is %ParentDir%
    goto :EOF
    
    :getparentdir
    if "%~1" EQU "" goto :EOF
    Set ParentDir=%~1
    shift
    goto :getparentdir
    

    Calling the above with parameter of "C:\Temp\Parent Dir With Space\myfile.txt" gives following:

    >GetParentDir "C:\Temp\Parent Dir With Space\myfile.txt"
    ParentDir is Parent Dir With Space
    

    The above works by replacing spaces with colons (these should not exist in Windows paths), then replacing directory delimiters with spaces to so individual directories are passed to getparentdir as separate arguments. Function getparentdir loops until it finds its last argument. Finally any colons in result are replaced by spaces.

    0 讨论(0)
  • 2020-12-08 22:13

    see this question

    @echo OFF
    set mydir="%~p1"
    SET mydir=%mydir:\=;%
    
    for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
    goto :EOF
    
    :LAST_FOLDER
    if "%1"=="" (
        @echo %LAST%
        goto :EOF
    )
    
    set LAST=%1
    SHIFT
    
    goto :LAST_FOLDER
    0 讨论(0)
  • 2020-12-08 22:15

    It can be very simple to get the parent folder of the batch file:

    @echo off
    for %%a in ("%~dp0\.") do set "parent=%%~nxa"
    echo %parent%
    

    And for a parent of a file path as per the question:

    @echo off
    for %%a in ("c:\test\pack\a.txt") do for %%b in ("%%~dpa\.") do set "parent=%%~nxb"
    echo %parent%
    
    0 讨论(0)
  • 2020-12-08 22:25

    Here's a way that does not use CALL and I believe is faster.Based on jeb's splitting function (might fail if the directory name contains !) :

    @echo off
    
    set "mydir=%~p1"
    SET mydir=%mydir:~0,-1%
    
    setlocal EnableDelayedExpansion
    set LF=^
    
    
    rem ** Two empty lines are required
    
    for %%L in ("!LF!") DO (
        set "dir_name=!mydir:\=%%L!"
    )
    for /f "delims=" %%P in (""!dir_name!"") do set "dn=%%~P"
    echo %dn%
    
    exit /b 0 
    
    0 讨论(0)
  • 2020-12-08 22:25

    Here is another solution:

    SET LAST=%CD%
    SET PARENTNAME=NONE
    cd /D C:\test\pack
    FOR %%I in (%CD%) do SET PARENTNAME=%%~nI
    cd /D %LAST%
    ECHO %PARENTNAME%
    

    %%~nI: '~n' extracts name from path stored in %%I variable
    cd: '/D' parameter added to switch between disks also

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