标题: [原创] 批处理实现绝对路径转换相对路径 [打印本页]
作者: canyuexiaolang 时间: 2012-5-11 20:40 标题: 批处理实现绝对路径转换相对路径
- @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
复制代码
本来就不是什么技术性的。发给大家学习玩玩吧
作者: canyuexiaolang 时间: 2012-5-11 20:42
本来打算autorun.inf和desktop.ini用的。输入一个路径。比如i:\system 它会列出i:\所有的路径并转换成相对于i:\system的路径
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |