Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to get a path in a selected folder from folderbrowserdialog?
Example:
1) User select D:\Content\Data
2) Inside folder "Data" there's contain some files and folder
3) I check all the files contains in the directory and want to copy it to new destination. But failed
4) How to get the file path from the selected folder?

eg:
"D:\Content\Data\ABC.ai" and get "ABC.ai" only
"D:\Content\Data\Cfg.ini" and get "Cfg.ini" only
"D:\Content\Data\Debug\App.exe" and get "Debug\App.exe" only
"D:\Content\Data\Debug\DB.mdb" and get "Debug\DB.mdb" only
VB
Assume Dim filename as String = 'Data from above'


Because the destination path is given as "\\user-pc\files".So I want to use
VB
Assume Dim strDestinationFolderPath as String = 'Path from above'


So the copied code will be as below
VB
My.Computer.FileSystem.CopyDirectory(fileName, strDestinationFolderPath & "\" filename & , True)
Posted

1 solution

I just got the thing:

VB
Public Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim f As New DirectoryInfo(targetDirectory)
        Dim dirs() As DirectoryInfo = f.GetDirectories()
        For Each d As DirectoryInfo In dirs
            Try
                My.Computer.FileSystem.CreateDirectory(strDestinationFolderPath & "\" & d.Name)
                txtevents.Text &= "Folder " & d.Name & " created" & Environment.NewLine
            Catch ex As Exception
                txtevents.Text &= ">> Error: Folder " & strDestinationFolderPath & "\" & d.Name & " failed to created" & Environment.NewLine
            End Try
        Next

        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        Dim fileName As String
        For Each fileName In fileEntries
            Try
                Dim strNewFile As String = fileName.Replace(strSourceFolderPath & "\", "")
                My.Computer.FileSystem.CopyFile(fileName, strDestinationFolderPath & strNewFile, True)
                txtevents.Text &= "File " & fileName & " copied" & Environment.NewLine
            Catch ex As Exception
                txtevents.Text &= ">> Error: File " & fileName & " failed to copied" & Environment.NewLine
            End Try
        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory
End Sub
 
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