Convert absolute path to relative path in batch file
Asked Answered
L

3

7

Is it possible to convert an absolute path to a relative path in a batch file? (the opposite of this). Obviously you would need two inputs: the absolute path to convert, and an absolute reference path that you want it to be relativised to.

eg:

Path to convert: c:\documents\mynicefiles\afile.txt
Reference path:  c:\documents
Result:          mynicefiles\afile.txt
Lase answered 19/4, 2012 at 2:2 Comment(0)
S
7
@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%
Shrovetide answered 19/4, 2012 at 3:54 Comment(5)
Thanks, that seems to work perfectly! I've never seen that syntax before, could you please provide an explanation or link?Lase
I am also upvoting because it works, even though I hate to say, I do not know why.Mephitic
Type: SET /? and look for the explanation under "Environment variable substitution has been enhanced as follows: %PATH:str1=str2%" and also under: "Delayed environment variable expansion allows you to..."Shrovetide
Doesn't seem to work when the paths aren't hardcoded into the script but taken from %CD% or FOR variables. :(Athallia
@Athallia Combining set ROOTDIR=%cd% and set LOCALPATH=!FULLPATH:*%ROOTDIR%=! worked for me (on Windows 10 Pro, 22H2).Airs
S
0

Here is another method that works if your want to remove the %cd% from the start of a string. It is slow but you could lower the number of loops if you situation allowed it.

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
Smoothbore answered 16/10, 2013 at 16:39 Comment(0)
M
0

Below is a 'dostips' solution.
Quite obscure at the level of operations used, but very efficient !

*Example provided in the header should help to understand usage of this macro.

::=//////////////////////////////////////////////////////////////////////////
::= 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

Mull answered 13/11, 2019 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.