已知道文件的具体地址路径(D:\123\456\789\aa.txt)
怎样可以获取这文件的文件夹路径呢
已知道文件的具体地址路径(D:\123\456\789\aa.txt)
怎样可以获取这文件的文件夹路径呢
Private Sub Command1_Click()
FileName = "D:\123\456\789\aa.txt" c = Split(FileName, "\", -1, vbBinaryCompare) a = UBound(c) x = 0 Do Text1.Text = Text1.Text & c(x) & "\" x = x + 1 Loop Until x = a End Sub
1.如果知道这个文件的名字,比方为aa.txt,用filename这个变量来表示,另外再假设这个文件的具体路径用变量path来表示(D:\123\456\789\aa.txt),则获得此文件的文件夹路径的方法(即获取D:\123\456\789\)为(用folderPath表示文件夹的路径):
Dim path, filename As String Dim folderPaht As String path = "D:\123\456\789\aa.txt" filename = "aa.txt" folderPaht = Mid(path, 1, Len(path) - Len(filename)) MsgBox folderPaht
2.如果不知道这个文件的名字,则需要加一个步骤,就是先获取这个文件的名字,具体的程序实现如下所示,用folderPath表示文件夹的路径
Dim path, filename As String Dim filepath() As String Dim folderPaht As String path = "D:\123\456\789\aa.txt" filepath = Split(path, "\") filename = filepath(UBound(filepath)) folderPaht = Mid(path, 1, Len(path) - Len(filename)) MsgBox folderPaht