Resolve absolute path from relative path and/or file name

前端 未结 14 1758
被撕碎了的回忆
被撕碎了的回忆 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 10:09

    Small improvement to BrainSlugs83's excellent solution. Generalized to allow naming the output environment variable in the call.

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Example input value.
    set RelativePath=doc\build
    
    rem Resolve path.
    call :ResolvePath AbsolutePath %RelativePath%
    
    rem Output result.
    echo %AbsolutePath%
    
    rem End.
    exit /b
    
    rem === Functions ===
    
    rem Resolve path to absolute.
    rem Param 1: Name of output variable.
    rem Param 2: Path to resolve.
    rem Return: Resolved absolute path.
    :ResolvePath
        set %1=%~dpfn2
        exit /b
    

    If run from C:\project output is:

    C:\project\doc\build
    

提交回复
热议问题