How to streamline code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmerboy
    New Member
    • Jul 2007
    • 84

    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

    Code:
    Dim di As New DirectoryInfo(folderPath)
    Dim totalFiles As Integer = 0
    Dim totalFolders As Integer = 0
    Dim totalSizeMB As Double = 0
    Dim innerDI As DirectoryInfo
    Dim tempFile As FileInfo
    
    totalFolders = di.GetDirectories("*", SearchOption.AllDirectories).Length
    totalFiles = di.GetFiles("*", SearchOption.AllDirectories).Length
    
    For Each innerDI In di.GetDirectories("*", SearchOption.AllDirectories)
                    For Each tempFile In innerDI.GetFiles("*", SearchOption.TopDirectoryOnly)
                        totalSizeMB += tempFile.Length
                    Next
    Next
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    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.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well I can offer a way to save you a little bit of duplicate work.
      [code=vbnet]
      Dim di As New DirectoryInfo(f olderPath)
      Dim totalFiles As Integer = 0
      Dim totalFolders As Integer = 0
      Dim totalSizeMB As Double = 0
      Dim tempFile As FileInfo
      Dim files As FileInfo[]

      totalFolders =di.GetDirector ies("*", SearchOption.Al lDirectories).L ength
      files=di.GetFil es("*", SearchOption.Al lDirectories)
      totalFiles = files.Length

      For Each tempFile In files
      totalSizeMB += tempFile.Length
      Next
      [/code]

      Comment

      • programmerboy
        New Member
        • Jul 2007
        • 84

        #4
        Originally posted by Plater
        Well I can offer a way to save you a little bit of duplicate work.
        [code=vbnet]
        Dim di As New DirectoryInfo(f olderPath)
        Dim totalFiles As Integer = 0
        Dim totalFolders As Integer = 0
        Dim totalSizeMB As Double = 0
        Dim tempFile As FileInfo
        Dim files As FileInfo[]

        totalFolders =di.GetDirector ies("*", SearchOption.Al lDirectories).L ength
        files=di.GetFil es("*", SearchOption.Al lDirectories)
        totalFiles = files.Length

        For Each tempFile In files
        totalSizeMB += tempFile.Length
        Next
        [/code]
        Thanks guys. It helps but I am still curious to know how to make it work like Windows.

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by programmerboy
          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.

          Comment

          • programmerboy
            New Member
            • Jul 2007
            • 84

            #6
            Originally posted by TRScheel
            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)?

            Comment

            • TRScheel
              Recognized Expert Contributor
              • Apr 2007
              • 638

              #7
              Originally posted by programmerboy
              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:
              Code:
              object object1 = new object();
              int object2 = 0;
              string object3 = "";
              List<object> objects = new List<object>();
              objects.Add(object1);
              objects.Add(object2);
              objects.Add(object3);
              Thread t = new Thread(new ParameterizedThreadStart(SomeFunction));
              t.Start(objects);
              ... somewhere else in your code

              Code:
              static void SomeFunction(object parameters)
              {
                  List<object> objects = (List<object>)parameters;
                  object someObject = objects[0];
                  int someInt = (int)objects[1];
                  string someString = (string)objects[2];
              
                  // do stuff
              }
              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 FileSystemWatch er 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.
              Last edited by TRScheel; Jul 28 '08, 11:03 PM. Reason: Stupid code mistake

              Comment

              Working...