Solved

Help with exporting folder

Posted on 2014-04-28
17
261 Views
Last Modified: 2014-04-30
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
Comment
Question by:vcharles
  • 11
  • 6
17 Comments
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40028753
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
 

Author Comment

by:vcharles
ID: 40029759
Hi,

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

Victor
0
 

Author Comment

by:vcharles
ID: 40029778
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 20

Expert Comment

by:ElrondCT
ID: 40029999
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
 

Author Comment

by:vcharles
ID: 40031122
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
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40031173
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
 

Author Comment

by:vcharles
ID: 40031216
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
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40031233
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
 

Author Comment

by:vcharles
ID: 40031260
Thanks.
0
 

Author Comment

by:vcharles
ID: 40032951
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
 

Author Comment

by:vcharles
ID: 40033008
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
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 40033178
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
 

Author Comment

by:vcharles
ID: 40033320
Hi,

I will try your code and get back to you.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 40033332
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
 

Author Comment

by:vcharles
ID: 40033537
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
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40033606
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
 

Author Closing Comment

by:vcharles
ID: 40033826
Thank You.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question