[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
vcharles
Asked:
vcharles
  • 11
  • 6
1 Solution
 
ElrondCTCommented:
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.
0
 
vcharlesAuthor Commented:
Hi,

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

Victor
0
 
vcharlesAuthor Commented:
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
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
ElrondCTCommented:
See your other question, "Help with over writing xml file," at http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28421332.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.
0
 
vcharlesAuthor Commented:
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
0
 
ElrondCTCommented:
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.
0
 
vcharlesAuthor Commented:
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.
0
 
ElrondCTCommented:
You select the output folder using the FolderBrowserDialog control, as we discussed in http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28420538.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.
0
 
vcharlesAuthor Commented:
Thanks.
0
 
vcharlesAuthor Commented:
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
0
 
vcharlesAuthor Commented:
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)
0
 
ElrondCTCommented:
Your check for the file existing needs to be inside the loop that looks at all the files in the original folder. You can skip any reference to the login form, because you're doing all the files, not just one
        FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        Dim di As New System.IO.DirectoryInfo(Application.StartupPath & "\Data\MasterFiles")
        Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")
        For Each oldFile As System.IO.FileInfo In FileInfoList
            Dim NewFilename As String = FolderBrowserDialog1.SelectedPath & "\" & oldFile.Name() & ".xml"
            If My.Computer.FileSystem.FileExists(NewFilename) Then
                dlgResult = MessageBox.Show("File " & NewFilename & " already exists. Overwrite?", "File exists", _
                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                If dlgResult = Windows.Forms.DialogResult.No Then
                    Continue For
                End If
            End If
            System.IO.File.Copy(oldFile.FullName, NewFilename, True)
            MsgBox("File " & NewFilename & " successfully exported")
        Next

Open in new window

I'm assuming that LinkFinal is part of the file name, not a folder name. So if a user's login name is Charles, the current full path name would be something like "C:\AppFolder\Data\Masterfiles\LinkFinalCharles.xml". (Replace "AppFolder" with whatever you get from Application.StartupPath.) If it should be "C:\AppFolder\Data\Masterfiles\LinkFinal\Charles.xml" (note the \ before Charles), then you'll have to adjust the code.

To change the direction of copying, just switch around the folder references:
        FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        Dim di As New System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)    ' Switch folder name
        Dim FileInfoList As System.IO.FileInfo() = di.GetFiles("LinkFinal*.xml")
        For Each oldFile As System.IO.FileInfo In FileInfoList
            Dim NewFilename As String = Application.StartupPath & "\Data\MasterFiles\" & oldFile.Name() & ".xml" ' Switch folder name
            If My.Computer.FileSystem.FileExists(NewFilename) Then
                dlgResult = MessageBox.Show("File " & NewFilename & " already exists. Overwrite?", "File exists", _
                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                If dlgResult = Windows.Forms.DialogResult.No Then
                    Continue For
                End If
            End If
            System.IO.File.Copy(oldFile.FullName, NewFilename, True)
            MsgBox("File " & NewFilename & " successfully exported")
        Next

Open in new window

0
 
vcharlesAuthor Commented:
Hi,

I will try your code and get back to you.

Thanks,

Victor
0
 
vcharlesAuthor Commented:
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
0
 
vcharlesAuthor Commented:
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.
0
 
ElrondCTCommented:
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.
0
 
vcharlesAuthor Commented:
Thank You.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now