473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to streamline code

In a previous post I have asked how to get total # of files, folders and folder size. By the help of you guys, I was able to get the code and it runs fine. But I am having an issue. I am trying to develop an application that will connect to different servers and will get their data. All servers will have same user name and password, so connecting to them is not an issue. What I am seeing is when I run my code it takes 15-20 secs to return total # of files, folders, and size. But when I open the same folder in window's explorer (using UNC path)and click properties then the same folder returns data within couple of seconds. If anyone can let me know the reasons and ways to improve the following code then I will appreciate it.
Thanks

P.S: Does anyone of you know what to put in CODE tag to show it as either vb or C# code

Expand|Select|Wrap|Line Numbers
  1. Dim di As New DirectoryInfo(folderPath)
  2. Dim totalFiles As Integer = 0
  3. Dim totalFolders As Integer = 0
  4. Dim totalSizeMB As Double = 0
  5. Dim innerDI As DirectoryInfo
  6. Dim tempFile As FileInfo
  7.  
  8. totalFolders = di.GetDirectories("*", SearchOption.AllDirectories).Length
  9. totalFiles = di.GetFiles("*", SearchOption.AllDirectories).Length
  10.  
  11. For Each innerDI In di.GetDirectories("*", SearchOption.AllDirectories)
  12.                 For Each tempFile In innerDI.GetFiles("*", SearchOption.TopDirectoryOnly)
  13.                     totalSizeMB += tempFile.Length
  14.                 Next
  15. Next
Jul 28 '08 #1
6 1547
Curtis Rutland
3,256 Expert 2GB
I don't know about the streamlining, but I know that for now, you can't define your code as one type or another. We used to be able to do it, but not anymore. Just put a comment at the top or your code, if you feel that it is necessary. Most people can tell at a glance what kind of code you are posting.
Jul 28 '08 #2
Plater
7,872 Expert 4TB
Well I can offer a way to save you a little bit of duplicate work.
Expand|Select|Wrap|Line Numbers
  1. Dim di As New DirectoryInfo(folderPath)
  2. Dim totalFiles As Integer = 0
  3. Dim totalFolders As Integer = 0
  4. Dim totalSizeMB As Double = 0
  5. Dim tempFile As FileInfo
  6. Dim files As FileInfo[]
  7.  
  8. totalFolders =di.GetDirectories("*", SearchOption.AllDirectories).Length
  9. files=di.GetFiles("*", SearchOption.AllDirectories)
  10. totalFiles = files.Length
  11.  
  12. For Each tempFile In files
  13.     totalSizeMB += tempFile.Length
  14. Next
  15.  
Jul 28 '08 #3
Well I can offer a way to save you a little bit of duplicate work.
Expand|Select|Wrap|Line Numbers
  1. Dim di As New DirectoryInfo(folderPath)
  2. Dim totalFiles As Integer = 0
  3. Dim totalFolders As Integer = 0
  4. Dim totalSizeMB As Double = 0
  5. Dim tempFile As FileInfo
  6. Dim files As FileInfo[]
  7.  
  8. totalFolders =di.GetDirectories("*", SearchOption.AllDirectories).Length
  9. files=di.GetFiles("*", SearchOption.AllDirectories)
  10. totalFiles = files.Length
  11.  
  12. For Each tempFile In files
  13.     totalSizeMB += tempFile.Length
  14. Next
  15.  
Thanks guys. It helps but I am still curious to know how to make it work like Windows.
Jul 28 '08 #4
TRScheel
638 Expert 512MB
Thanks guys. It helps but I am still curious to know how to make it work like Windows.
First step would be to thread the requests, especially if you already have a list of paths. The above code will probably take about 3 times as long as windows will. Windows goes a lot faster because it has an index to pull from. If you wanted similar results, create your own index. Instead of saying get every file it just asks get every file size.

Least ways, that's the way I understand it. I could be way off base.
Jul 28 '08 #5
First step would be to thread the requests, especially if you already have a list of paths. The above code will probably take about 3 times as long as windows will. Windows goes a lot faster because it has an index to pull from. If you wanted similar results, create your own index. Instead of saying get every file it just asks get every file size.

Least ways, that's the way I understand it. I could be way off base.
Your reply makes sense. I am not too sure about how to make threads in my my code, and also how do you create an index of your own (similar to Windows)?
Jul 28 '08 #6
TRScheel
638 Expert 512MB
Your reply makes sense. I am not too sure about how to make threads in my my code, and also how do you create an index of your own (similar to Windows)?
Keeping with the KISS mentality and without getting into intricate details about threads, here is some sampler code:
Expand|Select|Wrap|Line Numbers
  1. object object1 = new object();
  2. int object2 = 0;
  3. string object3 = "";
  4. List<object> objects = new List<object>();
  5. objects.Add(object1);
  6. objects.Add(object2);
  7. objects.Add(object3);
  8. Thread t = new Thread(new ParameterizedThreadStart(SomeFunction));
  9. t.Start(objects);
  10.  
... somewhere else in your code

Expand|Select|Wrap|Line Numbers
  1. static void SomeFunction(object parameters)
  2. {
  3.     List<object> objects = (List<object>)parameters;
  4.     object someObject = objects[0];
  5.     int someInt = (int)objects[1];
  6.     string someString = (string)objects[2];
  7.  
  8.     // do stuff
  9. }
  10.  
There is another version of a thread you can call without the parameter if you didnt want to pass something. In your case you would probably pass a folder or multiple folders for that thread to handle.


The problem with you creating an index is that you will need to watch the folders for changes which will be doing much the same thing as you are doing now every time the program restarts. If you can afford to have the program watch the folders indefinitely you could just have it use a FileSystemWatcher and any time a file/folder updates, deletes, or gets added you update your index.

For the index I would suggest SQL if it is going to be sufficiently large, XML if it is smaller.
Jul 28 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
4
by: Kathy | last post by:
Hi All, I am using Access 2000. I would like to streamline this code by using a variable for the column name. I have three tables with 255 columns each that I would like to populate with the...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
0
by: rw2007 | last post by:
I'd like to hear what you think about this... I'm working on a new architecture for an existing software product. I want this to be a flexible - and especially extendable - architecture for future...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.