From: Andrew Morton on
K wrote:
> 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.
>

Something like this:

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


From: Family Tree Mike on
On 6/11/2010 4:14 AM, 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.
>
> 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

Perhaps Fldrfl.Count is an error? From your code, Fldrfl should be a
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
From: K on
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.

First  |  Prev  | 
Pages: 1 2
Prev: elearning info not working
Next: Inner class problem.