|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
VB copy files from one folder to otherI 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 K wrote:
> Hi all, I am using Visual Basic 2008. I am working on code with which Go on, give us a chance - what's the error message?> 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. However... > Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As Why Long rather than Integer?> 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 I'm pretty sure you'd be better off using System.IO for this sort of thing.> Fso = CreateObject("Scripting.FileSystemObject") > Fldr = Fso.GetFolder(FldrNm) See System.IO.Path.GetExtension()> 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) You probably meant Fldr.Count> n = n + 1 > ProgressBar1.Value = (n / Fldrfl.Count) * 100 > Application.DoEvents() In general, it's best not to set things to Nothing because it gets in the > End If > Next > > Fldrfl = Nothing > Fldr = Nothing > Fso = Nothing way of garbage collection. > MsgBox("Files have been copied successful!", MsgBoxStyle.Information, > "Done!") > End If > End Sub -- Andrew Hi Andrew, Thanks for replying. Is it possible for you to write me a
code which can achive what i need. K wrote:
> Hi Andrew, Thanks for replying. Is it possible for you to write me a Well, I'm not very busy right now... tell us exactly what the code should > code which can achive what i need. do - the names of your TextBoxes don't help to explain. -- Andrew 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 K wrote:
> Thanks for you help Andrew. Ok i have checkbox on my form saying Something like this:> "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. > Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim sourceDir = "C:\Documents\Target" Dim destinationDir = "C:\Documents\Destination" If Not (Directory.Exists(sourceDir)) Then MsgBox("No source folder - " & sourceDir) Exit Sub End If Dim subdirsOption As SearchOption = SearchOption.TopDirectoryOnly If includeSubforms.Checked Then subdirsOption = SearchOption.AllDirectories End If ' get the filenames of the non-hidden xlsx files Dim files = From f In Directory.GetFiles(sourceDir, "*.xlsx", subdirsOption) Where ((New FileInfo(f).Attributes) And FileAttributes.Hidden) = 0 Select f If files Is Nothing OrElse files.Count = 0 Then MsgBox("No files were found to copy.") Exit Sub End If If Not Directory.Exists(destinationDir) Then Directory.CreateDirectory(destinationDir) End If Dim n As Integer = 0 For Each f In files File.Copy(f, Path.Combine(destinationDir, Path.GetFileName(f)), overwrite:=True) n += 1 ProgressBar1.Value = (n / files.Count) * 100 Application.DoEvents() Next ' now notify user End Sub Notes: 1) The subdirsOption is a Checkbox indicating if files should be taken from the subdirectories too. 2) If taking files from the subdirectories, they will be placed in the destination but not in their own subdirectories. 3) The File.Copy should really be in a Try...Catch structure so that exceptions can be dealt with gracefully. 4) The file copying part should really be done as a BackgroundWorker to keep the UI responsive and avoid the use of Application.DoEvents(). 5) Files will be overwritten in the destination if they already exist there. 6) I haven't tested it with a source file already being open. But it basically works as it is. HTH, -- Andrew Thanks lot Andrew for your code. Only one question that i changed
file extention from ".xlsx" to ".xls" in your code line and when i run your code it copies all the excel files instead of copying only those files which have extention ".xls". Any suggestions. On 6/11/2010 4:14 AM, K wrote:
Show quoteHide quote > Hi all, I am using Visual Basic 2008. I am working on code with which Perhaps Fldrfl.Count is an error? From your code, Fldrfl should be a > 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 file, but why would it have a count property? I think you meant Fldr.Count. Then your code would divide the iteration by the total file count. For what it is worth, Progressbar.value can throw an error when the value is not in the range 0 to 100. I think you simply have the wrong variable in use. Using .net objects rather than the FSO, and also option strict on at the top of your code will help in the long run. -- Mike
elearning info not working
in dotnet ide can you return to previous position logging to text file and opening at end of program Creating an event by force. Make a component on runtime. Inheritance and COM checking whether the current object is a specific class. Datagridview selected rows ToolStrip - how I can make rectangle on the last selected item ? ToolStrip. |
|||||||||||||||||||||||