用什么方法可以判断指定的路径是否合法?例如路径是C:\BackUp,却错写成C;/BackUp,这时候有什么函数可以返回一个真值或假值来判断此路径是否合法??请指教!!!
dir()
行不行?
自己写一个函数,只需要解析一个简单的字符串
条件是:
1 必须以盘符加冒号再加\开头
2 路径中不允许存在以下字符
: * ? | < > " /
3 不允许出现连续的\\符号
如楼上所述,只有自己写函数校验啦
Public Function checkPath(strPath As String) As Integer
On Error GoTo errPath
Dim intFlag As Integer
Dim strMess As String
Dim FileNumber As Integer
FileNumber = FreeFile
intFlag = -1
If InStr(strPath, ".csv") <= 1 Then
checkPath = 1
Else
If InStr(strPath, ":") > 1 Then
CreateNewDirectory strPath
strMess = Dir(strPath)
If strMess <> "" Then
intFlag = 2
End If
Open strPath For Output As #FileNumber
Close #FileNumber
Kill strPath
checkPath = 0
intFlag = 0
End If
End If
errPath:
If intFlag <> 0 Then
If intFlag = 2 Then
checkPath = 2
Else
checkPath = 1
End If
End If
End Function
Dim iFlag As Integer
Dim iFile As Integer
If TxtFilePath = "" Then
MsgBox Message("E0040", 0, 0, 0, "")
Exit Sub
End If
If Dir(TxtFilePath.text) <> "" Then
iFile = MsgBox(Message("M0001", 0, 0, 0, ""), vbYesNo, "エラー")
If iFile = 7 Then
Exit Sub
End If
End If
iFlag = checkPath(TxtFilePath.text)
If iFlag = 1 Then
MsgBox Message("E0041", 0, 0, 0, "")
Exit Sub
ElseIf iFlag = 2 Then
MsgBox Message("E0042", 0, 0, 0, "")
Exit Sub
End If
这是我写的原码,调试过,你看看吧,希望对你有所帮助!
if dir("路径")="" then
magbox "错误路径"
end if
dir("路径")=""
的前提是需要生成一个这样的路径才对的啊。
代码最少的是:mkdir("路径"),如果不出错就是OK的,但这样会真的生成这个目录。
这个方法是在某些情况下不可取。