Resolve absolute path from relative path and/or file name

前端 未结 14 1586
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 09:38

Is there a way in a Windows batch script to return an absolute path from a value containing a filename and/or relative path?

Given:

         


        
相关标签:
14条回答
  • 2020-11-27 09:58

    I have not seen many solutions to this problem. Some solutions make use of directory traversal using CD and others make use of batch functions. My personal preference has been for batch functions and in particular, the MakeAbsolute function as provided by DosTips.

    The function has some real benefits, primarily that it does not change your current working directory and secondly that the paths being evaluated don't even have to exist. You can find some helpful tips on how to use the function here too.

    Here is an example script and its outputs:

    @echo off
    
    set scriptpath=%~dp0
    set siblingfile=sibling.bat
    set siblingfolder=sibling\
    set fnwsfolder=folder name with spaces\
    set descendantfolder=sibling\descendant\
    set ancestorfolder=..\..\
    set cousinfolder=..\uncle\cousin
    
    call:MakeAbsolute siblingfile      "%scriptpath%"
    call:MakeAbsolute siblingfolder    "%scriptpath%"
    call:MakeAbsolute fnwsfolder       "%scriptpath%"
    call:MakeAbsolute descendantfolder "%scriptpath%"
    call:MakeAbsolute ancestorfolder   "%scriptpath%"
    call:MakeAbsolute cousinfolder     "%scriptpath%"
    
    echo scriptpath:       %scriptpath%
    echo siblingfile:      %siblingfile%
    echo siblingfolder:    %siblingfolder%
    echo fnwsfolder:       %fnwsfolder%
    echo descendantfolder: %descendantfolder%
    echo ancestorfolder:   %ancestorfolder%
    echo cousinfolder:     %cousinfolder%
    GOTO:EOF
    
    ::----------------------------------------------------------------------------------
    :: Function declarations
    :: Handy to read http://www.dostips.com/DtTutoFunctions.php for how dos functions
    :: work.
    ::----------------------------------------------------------------------------------
    :MakeAbsolute file base -- makes a file name absolute considering a base path
    ::                      -- file [in,out] - variable with file name to be converted, or file name itself for result in stdout
    ::                      -- base [in,opt] - base path, leave blank for current directory
    :$created 20060101 :$changed 20080219 :$categories Path
    :$source http://www.dostips.com
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "src=%~1"
    if defined %1 set "src=!%~1!"
    set "bas=%~2"
    if not defined bas set "bas=%cd%"
    for /f "tokens=*" %%a in ("%bas%.\%src%") do set "src=%%~fa"
    ( ENDLOCAL & REM RETURN VALUES
        IF defined %1 (SET %~1=%src%) ELSE ECHO.%src%
    )
    EXIT /b
    

    And the output:

    C:\Users\dayneo\Documents>myscript
    scriptpath:       C:\Users\dayneo\Documents\
    siblingfile:      C:\Users\dayneo\Documents\sibling.bat
    siblingfolder:    C:\Users\dayneo\Documents\sibling\
    fnwsfolder:       C:\Users\dayneo\Documents\folder name with spaces\
    descendantfolder: C:\Users\dayneo\Documents\sibling\descendant\
    ancestorfolder:   C:\Users\
    cousinfolder:     C:\Users\dayneo\uncle\cousin
    

    I hope this helps... It sure helped me :) P.S. Thanks again to DosTips! You rock!

    0 讨论(0)
  • 2020-11-27 09:58

    PowerShell is pretty common these days so I use it often as a quick way to invoke C# since that has functions for pretty much everything:

    @echo off
    set pathToResolve=%~dp0\..\SomeFile.txt
    for /f "delims=" %%a in ('powershell -Command "[System.IO.Path]::GetFullPath( '%projectDirMc%' )"') do @set resolvedPath=%%a
    
    echo Resolved path: %resolvedPath%
    

    It's a bit slow, but the functionality gained is hard to beat unless without resorting to an actual scripting language.

    0 讨论(0)
  • 2020-11-27 09:59

    In batch files, as in standard C programs, argument 0 contains the path to the currently executing script. You can use %~dp0 to get only the path portion of the 0th argument (which is the current script) - this path is always a fully qualified path.

    You can also get the fully qualified path of your first argument by using %~f1, but this gives a path according to the current working directory, which is obviously not what you want.

    Personally, I often use the %~dp0%~1 idiom in my batch file, which interpret the first argument relative to the path of the executing batch. It does have a shortcoming though: it miserably fails if the first argument is fully-qualified.

    If you need to support both relative and absolute paths, you can make use of Frédéric Ménez's solution: temporarily change the current working directory.

    Here's an example that'll demonstrate each of these techniques:

    @echo off
    echo %%~dp0 is "%~dp0"
    echo %%0 is "%0"
    echo %%~dpnx0 is "%~dpnx0"
    echo %%~f1 is "%~f1"
    echo %%~dp0%%~1 is "%~dp0%~1"
    
    rem Temporarily change the current working directory, to retrieve a full path 
    rem   to the first parameter
    pushd .
    cd %~dp0
    echo batch-relative %%~f1 is "%~f1"
    popd
    

    If you save this as c:\temp\example.bat and the run it from c:\Users\Public as

    c:\Users\Public>\temp\example.bat ..\windows

    ...you'll observe the following output:

    %~dp0 is "C:\temp\"
    %0 is "\temp\example.bat"
    %~dpnx0 is "C:\temp\example.bat"
    %~f1 is "C:\Users\windows"
    %~dp0%~1 is "C:\temp\..\windows"
    batch-relative %~f1 is "C:\Windows"
    

    the documentation for the set of modifiers allowed on a batch argument can be found here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call

    0 讨论(0)
  • 2020-11-27 09:59

    Without having to have another batch file to pass arguments to (and use the argument operators), you can use FOR /F:

    FOR /F %%i IN ("..\relativePath") DO echo absolute path: %%~fi
    

    where the i in %%~fi is the variable defined at /F %%i. eg. if you changed that to /F %%a then the last part would be %%~fa.

    To do the same thing right at the command prompt (and not in a batch file) replace %% with %...

    0 讨论(0)
  • 2020-11-27 10:04

    This is to help fill in the gaps in Adrien Plisson's answer (which should be upvoted as soon as he edits it ;-):

    you can also get the fully qualified path of your first argument by using %~f1, but this gives a path according to the current path, which is obviously not what you want.

    unfortunately, i don't know how to mix the 2 together...

    One can handle %0 and %1 likewise:

    • %~dpnx0 for fully qualified drive+path+name+extension of the batchfile itself,
      %~f0 also suffices;
    • %~dpnx1 for fully qualified drive+path+name+extension of its first argument [if that's a filename at all],
      %~f1 also suffices;

    %~f1 will work independent of how you did specify your first argument: with relative paths or with absolute paths (if you don't specify the file's extension when naming %1, it will not be added, even if you use %~dpnx1 -- however.

    But how on earth would you name a file on a different drive anyway if you wouldn't give that full path info on the commandline in the first place?

    However, %~p0, %~n0, %~nx0 and %~x0 may come in handy, should you be interested in path (without driveletter), filename (without extension), full filename with extension or filename's extension only. But note, while %~p1 and %~n1 will work to find out the path or name of the first argument, %~nx1 and %~x1 will not add+show the extension, unless you used it on the commandline already.

    0 讨论(0)
  • 2020-11-27 10:05

    stijn's solution works with subfolders under C:\Program Files (86)\,

    @echo off
    set projectDirMc=test.txt
    
    for /f "delims=" %%a in ('powershell -Command "[System.IO.Path]::GetFullPath( '%projectDirMc%' )"') do @set resolvedPath=%%a
    
    echo full path:    %resolvedPath%
    
    0 讨论(0)
提交回复
热议问题