【问题标题】:Convert absolute path to relative path in batch file将批处理文件中的绝对路径转换为相对路径
【发布时间】:2012-04-30 12:44:10
【问题描述】:

是否可以在批处理文件中将绝对路径转换为相对路径? (与this 相反)。显然,您需要两个输入:要转换的绝对路径,以及您希望将其相对化的绝对参考路径。

例如:

Path to convert: c:\documents\mynicefiles\afile.txt
Reference path:  c:\documents
Result:          mynicefiles\afile.txt

【问题讨论】:

    标签: windows batch-file relative-path absolute-path


    【解决方案1】:
    @echo off
    setlocal EnableDelayedExpansion
    set Path_to_convert=c:\documents\mynicefiles\afile.txt
    set Reference_path=c:\documents
    set Result=!Path_to_convert:*%Reference_path%\=!
    echo Result: %Result%
    

    【讨论】:

    • 谢谢,这似乎工作得很好!我以前从未见过这种语法,请您提供解释或链接吗?
    • 我也赞成,因为它有效,尽管我不想说,我不知道为什么。
    • 输入:SET /? 并在“环境变量替换已增强如下:%PATH:str1=str2%”和“延迟的环境变量扩展允许您这样做”下查找解释。 ..”
    • 当路径没有硬编码到脚本中而是取自 %CD% 或 FOR 变量时,似乎不起作用。 :(
    【解决方案2】:

    如果您想从字符串的开头删除 %cd%,这是另一种有效的方法。它很慢,但如果情况允许,您可以减少循环次数。

    call :removeCommonAtStart outvar C:\Users\Public\Documents\ASUSAccess
    
    :removeCommonAtStart
    :: Description: loops through two strings and sets new variable representing unique data
    :: Required parameters:
    :: name - name of the variable to be returned
    :: test - string to have common data removed from start
    :: Optional parameters:
    :: remove - string if not defined then use %cd% as string.
    :: Required functions:
    :: removelet
    set name=%~1
    set test=%~2
    set remove=%~3
    if not defined remove set remove=%cd%
    set endmatch=
    FOR /L %%l IN (0,1,150) DO if not defined notequal call :removelet
    goto :eof
    
    :removelet
    :: Description: called by removeCommonAtStart to remove one letter from the start of two string variables
    :: Required preset variables:
    :: test
    :: remove
    :: name
    set test=%test:~1%
    set %name%=%test:~1%
    set remove=%remove:~1%
    if "%test:~0,1%" neq "%remove:~0,1%" set notequal=on&exit /b
    goto :eof
    

    【讨论】:

      【解决方案3】:

      以下是“dostips”解决方案。
      在所使用的操作级别上相当模糊,但非常有效!

      *标题中提供的示例应该有助于理解此宏的用法。

      ::=//////////////////////////////////////////////////////////////////////////
      ::= Makes a file name relative to a base path.
      ::= *param-1:  file [in,out]  - variable with file name to be converted, or file name itself for result in stdout
      ::= *param-2:  base [in,opt*] - base path | *leave blank for current directory
      ::= *return:   The resolved relative path is stored into *param-1
      ::=
      ::= SAMPLE*HOW-TO-USE:
      ::=  a) Current path       =D:\titi\toto\
      ::=  b) DATAPATH_absolute  =D:\titi\tata\tutu\log.txt
      ::=  c) --relativize the data-path--
      ::=
      ::=    c1) INIT the data:
      ::=        $>  set DATAPATH_relative=%DATAPATH_absolute%
      ::=
      ::=    c2) CALL the sub-function:
      ::=        $>  call  :MakeRelative  DATAPATH_relative
      ::=
      ::=    c3) (( After the call, result is set in %DATAPATH_relative% ))
      ::=
      ::=  d) echo DATAPATH_relative =%DATAPATH_relative%
      ::=                     /--->   "..\tata\tutu\log.txt"
      ::=
      ::= $source https://www.dostips.com
      ::= **web:  https://www.dostips.com/?t=Function.MakeRelative
      ::= **web:  https://www.dostips.com/DtCodeCmdLib.php#Function.MakeRelative
      ::=--------------------------------------------------------------------------
      
      :MakeRelative
          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 ("%src%") do set src=%%~fa
          for /f "tokens=*" %%a in ("%bas%") do set bas=%%~fa
          set mat=&rem variable to store matching part of the name
          set upp=&rem variable to reference a parent
      
          for /f "tokens=*" %%a in ('echo.%bas:\=^&echo.%') do (
              set sub=!sub!%%a\
              call set tmp=%%src:!sub!=%%
              if "!tmp!" NEQ "!src!" (set mat=!sub!) ELSE (set upp=!upp!..\)
          )
      
          set src=%upp%!src:%mat%=!
          ( ENDLOCAL & REM RETURN VALUES
              IF defined %1 (SET %~1=%src%) ELSE ECHO.%src%
          )
          EXIT /b
      

      DosTips.com, :MakeRelative

      【讨论】:

        猜你喜欢
        • 2016-12-11
        • 2017-08-01
        • 2011-11-07
        • 2011-05-02
        相关资源
        最近更新 更多