Click here to Skip to main content
15,918,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

Below code is working fine, but takes long time. Its not the network speed, I believe. Can you please help?

VB
Dim i As Integer = 0
       Dim BatchNumber As String

       'Target Directory
       Dim directory = "\\fppdszv03\GPM-QM\PROD\Indigo5500" '     C:\Users\Shafiul Alam\Desktop\Hot Folder

       For i = 0 To dgv.Rows.Count - 1

           BatchNumber = dgv.Rows(i).Cells(0).Value & "*.mjm"
           For Each filename As String In IO.Directory.GetFiles(directory, BatchNumber, IO.SearchOption.AllDirectories)

               dgv.Rows(i).Cells(5).Value = "Searching.."
               'The next line of code gets only file extensions from searched directories and subdirectories
               Dim fName As String = filename 'IO.Path.GetExtension(filename)
               Dim FileInfo As New FileInfo(filename)

               dgv.Rows(i).Cells(6).Value = FileInfo.Name
               dgv.Rows(i).Cells(7).Value = filename
               dgv.Rows(i).Cells(5).Value = "File Found"
               'Label6.Text = Label6.Text + 1
               dgv.Rows(i).Cells(8).Value = "C:\Users\Shafiul Alam\Desktop\DFE\" & FileInfo.Name
               File.Copy(dgv.Rows(i).Cells(7).Value, dgv.Rows(i).Cells(8).Value)
               dgv.Rows(i).Cells(5).Value = "Send to DFE"
               'Label7.Text = Label7.Text + 1
               dgv.Rows(i).Cells(5).Style.BackColor = Color.DarkGreen
           Next
       Next
Posted
Comments
Suvendu Shekhar Giri 30-Jun-15 6:36am    
How many files are there in the selected directory?
Shafiul Alam 30-Jun-15 7:04am    
There a lot of them, and will be increasing day by day.

1 solution

Firstly, take a look at your directory: since the GetFiles call is returning all files in this and all subdirectories, it may take some considerable time to return, as the file system has crawl the entire structure.

Secondly, look at what you are doing inside the inner loop - updating the same cells over and over again.

Remember, you are doing this inside a loop on the UI thread - so there is a good chance that the directory search is blocking any updates anyway.

I'd consider moving the code into a BackgroundWorker, and issuing an "update" for a row via a Progress report.
 
Share this answer
 
Comments
Shafiul Alam 30-Jun-15 7:01am    
Hi OriginalGriff, Can you help me to modify the code so that, it does not returning all files.

Thanks
OriginalGriff 30-Jun-15 7:07am    
What are you actually trying to do? Because it sounds a lot like you are just guessing here - and there may be a better way to do the actual task.
Shafiul Alam 30-Jun-15 7:21am    
Thanks for Asking. I am trying to copy all the files from One Location to another. We dont know the file name, but only a part of it.

Trying to communicate the status through the DataGridView.
OriginalGriff 30-Jun-15 7:57am    
OK - are all the files in a single directory? If so, then just use

For Each filename As String In IO.Directory.GetFiles(directory, BatchNumber)

That way, it won't look at subdirectories.

Then, separate the move (you are actually copying there) from the reporting: use a BackgroundWorker to do all the "donkey work" and report it's progress back to the UI thread via the ProgressChanged event - the ProgressChangedEventArgs class has a UserState property you can use to pass a class instance containing all the update info you need as an object.
Handle the event in the UI thread and update the DGV.

What happens then is that the slow stuff is done on a separate thread, so updates to the DGV occur even while the files are copying - because the UI thread isn't busy copying files across your network, which is "blocking" operation.

You should see progress information updated a lot better, even if it doesn't speed up the total operation.

(But do remember that lots of small files can take a lot of time to copy - more than the equivalent as a single huge file would)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900