将绝对路径转换为批处理文件中的相对路径

Dav*_*ook 7 windows batch-file relative-path absolute-path

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

例如:

Path to convert: c:\documents\mynicefiles\afile.txt
Reference path:  c:\documents
Result:          mynicefiles\afile.txt
Run Code Online (Sandbox Code Playgroud)

Aac*_*ini 5

@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%
Run Code Online (Sandbox Code Playgroud)

  • 当路径没有硬编码到脚本中但是从%CD%或FOR变量中获取时似乎不起作用.:( (2认同)