html 相对路径通配符,从相对路径和/或文件名解析绝对路径

我没有看到很多解决此问题的方法。一些解决方案使用CD进行目录遍历,而其他解决方案则使用批处理功能。我个人偏爱批处理功能,尤其是DosTips提供的MakeAbsolute功能。

该函数具有一些实际好处,首先是它不会更改您当前的工作目录,其次,甚至不必存在要评估的路径。您也可以在此处找到一些有关如何使用该功能的有用提示。

这是一个示例脚本及其输出:

@echo off

set scriptpath=%~dp0

set siblingfile=sibling.bat

set siblingfolder=sibling\

set fnwsfolder=folder name with spaces\

set descendantfolder=sibling\descendant\

set ancestorfolder=..\..\

set cousinfolder=..\uncle\cousin

call:MakeAbsolute siblingfile "%scriptpath%"

call:MakeAbsolute siblingfolder "%scriptpath%"

call:MakeAbsolute fnwsfolder "%scriptpath%"

call:MakeAbsolute descendantfolder "%scriptpath%"

call:MakeAbsolute ancestorfolder "%scriptpath%"

call:MakeAbsolute cousinfolder "%scriptpath%"

echo scriptpath: %scriptpath%

echo siblingfile: %siblingfile%

echo siblingfolder: %siblingfolder%

echo fnwsfolder: %fnwsfolder%

echo descendantfolder: %descendantfolder%

echo ancestorfolder: %ancestorfolder%

echo cousinfolder: %cousinfolder%

GOTO:EOF

::----------------------------------------------------------------------------------

:: Function declarations

:: Handy to read http://www.dostips.com/DtTutoFunctions.php for how dos functions

:: work.

::----------------------------------------------------------------------------------

:MakeAbsolute file base -- makes a file name absolute considering a base path

:: -- file [in,out] - variable with file name to be converted, or file name itself for result in stdout

:: -- base [in,opt] - base path, leave blank for current directory

:$created 20060101 :$changed 20080219 :$categories Path

:$source http://www.dostips.com

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 ("%bas%.\%src%") do set "src=%%~fa"

( ENDLOCAL & REM RETURN VALUES

IF defined %1 (SET %~1=%src%) ELSE ECHO.%src%

)

EXIT /b

并输出:

C:\Users\dayneo\Documents>myscript

scriptpath: C:\Users\dayneo\Documents\

siblingfile: C:\Users\dayneo\Documents\sibling.bat

siblingfolder: C:\Users\dayneo\Documents\sibling\

fnwsfolder: C:\Users\dayneo\Documents\folder name with spaces\

descendantfolder: C:\Users\dayneo\Documents\sibling\descendant\

ancestorfolder: C:\Users\

cousinfolder: C:\Users\dayneo\uncle\cousin

希望对您有所帮助...它肯定对我有所帮助:) PS再次感谢DosTips!你摇滚!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值