Solved

Help with exporting folder

Posted on 2014-04-28
17
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:vcharles
Comment Utility
Thanks.
0
 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

I will try your code and get back to you.

Thanks,

Victor
0
 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now