Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
How Do I do this? here is what I'm trying to do:

A SCREENSHOT to what I'm trying to Achive
This is the controls that I have created: Button1 "Browse..." to files, I'm using OpenFileDialog select the file(s) and add them into the CheckListBox1, TextBox1 contains path where the file(s) are copied to e.g. "D:\temp", Checkbox1, Checkbox2, Checkbox3 are folder that are created when checked under "D:\temp\folder1" if no CheckBox is checked no folder(s) are and no file(s) Button2 "Copy File(s)" when clicked folder(s) will be created and file(s) copied to the path of TextBox1 according to the Checkbox1, Checkbox2, Checkbox3 if they checked.

This is the code that I have:

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim browseFiles As New OpenFileDialog
    browseFiles.Title = "Select files to copy"
    browseFiles.Filter = "All Files (*.*)|*.*"
    'allow multiselection in the open file dialog
    browseFiles.Multiselect = True
    Dim result As DialogResult = browseFiles.ShowDialog()

    If Not result = Windows.Forms.DialogResult.Cancel Then
        'here its file name(s) with s to return array of file names
        Dim names() As String = browseFiles.FileNames
        For Each name As String In names
            Dim fileName As String = System.IO.Path.GetFileName(name)
            CheckedListBox1.Items.Add(fileName)
        Next
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim txt As String
    txt = TextBox1.Text
    If CheckBox1.CheckState = 1 Then
        My.Computer.FileSystem.CreateDirectory("" & txt & "\Folder1")
        MessageBox.Show("Folder1 created successfully, click OK")
    End If
    If CheckBox2.CheckState = 1 Then
        My.Computer.FileSystem.CreateDirectory("" & txt & "\Folder2")
        MessageBox.Show("Folder2 created successfully, click OK")
    End If

    If CheckBox3.CheckState = 1 Then
        My.Computer.FileSystem.CreateDirectory("" & txt & "\Folder3")
        MessageBox.Show("Folder3 created successfully, click OK")
    End If
    If CheckBox4.CheckState = 1 Then
        My.Computer.FileSystem.CreateDirectory("" & txt & "\Folder4")
        MessageBox.Show("Folder4 created successfully, click OK")
    End If
End Sub
Posted
Updated 23-Oct-14 23:14pm
v3
Comments
[no name] 18-Oct-14 19:11pm    
And the problem that I have with my code is.... ?
Member 11155542 20-Oct-14 8:11am    
I'll update my question
[no name] 20-Oct-14 8:53am    
Okay please do. Version 2 still does not have a description of any kind of a problem.
Dave Kreskowiak 18-Oct-14 19:47pm    
You first have to learn how to communicate with people. For example, we have no idea what your question is.
Sergey Alexandrovich Kryukov 19-Oct-14 0:53am    
Do you have a link to a book "Learn communicating with people in ten days"? :-)
—SA

For a start, you are using the wrong control if you want to get a list of all files in a directory based on a folder path. (Unless you want specifically select these files only).

You are using the OpenFileDialog when you would be better to use the OpenFolderDialog. I would only be using the OpenFileDialog if you want to choose which files you want to add manually, otherwise you could use recursive measures to do the work for you.

The open folder dialog is all you need to return the folder path that your user has selected.

You can use System.IO.DirectoryInfo to return directory info, and System.IO.FileInfo to return file info. To return a list of the files in that directory you can use the System.IO.Path as you have in your above example.

You don't need this because the path will never be null if it can be selected. :)
If Not String.IsNullOrEmpty(txtFolderPath.Text) Then

I rewrote it using the folderdialog instead. It is easy to change it back should you need too. This code is working perfectly fine and I have included a lot of functionality to allow you to play around with the file paths and file names which is why I used the dictionary for that.

The code is not commented, but if you have any questions, let me know. If you are happy with this, please mark it as a solution.

VB
#Region "Declationss"
     Dim FileDetails As New Dictionary(Of String, String)
    Dim CheckedItem As Integer = 0
    Dim CheckedSelected As String = ""

#End Region

#Region "Subroutines"
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim result As DialogResult = FolderBrowserDialog.ShowDialog()

        If Not result = Windows.Forms.DialogResult.Cancel Then

            GetListOfFiles(Currentlocation:=FolderBrowserDialog.SelectedPath, NewLocation:=FolderBrowserDialog.SelectedPath & "\" & "folder", Done:=False)

        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For Me.CheckedItem = 0 To CheckedListBox1.CheckedItems.Count - 1
            CheckedSelected = CheckedListBox1.CheckedItems(CheckedItem).ToString
            If FileDetails.ContainsKey(CheckedSelected) Then
                ' Write value of the key.
                Dim zLocation As String = Nothing
                zLocation = FileDetails.Item(CheckedSelected)
                Dim Comparison As KeyValuePair(Of String, String)
                Dim ComparisonValue As String = Nothing
                Dim eFileNameOnly As String = Nothing
                For Each Comparison In FileDetails
                    If Comparison.Key = CheckedSelected Then
                        ComparisonValue = Comparison.Value & Comparison.Key
                        eFileNameOnly = Comparison.Key
                        FileCopy(iLocation:=zLocation, iNewLocation:=zLocation & "folder\", eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)
                    End If
                Next
            End If
        Next
    End Sub

#End Region


#Region "Functions"
     Private Function GetListOfFiles(ByVal Currentlocation As String, ByVal NewLocation As String, ByRef Done As Boolean) As String

        If Directory.Exists(NewLocation) = False Then
            My.Computer.FileSystem.CreateDirectory(NewLocation)
            MessageBox.Show("folder created successfully, click OK")
        End If

        For Each eFilename As String In IO.Directory.GetFiles(Currentlocation, "*", IO.SearchOption.TopDirectoryOnly)
            Dim iFileName As String = System.IO.Path.GetFileName(eFilename)

            Dim NewDirNameCut As String = eFilename.Replace(iFileName, "")

            FileDetails.Add(iFileName, NewDirNameCut)

            CheckedListBox1.Items.Add(iFileName)

        Next

        Done = True
        Return Done

    End Function
    Private Function FileCopy(ByVal iLocation As String, ByVal iNewLocation As String, ByVal eFile As String, ByVal FileNameOnly As String, ByRef Done As Boolean) As Boolean

        If File.Exists(eFile) Then

            Dim OldFileToCopy = iLocation & FileNameOnly
            Dim NewFileToCopy = iNewLocation & FileNameOnly

            If System.IO.File.Exists(OldFileToCopy) = True Then
                System.IO.File.Copy(OldFileToCopy, NewFileToCopy, True)
            End If

            MessageBox.Show("Copied: " & iLocation & FileNameOnly & " to " & iNewLocation & FileNameOnly)
            Done = True
            Return Done
        Else
            Done = False
            Return Done
        End If

    End Function

#End Region
 
Share this answer
 
v2
Comments
Member 11155542 20-Oct-14 5:07am    
CodingK, Thank you very much for your Reply, I'm looking at your code and see how I can use it, I'll let you know if I have another question. Thanks
Member 11155542 20-Oct-14 8:46am    
Hi CodingK, I have updated my question, if you got time please have a look. Thank you
[no name] 20-Oct-14 9:58am    
If you want to add additional folders then in this subroutine:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Where this line resides:
If Comparison.Key = CheckedSelected Then
Add another if statement inside it to check the checked property state of each checkbox, and if the checked state is checked, then include a variable like:
Dim CheckBox1Path As String = "D:\Folder1"
You will also need to amend the code in button 2 to extend your directory structure based on those statements checking the checked state of each checkbox before sending the new paths to the function.

The functions I provided you work perfectly, so use them. The work that you need to do is in Button 2 as I stated.

As for changing the OpenFolderDialog to OpenFileDialog, you will need to copy the code in this function:
Private Function GetListOfFiles(ByVal Currentlocation As String, ByVal NewLocation As String, ByRef Done As Boolean) As String
And put it in Button 1, but this will require some tweaking.

Since the solution works, would you care to rate it or accept it as a solution.
Hi, here is a another solution for you which works perfectly. So you can also mark this as a solution too. If you have any questions, please post your questions under my solution and I will get back to you when I have some free time. :)

As you requested alterations for the checkboxes locations, I have provided two methods which are explained in the source code of Button2. Hope it helps you.

VB
#Region "MoveFiles Region"
 
#Region "Declations"
     Dim FileDetails As New Dictionary(Of String, String)
    Dim CheckedItem As Integer = 0
    Dim CheckedSelected As String = ""
    'The CBCheckedFolders will be used to add the two directories together if they are both selected.  
    Dim CBCheckedFolders As String = Nothing
    'The CtrlDict Dictionary will be used to collect the new folder paths from your CheckBox Text property, and an integer to represent its position and state.
    Dim CtrlDict As New Dictionary(Of Integer, String)
    'The Boolean variable(s) below will be true if a value in the CtrlDict = one of our checkstate keys added 
    'from our CheckBoxes Checked Changed Event. 
    Dim CheckA, CheckB, CheckC, CheckD As Boolean
    Dim FolderBrowserPath As String = Nothing
    Dim SetLabelPath As String = ""
    Dim SetLabelPathCut As String = Nothing

#End Region

#Region "Subroutines"
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LocationLabel.Visible = False
        LocationLabel.Text = "Location:"
    End Sub

    Private Sub CB1FolderA_CheckedChanged(sender As Object, e As EventArgs) Handles CB1FolderA.CheckedChanged
        If Me.CB1FolderA.CheckState = CheckState.Checked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Add(1, CB1FolderA.Text & "\")
            LocationLabel.Visible = True
            If Not SetLabelPath.Contains(CB1FolderA.Text) Then
                SetLabelPath = SetLabelPath + "\" + CB1FolderA.Text
                LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
                CBCheckedFolders = SetLabelPath
            End If
        ElseIf Me.CB1FolderA.CheckState = CheckState.Unchecked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Remove(1)
            LocationLabel.Visible = True
            If SetLabelPath.Contains(CB1FolderA.Text) Then
                SetLabelPathCut = SetLabelPath.Replace("\" & CB1FolderA.Text, "")
                SetLabelPath = SetLabelPathCut
                LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
                CBCheckedFolders = SetLabelPath
            End If

        Else
            MsgBox("Browse a folder first.")
        End If
    End Sub

    Private Sub CB2FolderB_CheckedChanged(sender As Object, e As EventArgs) Handles CB2FolderB.CheckedChanged
        If Me.CB2FolderB.CheckState = CheckState.Checked Then
            CtrlDict.Add(2, CB2FolderB.Text & "\")
            LocationLabel.Visible = True
            SetLabelPath = SetLabelPath + "\" + CB2FolderB.Text
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
            CBCheckedFolders = SetLabelPath
        ElseIf Me.CB2FolderB.CheckState = CheckState.Unchecked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Remove(2)
            LocationLabel.Visible = True
            If SetLabelPath.Contains(CB2FolderB.Text) Then
                SetLabelPathCut = SetLabelPath.Replace("\" & CB2FolderB.Text, "")
                SetLabelPath = SetLabelPathCut
                LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
                CBCheckedFolders = SetLabelPath
            End If
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
        Else
            MsgBox("Browse a folder first.")
        End If
    End Sub

    Private Sub CB3FolderC_CheckedChanged(sender As Object, e As EventArgs) Handles CB3FolderC.CheckedChanged
        If Me.CB3FolderC.CheckState = CheckState.Checked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Add(3, CB3FolderC.Text & "\")
            LocationLabel.Visible = True
            SetLabelPath = SetLabelPath + "\" + CB3FolderC.Text
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
            CBCheckedFolders = SetLabelPath
        ElseIf Me.CB3FolderC.CheckState = CheckState.Unchecked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Remove(3)
            LocationLabel.Visible = True
            If SetLabelPath.Contains(CB3FolderC.Text) Then
                SetLabelPathCut = SetLabelPath.Replace("\" & CB3FolderC.Text, "")
                SetLabelPath = SetLabelPathCut
                LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
                CBCheckedFolders = SetLabelPath
            End If
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
        Else
            MsgBox("Browse a folder first.")
        End If
    End Sub

    Private Sub CB4FolderD_CheckedChanged(sender As Object, e As EventArgs) Handles CB4FolderD.CheckedChanged
        If Me.CB4FolderD.CheckState = CheckState.Checked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Add(4, CB4FolderD.Text & "\")
            LocationLabel.Visible = True
            SetLabelPath = SetLabelPath + "\" + CB4FolderD.Text
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
            CBCheckedFolders = SetLabelPath
        ElseIf Me.CB4FolderD.CheckState = CheckState.Unchecked AndAlso FolderBrowserPath IsNot Nothing Then
            CtrlDict.Remove(4)
            LocationLabel.Visible = True
            If SetLabelPath.Contains(CB4FolderD.Text) Then
                SetLabelPathCut = SetLabelPath.Replace("\" & CB4FolderD.Text, "")
                SetLabelPath = SetLabelPathCut
                LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
                CBCheckedFolders = SetLabelPath
            End If
            LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
        Else
            MsgBox("Browse a folder first.")
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim result As DialogResult = FolderBrowserDialog.ShowDialog()

        If Not result = Windows.Forms.DialogResult.Cancel Then

            FolderBrowserPath = FolderBrowserDialog.SelectedPath


            GetListOfFiles(Currentlocation:=FolderBrowserDialog.SelectedPath, Done:=False)


        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For Me.CheckedItem = 0 To CheckedListBox1.CheckedItems.Count - 1
            CheckedSelected = CheckedListBox1.CheckedItems(CheckedItem).ToString
            If FileDetails.ContainsKey(CheckedSelected) Then
                ' Write value of the key.
                Dim zLocation As String = Nothing
                zLocation = FileDetails.Item(CheckedSelected)
                Dim Comparison As KeyValuePair(Of String, String)
                Dim ComparisonValue As String = Nothing
                Dim eFileNameOnly As String = Nothing
                For Each Comparison In FileDetails
                    If Comparison.Key = CheckedSelected Then
                        ComparisonValue = Comparison.Value & Comparison.Key
                        eFileNameOnly = Comparison.Key
                        Dim Checked As Boolean
                        If WhatsChecked(Done:=Checked) = True Then
                            CombineChecked(RTCombinePath:=CBCheckedFolders)


                            Dim LabelPathText As String = LocationLabel.Text
                            Dim LabelPathTextCut As String = LabelPathText.Remove(0, 10)
'#########################What You Need To Do#######################
'You need to add one additional Label to your form called: LocationLabel
'You need to rename your checkboxes on your form as they are named below:
'CB1FolderA, CB2FolderB, CB3FolderC, CB4FolderD
'Add a OpenFolderDiaLog with the name: OpenFolderDiaLog
'Replace your Button1, and Button2 code with mine. 
'If you get stuck, or have questions, feel free to ask me in the comments.

'########################How It Works###############################
'Now, this part is important as I've wrote this in a way 
'that you have two methods, working differently, and they 
'both do the same job. They both do the job differently, 
'One of the options is using a Dictionary called: CtrlDict
'It adds the value of a selected checkboxes text property 
'to a dictionary.value property. 

'We then use the dictionary.key property from the same 
'Dictionary (CtrlDict) in a function called: WhatsChecked
'This checkes the dictionary keys against each checked 
'checkbox. Thus enabling a Boolean value to True for each
'match found... 

'We then use the CombineChecked to those Boolean Values we 
'set in the: WhatsChecked function, and this returns the 
'enabled checkboxes text property for the checkboxes which
'have a CheckState = to Checked. This gives you a variable 
'path: CBCheckedFolders with the Checkboxes text property's
'added to the variable which is then used and passed on to 
'the function: FileCopy as you can see in the code from 
'button 2. 

'####################Additional Method###############################

'So what is the commented line below the non-commented 
'FileCopy function? That is another method which you can 
'also use to pass on to the FileCopy Function, and its a lot
'easier to work with than the dictionary method. 
'(I added the dictionary method to give you experience and
'to show you alternatives to using other functionality to
'broaden your ability to expand on the use of the code.) 

'Anyway, The non-commented out FileCopy method below uses a 
'LabelPathTextCut Variable which removed the text: Location
'from the variable named: LabelPathText whcih is = to the 
'LocationLabel.Text property, and this gives you an absolute
'path to the OpenFolderDialog and the Checked Checkboxes 
'text properties. And this is handled in the CheckedChanged
'EventArgs. The SetLabelPathCut and SetLabelPath variables 
'handle the Checkboxes (Checked) Text properties and 
'combines them. And lastly; the LocationLabel.Text property
'Is then equal to:
'LocationLabel.Text = "Location: " & FolderBrowserPath + SetLabelPath
'then we removed the (Location:) part in the: LabelPathTextCut 
'variable where you can pass it on to the FileCopy function. 

'Job done, and have fun with it, as it is all fully working, and 
'you can choose which method you like best and remove the alternative.
'But If you break it, I won't be fixing it. :)

'Now you can remove whatever functionality you don't like or want.  
                            '###############################END###################################

                            FileCopy(iLocation:=zLocation, iNewLocation:=LabelPathTextCut & "\", eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)

                            '       FileCopy(iLocation:=zLocation, iNewLocation:=zLocation & CBCheckedFolders, eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)


                        End If
                        'Recently added in updated code. 
                    End If
                Next
            End If
        Next
    End Sub

#End Region

#Region "Functions"
    Private Function CombineChecked(ByRef RTCombinePath As String) As String 'Recently added in updated code.
        'This will reset the RTCombinePath each time this function is run. 
        RTCombinePath = Nothing

        Dim NonNull As KeyValuePair(Of Integer, String)

        For Each NonNull In CtrlDict

            'Now we will write a new directory structure based on the order of the checkboxes. So from CB1FolderA to CB4FolderD
            'Returning each one that was checked.

            With NonNull
                If CheckA = True Then
                    'The If Not statement ensures no duplicate additions are added.
                    If Not CBCheckedFolders = Nothing AndAlso Not CBCheckedFolders.Contains(.Value) Then
                        CBCheckedFolders = CBCheckedFolders + (.Value)
                    ElseIf CBCheckedFolders = Nothing Then
                        CBCheckedFolders = (.Value)
                    End If
                End If
                If CheckB = True Then
                    If Not CBCheckedFolders = Nothing AndAlso Not CBCheckedFolders.Contains(.Value) Then
                        CBCheckedFolders = CBCheckedFolders + (.Value)
                    ElseIf CBCheckedFolders = Nothing Then
                        CBCheckedFolders = (.Value)
                    End If
                End If
                If CheckC = True Then
                    If Not CBCheckedFolders = Nothing AndAlso Not CBCheckedFolders.Contains(.Value) Then
                        CBCheckedFolders = CBCheckedFolders + (.Value)
                    ElseIf CBCheckedFolders = Nothing Then
                        CBCheckedFolders = (.Key)
                    End If
                End If
                If CheckD = True Then
                    If Not CBCheckedFolders = Nothing AndAlso Not CBCheckedFolders.Contains(.Value) Then
                        CBCheckedFolders = CBCheckedFolders + (.Value)
                    ElseIf CBCheckedFolders = Nothing Then
                        CBCheckedFolders = (.Value)
                    End If
                End If
            End With

        Next
        RTCombinePath = CBCheckedFolders
        Return RTCombinePath
    End Function
    Private Function WhatsChecked(ByRef Done As Boolean) As Boolean 'Recently added in updated code.
        
        Dim NonNull As KeyValuePair(Of Integer, String)

        For Each NonNull In CtrlDict

            'Add Individual ifs to check each key from the collection against each checkbox.
            'Example, if NonNul.Key = a Numbered Integer, We know to enable that boolean: 
            'CheckA to = True. In this case, 1 , 2 , 3 , and 4 represent the first, second
            'third and forth checkbox etc.... 
            If NonNull.Key = 1 = False Then
                Done = False
            End If
            If NonNull.Key = 2 = False Then
                Done = False
            End If
            If NonNull.Key = 3 = False Then
                Done = False
            End If
            If NonNull.Key = 4 = False Then
                Done = False
            End If

            'Now we will write a new directory structure based on the order of the checkboxes. So from CB1FolderA to CB4FolderD

            If NonNull.Key = 1 = True Then
                CheckA = True
                Done = True
            End If
            If NonNull.Key = 2 = True Then
                CheckB = True
                Done = True
            End If
            If NonNull.Key = 3 = True Then
                CheckC = True
                Done = True
            End If
            If NonNull.Key = 4 = True Then
                CheckD = True
                Done = True
            End If
        Next
        Return Done

    End Function
    Private Function GetListOfFiles(ByVal Currentlocation As String, ByRef Done As Boolean) As String

        For Each eFilename As String In IO.Directory.GetFiles(Currentlocation, "*", IO.SearchOption.TopDirectoryOnly)
            Dim iFileName As String = System.IO.Path.GetFileName(eFilename)


            Dim NewDirNameCut As String = eFilename.Replace(iFileName, "")

            FileDetails.Add(iFileName, NewDirNameCut)

            CheckedListBox1.Items.Add(iFileName)

        Next

        Done = True
        Return Done

    End Function
    Private Function FileCopy(ByVal iLocation As String, ByVal iNewLocation As String, ByVal eFile As String, ByVal FileNameOnly As String, ByRef Done As Boolean) As Boolean

        If Directory.Exists(iNewLocation) = False Then
            My.Computer.FileSystem.CreateDirectory(iNewLocation)
            MessageBox.Show("folder created successfully, click OK")
        End If

        If File.Exists(eFile) Then

            Dim OldFileToCopy = iLocation & FileNameOnly
            Dim NewFileToCopy = iNewLocation & FileNameOnly

            If System.IO.File.Exists(OldFileToCopy) = True Then
                System.IO.File.Copy(OldFileToCopy, NewFileToCopy, True)
            End If

            MessageBox.Show("File Copied From: " & Environment.NewLine & iLocation & FileNameOnly & Environment.NewLine & "To:" & Environment.NewLine & iNewLocation & FileNameOnly)
            Done = True
            Return Done
        Else
            Done = False
            Return Done
        End If

    End Function

#End Region

#End Region
 
Share this answer
 
v5
Comments
[no name] 25-Oct-14 9:24am    
One other thing, to set the paths of the checkboxes: CB1FolderA, CB2FolderB, CB3FolderC, CB4FolderD -- You need to set their text property to something like: CB1 New Folder, CB2 New Folder, CB3 New Folder, CB4 New Folder. ;)

Let me know how it goes for you.
Member 11155542 25-Oct-14 17:59pm    
Hi CodingK, Thank you very much for taking time and writing all of this code, much appreciated. currently when folder are created it's created as subfolders to each other, is there a way that each folder can be created as separate folders? e.g.
c:\CB1 New Folder
c:\CB2 New Folder
c:\CB3 New Folder
c:\CB4 New Folder
instead of when all checkboxes are selected this is created c:\CB1 New Folder\CB2 New Folder\CB3 New Folder\CB4 New Folder, Thank you.
[no name] 25-Oct-14 18:28pm    
Firstly, every solution you have been provided, you have wanted an alteration too, I am not a free coding service and I do run a coding business outside of Code Project should you wish to hire me directly? I feel that I have contributed more than my share of contributions to your question; as well as answered each and every question you put to me, and perhaps you might want to try manipulate my logic yourself or else hire me through our website.

What I suggest is that you remove the current logic in the checkboxes checked event that adds the text property of each checkbox to the dictionary, and in the CopyFiles function, pass your own path to that function, and check each checked checkbox check status and create your own directory structure to be passed to that function instead.

Please also mark this as a solution to your question since it has been answered successfully, and if you make changes to the code, in the comments, I will of course advise you of any mistakes you make, but you too need to make and show some effort regarding alterations.
Member 11155542 26-Oct-14 4:32am    
Thank you CodingK, you've Helped me enough :-), I'm going to try your advice. Once again you are the only person who could help on this post, so you are a true coder :-) and I Thank you very much.
[no name] 26-Oct-14 8:13am    
You're welcome.

You could try make a copy of button2 and use this function only if you want to join the paths.
FileCopy(iLocation:=zLocation, iNewLocation:=zLocation & CBCheckedFolders, eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)
Alternative to not joining the strings in the CheckBox Checked Event to stop the paths adding on to each other; maybe change: SetLabelPath = SetLabelPath + "\" + CB1FolderA.Text
to:
SetLabelPath = "\" + CB1FolderA.Text

But you will need to do that for each one (change the name of CB1FolderA to the events control you are editing) and play with it and debug it to ensure the code is doing as you like. Thanks for marking as an alternate solution.
Hi CokingK, I do still need your assistance if possible, I've managed as litle as possible to copy the file to Temp folder, I have a ScreenShot here and here is the code:


VB
#Region "Declaration"
    Dim FileDetails As New Dictionary(Of String, String)
    Dim CheckedItem As Integer = 0
    Dim CheckedSelected As String = ""
#End Region

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
#Region "Subroutines"
     Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
      Dim result As DialogResult = FolderBrowserDialog1.ShowDialog()
        If Not result = Windows.Forms.DialogResult.Cancel Then
            GetListOfFiles(Currentlocation:=FolderBrowserDialog1.SelectedPath, NewLocation:=FolderBrowserDialog1.SelectedPath & "\" & "", Done:=False)
        End If

    End Sub

    Private Sub btnCopyFiles_Click(sender As Object, e As EventArgs) Handles btnCopyFiles.Click

        Dim NewLocation As String = TextBox1.Text
        Dim fdAr As String = CheckBox1.Text

        For Me.CheckedItem = 0 To CheckedListBox1.CheckedItems.Count - 1
            CheckedSelected = CheckedListBox1.CheckedItems(CheckedItem).ToString
            If FileDetails.ContainsKey(CheckedSelected) Then
                ' Write value of the key.
                Dim zLocation As String = Nothing
                zLocation = FileDetails.Item(CheckedSelected)
                Dim Comparison As KeyValuePair(Of String, String)
                Dim ComparisonValue As String = Nothing
                Dim eFileNameOnly As String = Nothing
                For Each Comparison In FileDetails
                    If Comparison.Key = CheckedSelected Then

                        ComparisonValue = Comparison.Value & Comparison.Key
                        eFileNameOnly = Comparison.Key
                        FileCopy(iLocation:=zLocation, iNewLocation:=zLocation & NewLocation & "", eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)


                    End If
                Next
            End If
        Next

        
    End Sub


    Private Function GetListOfFiles(ByVal Currentlocation As String, ByVal NewLocation As String, ByRef Done As Boolean) As String
        NewLocation = TextBox1.Text
        If Directory.Exists(NewLocation) = False Then
            
            If CheckBox1.CheckState = 1 Then
                My.Computer.FileSystem.CreateDirectory("" & NewLocation & "\ar")
                MessageBox.Show("Arabic folder created successfully, click OK")
            End If

        End If
        For Each eFilename As String In IO.Directory.GetFiles(Currentlocation, "*", IO.SearchOption.TopDirectoryOnly)
            Dim iFileName As String = System.IO.Path.GetFileName(eFilename)

            Dim NewDirNameCut As String = eFilename.Replace(iFileName, "")

            FileDetails.Add(iFileName, NewDirNameCut)

            CheckedListBox1.Items.Add(iFileName)
        Next

        Done = True
        Return Done
    End Function

    Private Function FileCopy(ByVal iLocation As String, ByVal iNewLocation As String, ByVal eFile As String, ByVal FileNameOnly As String, ByRef Done As Boolean) As Boolean

        If File.Exists(eFile) Then
            iNewLocation = TextBox1.Text
            Dim OldFileToCopy = iLocation & FileNameOnly
            Dim NewFileToCopy = iNewLocation & FileNameOnly

            If System.IO.File.Exists(OldFileToCopy) = True Then
                System.IO.File.Copy(OldFileToCopy, NewFileToCopy, True)
            End If

            MessageBox.Show("Copied: " & iLocation & FileNameOnly & " to " & iNewLocation & FileNameOnly)
            Done = True
            Return Done
        Else
            Done = False
            Return Done
        End If

    End Function

#End Region


ScreenShot
 
Share this answer
 
v2
Comments
[no name] 24-Oct-14 8:51am    
Please do not undo answers which you accepted as correct and which are clearly correct. Especially do not undo answers to demand more help on working code. That is also plain rude...

I myself have spent a lot of time assisting you with this, and as a result of un-marking my answer as a solution, you removed -25 Authority points from me...

I told you i would help you with edits if you replied in a comment to the answered solution!
Member 11155542 24-Oct-14 8:56am    
my apologies, didn't mean anything bad by it, just thought as I've changed the code... I'll accept it back, after all your code is the right one. I'm new to all of this.
[no name] 24-Oct-14 9:06am    
You will accept the answer back regardless, since that is what you asked for in your first question. And I provided you with the functionality you needed.

What you are asking for now is more code and alterations to extend onto my solution which I am not obliged to do.

Had you been polite and asked for help altering the current code without unmarking the answer. I would have done so. What you did was just plain rude and ungrateful.

Take care.
Member 11155542 24-Oct-14 9:08am    
Not a Problem at all CodingK, Thank you for all the help, best regards and take care, keep helping the noobs, you are good at it.
[no name] 24-Oct-14 9:16am    
I understand you may be new to a site like this and how the CP questions work. But when someone gives you code which does not work, then you can undo their solution after you accept it and tried their failed code.

Note: if a solution works, but does not provide all of the functionality you need, but yet fixes your problem, that too is still a solution you should accept out of respect of the person who wrote it to you. It is then up to you to make your own required alterations and not the person providing you with free code!

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