Click here to Skip to main content
15,881,669 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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

VB
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.
Posted

1 solution

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 , "")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900