Created
March 7, 2012 02:50
-
-
Save wangye/1990576 to your computer and use it in GitHub Desktop.
VBScript Path Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' | |
' Author: wangye | |
' For more information please visit | |
' http://wangye.org/blog/archives/250/ | |
' | |
Function PathRemoveFileSpec1(strFileName) | |
' 将类Unix路径 / 替换为 \ | |
strFileName = Replace(strFileName, "/", "\") | |
Dim iPos | |
' 从路径末尾开始搜索\,这样从第一个字符到这个位置 | |
' 就是我们所需要的 | |
iPos = InStrRev(strFileName, "\") | |
' 使用Left保留需要的部分 | |
PathRemoveFileSpec1 = Left(strFileName, iPos) | |
End Function | |
Function PathRemoveFileSpec2(strFileName) | |
Dim RegEx | |
Set RegEx = New RegExp | |
RegEx.Pattern = "([^\\/]*)$" | |
' 替换掉正斜杠/或者反斜杠\后面的内容 | |
PathRemoveFileSpec2 = RegEx.Replace(strFileName, "") | |
Set RegEx = Nothing | |
End Function | |
Function PathRemoveFileSpec3(strFileName) | |
Dim fso,file | |
Set fso = WSH.CreateObject("Scripting.FileSystemObject") | |
Set file = fso.GetFile(strFileName) | |
PathRemoveFileSpec3 = fso.GetParentFolderName(file) | |
Set file = Nothing | |
Set fso = Nothing | |
End Function | |
Function PathRemoveFileSpec4(strFileName) | |
Dim fso,file | |
Set fso = WSH.CreateObject("Scripting.FileSystemObject") | |
If fso.FileExists(strFileName) Then | |
Set file = fso.GetFile(strFileName) | |
PathRemoveFileSpec4 = fso.GetParentFolderName(file) | |
Set file = Nothing | |
ElseIf fso.FolderExists(strFileName) Then | |
' 如果已经是文件夹则不需要处理直接返回 | |
PathRemoveFileSpec4 = strFileName | |
Else | |
' Oops 文件或文件夹不存在 | |
' 原样返回当然也可以调用 | |
' PathRemoveFileSpec2正则文本处理一下 | |
PathRemoveFileSpec4 = strFileName | |
End If | |
Set fso = Nothing | |
End Function | |
Function PathAddBackslash(strFileName) | |
Dim fso,file | |
PathAddBackslash = strFileName | |
Set fso = WSH.CreateObject("Scripting.FileSystemObject") | |
If fso.FolderExists(strFileName) Then | |
Dim last | |
' 文件夹存在 | |
' 截取最后一个字符 | |
last = Right(strFileName, 1) | |
If last<>"\" And last<>"/" Then | |
PathAddBackslash = strFileName & "\" | |
End If | |
End If | |
Set fso = Nothing | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment