Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with exporting folder

Hi,

I'm using the code below to export an xml file, how do I modify it to export a folder which contains multiple xml files?

FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\LinkFiles"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        If dlgResult = DialogResult.OK Then
            System.IO.File.Copy(Application.StartupPath & "\LinkFiles\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml", _
                FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml")
        End If

Thanks,

Victor
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

You can't do it as a single command. You'll have to create a list of all the files you want in the folder, then copy each one.
        Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\LinkFiles")
        Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")
        For Each oldFile As System.IO.FileInfo In FileInfoList
            System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name())
        Next

Open in new window


Putting

Imports System.IO

at the top of the form code, of course, will save you having to retype System.IO every time.
Avatar of Victor  Charles

ASKER

Hi,

Thanks for the code. How do I check if the file exist before executing the code the export it?

Victor
Hi,

How do I modify the code to also import all files containing LinkFinal to my applications' LinkFiles sub folder?

If you prefer, I can post as a new question.

Thanks,

Victor
See your other question, "Help with over writing xml file," at https://www.experts-exchange.com/questions/28421332/Help-with-over-writing-xml-file.html for how to check if the destination file already exists.

All files in a single folder, or all files anywhere on your system? For all files in a single folder, just switch the source and destination folders. For all files anywhere on your system, you'll have to work recursively through the entire directory structure; see http://support.microsoft.com/kb/306666 for more details on how to do that.
Hi,

I tried the code to export the files but received error message:

Access to the path '\LinkFinal.xml' is denied.

On line:

 System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name())

The  folder is in the \Data\LinkFiles\ sub folder of the application.




 Private Sub Button28_Click(sender As System.Object, e As System.EventArgs) Handles Button28.Click
        Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\Data\LinkFiles")
        Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")
        For Each oldFile As System.IO.FileInfo In FileInfoList
            System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name())
        Next
    End Sub
That suggests that FolderBrowserDialog1.SelectedPath is blank. In other words, you didn't select an output folder, or the name of the FolderBrowserDialog control that you're actually using doesn't match the one in this statement.

You should probably put a check in the procedure to make sure that FolderBrowserDialog1.SelectedPath isn't blank.
Hi,

My Data\LinkFiles sub folder contains multiple LinkFinalXXX.xml files.

Below is my code, but I don't see the part that allows me to export the files from my "My Data\LinkFiles sub folder " to any folder I select.

  Private Sub Button32_Click(sender As System.Object, e As System.EventArgs) Handles Button32.Click
        Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\Data\LinkFiles")
        Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")
        For Each oldFile As System.IO.FileInfo In FileInfoList
            System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name())
        Next
    End Sub

Thanks,

V.
You select the output folder using the FolderBrowserDialog control, as we discussed in https://www.experts-exchange.com/questions/28420538/Help-with-choosing-path-to-save-file.html. Then the Copy statement copies files from the old location to the new. But if you never select a folder using the FBD control, there's no place to go.
Thanks.
Hi,

The code below works but how do I check if all the files from the source folder already exist in the target folder instead of just checking for LinkFinal & Trim(LoginForm1.username.Text) & ".xml"?

FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
              Dim NewFilename As String = FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml"

        If dlgResult = DialogResult.OK Then
       If My.Computer.FileSystem.FileExists(NewFilename) Then

                dlgResult = MessageBox.Show("File " & NewFilename & " already exist. Overwrite?", "File exists", _
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                If dlgResult = Windows.Forms.DialogResult.No Then
                    Return
                End If
                Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\Data\MasterFiles")
                Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("*.xml")
                For Each oldFile As System.IO.FileInfo In FileInfoList
                    System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name(), True)
                Next
                MsgBox("File succesfully exported")
            Else
         Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\Data\MasterFiles")
                Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("*.xml")
                For Each oldFile As System.IO.FileInfo In FileInfoList
                    System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name(), True)
                Next
                MsgBox("File succesfully exported")
            End If
        End If



Thanks,

Victor
Regarding the Import function, all the files are in a single folder, can you please send me an example on how to switch the source and destination folders. I tried the code below bu the syntax is incorrect.

 If My.Computer.FileSystem.FileExists(NewFilename) Then

                dlgResult = MessageBox.Show("File " & NewFilename & " already exist. Overwrite?", "File exists", _
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                If dlgResult = Windows.Forms.DialogResult.No Then
                    Return
                End If
                Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\oldFile.Name()")
                Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("*.xml")
                For Each oldFile As System.IO.FileInfo In FileInfoList
                    System.IO.File.Copy(oldFile.FullName, FolderBrowserDialog1.SelectedPath & \Data\MasterFilesTrue)
ASKER CERTIFIED SOLUTION
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I will try your code and get back to you.

Thanks,

Victor
Hi again,

Just to clarify I'm trying to Export/Import all files in the folder selected instead of LinkFinal & Trim(LoginForm1.username.Text) & ".xml".

I need to replace Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")

With

Dim FileInfoList As System.IO.FileInfo() = di.GetFiles(*.xml")


Victor
Ok, because filenames will vary, best approach maybe to warn users they will overwrite exit files and not include the code to check for exiting files.
Regarding your next-to-last message: Yes, if you want to get all the .xml files in the folder, you'd use "*.xml" rather than "LinkFinal*.xml". You know your own situation best, but it's often not necessary to warn people about overwriting an existing file if there's no reason that they would need the old file.
Thank You.