- @echo off & setlocal enabledelayedexpansion
- set /p "abspath=U盘绝对路径:"
- if not defined abspath goto :eof
- if not exist "!abspath!" goto :eof
- if not "!abspath:~-1!"=="\" set "abspath=!abspath!\"
- call :pathlen
- for /r "%abspath:~0,2%" %%i in (.) do (
- setlocal
- set "_abspath=%%i" & set "_abspath=!_abspath:~0,-1!"
- call :getrelpath "!_abspath!"
- for /f "tokens=1* delims=\" %%q in ("!abspath!") do set "rel=!rel:~3!%%r"
- if /i "!_abspath:~0,%pathlen%!"=="!abspath!" set rel=!_abspath:~%pathlen%!
- echo !_abspath! ---^> !rel!
- endlocal
- )
- pause>nul
- goto :eof
-
- :getrelpath
- for /f "tokens=1* delims=\" %%i in ("%~1") do (
- set rel=..\!rel!
- call :getrelpath "%%j"
- )
- goto :eof
-
- :pathlen
- for /l %%i in (0 1 100) do if "!abspath:~%%i,1!"=="" set "pathlen=%%i" & goto :eof
- goto :eof
复制代码
本来就不是什么技术性的。发给大家学习玩玩吧 |