将绝对path转换为batch file中的相对path

是否有可能将绝对path转换为batch file中的相对path? (与此相反)。 显然你需要两个input:转换的绝对path,以及一个你想要相对的绝对参考path。

例如:

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

Solutions Collecting From Web of "将绝对path转换为batch file中的相对path"

 @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% 

这里是另一种方法,如果你想从一个字符串的开始删除%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