vbs从文件路径截取文件名

上一篇 / 下一篇  2015-06-28 14:27:25 / 个人分类:VBScript

如文件路径为:d:\test\upload\upload.xlsx,想要获得文件名upload.xlsx或获得不包含扩展名的文件名:
51Testing软件测试网;c$h_v)L
*使用min和instrrev函数:
mid(str,instrrev(str,"\")+1)   '返回upload.xlsx
mid(str,instrrev(str,"\")+1,instrrev(str,".")-instrrev(str,"\")-1)           '返回upload

:d5@}%Fu |pu0mid函数:返回字符串指定起始位置指定长度的字符
instrrev函数:返回指定字符在元素字符串中的位置(反序查找,但位置从开始算)
 
*使用正则表达式
Function RegExpTest(patrn, strng)51Testing软件测试网4dvz5{3K
  Dim regEx, Match, Matches      ' Create variable.
!_'MH"{G0  Set regEx = New RegExp         ' Create a regular expression.51Testing软件测试网-hI F|(p-q8q
  regEx.Pattern = patrn         ' Set pattern.
*u,^{-T4g&O~Qa0  regEx.IgnoreCase = True         ' Set case insensitivity.51Testing软件测试网 hMV5el4W
  regEx.Global = True         ' Set global applicability.
)jaz0`$| ]0  Set Matches = regEx.Execute(strng)   ' Execute search.
Qv'nE];Y*wG/eB ~I0  For Each Match in Matches      ' Iterate Matches collection.51Testing软件测试网!~:?lLMW,f6q
  51Testing软件测试网*g0l)~+FA?
    RetStr =  Match.Value51Testing软件测试网+D0[\#]8X-`;n
  Next
4n&X/j;Y2Vh"bmx+~0  RegExpTest = RetStr51Testing软件测试网-XC.Vf+U!n
End Function51Testing软件测试网5R+?3i2l3oY?$O

0Idp*U'tx] _-f&WX0filpath="d:\test\upload\upload.xlsx"
str1= RegExpTest("[^\\]+$",filpath)   '返回uplad.xlsx
str2=RegExpTest("^[^\.]+",str1)       '返回upload

TAG:

 

评分:0

我来说两句

Open Toolbar