Prev: elearning info not working
Next: Inner class problem.
From: K on 11 Jun 2010 04:14 Hi all, I am using Visual Basic 2008. I am working on code with which I can copy specified extention files from one folder to other. I need two codes, one which can copy files of specified extention only from top main folder and two which can copy files of specified extention from top main folder as well as from all the subfolder which exist in that top main folder. In these both codes I also need to have progress bar code to show user the progress. I am struggling on this and so far i came up with code (see below) which only copies files from top main folder and i am getting error on line "ProgressBar1.Value = (n / Fldrfl.Count) * 100". I'll be very greatful if any friend can help me on this. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim FldrNm As String Dim Fso As Object Dim Fldr As Object Dim Fldrfl As Object Dim n As Long FldrNm = TextBox1.Text Fso = CreateObject("Scripting.FileSystemObject") Fldr = Fso.GetFolder(FldrNm) For Each Fldrfl In Fldr.Files If Microsoft.VisualBasic.Right(Fldrfl.Name, Microsoft.VisualBasic.Len(Fldrfl.Name) - Microsoft.VisualBasic.InStrRev(Fldrfl.Name, ".")) = TextBox2.Text Then Fldrfl.Copy(TextBox3.Text & "\" & Fldrfl.Name) n = n + 1 ProgressBar1.Value = (n / Fldrfl.Count) * 100 Application.DoEvents() End If Next Fldrfl = Nothing Fldr = Nothing Fso = Nothing MsgBox("Files have been copied successful!", MsgBoxStyle.Information, "Done!") End If End Sub
From: Andrew Morton on 11 Jun 2010 04:33 K wrote: > Hi all, I am using Visual Basic 2008. I am working on code with which > I can copy specified extention files from one folder to other. I need > two codes, one which can copy files of specified extention only from > top main folder and two which can copy files of specified extention > from top main folder as well as from all the subfolder which exist in > that top main folder. In these both codes I also need to have > progress bar code to show user the progress. I am struggling on this > and so far i came up with code (see below) which only copies files > from top main folder and i am getting error on line > "ProgressBar1.Value = (n / Fldrfl.Count) * 100". I'll be very > greatful if any friend can help me on this. Go on, give us a chance - what's the error message? However... > Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button3.Click > Dim FldrNm As String > Dim Fso As Object > Dim Fldr As Object > Dim Fldrfl As Object > Dim n As Long Why Long rather than Integer? > FldrNm = TextBox1.Text > Fso = CreateObject("Scripting.FileSystemObject") I'm pretty sure you'd be better off using System.IO for this sort of thing. > Fldr = Fso.GetFolder(FldrNm) > For Each Fldrfl In Fldr.Files > If Microsoft.VisualBasic.Right(Fldrfl.Name, > Microsoft.VisualBasic.Len(Fldrfl.Name) - > Microsoft.VisualBasic.InStrRev(Fldrfl.Name, ".")) = TextBox2.Text Then See System.IO.Path.GetExtension() > Fldrfl.Copy(TextBox3.Text & "\" & Fldrfl.Name) > n = n + 1 > ProgressBar1.Value = (n / Fldrfl.Count) * 100 You probably meant Fldr.Count > Application.DoEvents() > End If > Next > > Fldrfl = Nothing > Fldr = Nothing > Fso = Nothing In general, it's best not to set things to Nothing because it gets in the way of garbage collection. > MsgBox("Files have been copied successful!", MsgBoxStyle.Information, > "Done!") > End If > End Sub -- Andrew
From: K on 11 Jun 2010 04:59 Hi Andrew, Thanks for replying. Is it possible for you to write me a code which can achive what i need.
From: Andrew Morton on 11 Jun 2010 05:15 K wrote: > Hi Andrew, Thanks for replying. Is it possible for you to write me a > code which can achive what i need. Well, I'm not very busy right now... tell us exactly what the code should do - the names of your TextBoxes don't help to explain. -- Andrew
From: K on 11 Jun 2010 05:39
Thanks for you help Andrew. Ok i have checkbox on my form saying "include subforms". if checkbox is unticked then i want macro to copy all those files which have extention ".xlsx" from folder "C\Documents \Target" to "C\Documents\Destination". And if checkbox is ticked then macro should copy all "xlsx" files from folder "C\Documents\Target" as well as from all Subfolder which exists in folder "C\Documents \Target" to "C\Documents\Destination". and i also what progress bar code in between to show the progress. Please Note that macro should only copy files of which attributes are not hidden. It might be possible that file is open in the folder while macro is running. I have slightly modified the code below so you can have good idea Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim FldrNm As String Dim Fso As Object Dim Fldr As Object Dim Fldrfl As Object Dim n As Long FldrNm = "C\Documents\Target" Fso = CreateObject("Scripting.FileSystemObject") Fldr = Fso.GetFolder(FldrNm) For Each Fldrfl In Fldr.Files If Microsoft.VisualBasic.Right(Fldrfl.Name, Microsoft.VisualBasic.Len(Fldrfl.Name) - Microsoft.VisualBasic.InStrRev(Fldrfl.Name, ".")) = "xlsx" Fldrfl.Copy("C\Documents\Destination" & "\" & Fldrfl.Name) n = n + 1 ProgressBar1.Value = (n / Fldrfl.Count) * 100 Application.DoEvents() End If Next MsgBox("Files have been copied successful!", MsgBoxStyle.Information, "Done!") End If End Sub |