vba从文件路径获取文件夹名称 [英] vba get folder name from file path
本文介绍了vba从文件路径获取文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请指南从路径中获取文件夹名称
我试过并获取文件名但无法获取文件夹名称
这是我的代码
Please Guide to grt the folder name from the path
I have tried and get the file name but not able to get the folder name
Here is my code
Sub GetselectedPath()
Dim fd As FileDialog
Dim i As Integer
Dim pth As String
Dim sPath As String
Dim sFile As String
Dim foldname As String
Dim fso As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Set fd = Application.FileDialog(3)
With fd
.AllowMultiSelect = True
.Title = "Select Folder"
.InitialFileName = "C:\" 'intial directory
If .Show = -1 Then
For i = 1 To .SelectedItems.Count
pth = .SelectedItems(1)
sFile = FileName(.SelectedItems(i), sPath)
'MsgBox sPath & "---" & sFile
FromPath = sPath '<< Change
ToPath = "C:\00temp\00 WinRar\" '<< Change
FileExt = sFile '<< Change
'You can use *.* for all files or *.doc for word files
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If fso.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
fso.Copyfile source:=FromPath & FileExt, Destination:=ToPath
Next
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End If
End With
Set fd = Nothing
End Sub
Public Function FileName(ByVal strPath As String, sPath) As String
sPath = Left(strPath, InStrRev(strPath, "\"))
FileName = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
这里我需要从我选择文件的地方获取文件夹名称。
Here i need to get the folder name from where I have selected the files.
推荐答案
从代码中,你有两个变量pth和sFile。
pth - 完整文件名。
sFile - 选择文件名。
使用下面的代码获取文件夹。
sFolder =替换(pth,sFile,)
From the code, you have two variables pth and sFile.
pth - Full file name.
sFile - file name selected.
use below code to get folder.
sFolder = Replace(pth ,sFile , "")
这篇关于vba从文件路径获取文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文