• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Loop Issue - Too Many Loops

Need help fixing my loops.

I have a table which populates a gridview with data from two separate log files. Each storage system folder contains these log files and my loop needs to cycle through every folder in a specific directory and pull out these log files.

The problem is when I add the second file that pulls data the table size triples. It seems 3 iterations are performed on each folder. I'm not sure how to code this correctly. I could hack it with another gridview with just the entries from the 2nd log but i really want them all in one gridview.

Code below.
For Each slotConfigInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_SlotConfigInfo.log", SearchOption.AllDirectories)
            For Each versionInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_VersionInfo.log", SearchOption.AllDirectories)

            Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
            Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
            Dim systemName As String = slotConfigData(4).Substring(14)
            Dim systemID As String = slotConfigData(6).Substring(12)
            Dim systemModel As String = slotConfigData(8).Substring(15)
            Dim systemSerialNumber As String = slotConfigData(14).Substring(23)
            Dim cpuCount As String = slotConfigData(30).Substring(23)
            Dim memorySize As String = slotConfigData(32).Substring(14)
            Dim ontapVersion As String = versionInfoData(2).Substring(24, 9)

            'Declare 1 Row for data
            Dim Row1 As DataRow

            Try
                'Declare new row
                Row1 = Table1.NewRow()
                'Fill row with data - Item property is used to set the field value
                Row1.Item(filerName) = systemName
                Row1.Item(filerModel) = systemModel
                Row1.Item(Ontap) = ontapVersion
                Row1.Item(sysID) = systemID
                Row1.Item(sysSerial) = systemSerialNumber
                Row1.Item(sysMem) = memorySize
                Row1.Item(sysCPU) = cpuCount
                Table1.Rows.Add(Row1)

            Catch ex As Exception

            End Try

            Next
        Next

        'Bind grid to DataTable
        gridFilerProperties.DataSource = Table1
        gridFilerProperties.DataBind()

Open in new window

0
newimagent
Asked:
newimagent
  • 4
  • 3
2 Solutions
 
djon2003Commented:
These loops should not be embedded. Like this, it will give you the total number of file of one multiply by the other. Which, if I understand correctly, should be either config count + version count or max(config count, version count).

So the loops should be one next the other, or another way.
0
 
newimagentAuthor Commented:
One next to the other I cannot add the data from the second log to the same row of the gridview. I end up having to create a new row to place that data. There must be some way I can loop all of this data from two separate files into one table and place the data in the column I want to display it.

I'm not sure what I would gain by the count. Could I nest the for loop and count the number of folders in the "Storage System" directory and force the loop to stop at that number of iterations? If so can you give me example code?

Thanks!
0
 
djon2003Commented:
Well, first question.. How to how know that the info taken from one version file is going with another config file ? Because from the code you posted, it seems to be random.

When this will be answered, I'll be able to tell you how to do it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
newimagentAuthor Commented:
I know it's pulling the data correctly because the data in both files matches up with the appropriate system for all 3 entries in my test system. Because it is pulling the files correctly I would assume its looping through reading the files based on folder name alphabetically or something like that but I can't say for sure.

If there is a more programmatic way such as getting all the directory names in the Storage System directory and then calling each of those values in a new loop.... I'm not really sure what to do here because i need data from two files in each row.
0
 
Rouneh10Commented:
I think what you're trying to do is something like this:

' Iterate through all subfolders of the StorageSystem folder
For Each LogFolder As String In System.IO.Directory.GetDirectories(txtPath.Text & "\StorageSystem\")
    'Create an IEnumerable<string> of files in the folder
    Dim FolderFiles = System.IO.Directory.EnumerateFiles(LogFolder)
    'Fetch the path of the SlotConfigInfo.log file if it exists, or nothing if it's not there.
    Dim slotConfigInfo As String = FolderFiles.FirstOrDefault(Function(ByRef FilePath)
                                                                  Return FilePath.Contains("SlotConfigInfo.log")
                                                              End Function)
    'Fetch the path of the VersionInfo.log file if it exists, or nothing if it's not there.
    Dim versionInfo As String = FolderFiles.FirstOrDefault(Function(ByRef FilePath)
                                                               Return FilePath.Contains("VersionInfo.log")
                                                           End Function)
    'Read the file contents into arrays
    Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
    Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
    ....
Next

Open in new window


You may want to include some logic to skip the folders where the log files aren't found.
0
 
newimagentAuthor Commented:
That looks like a good way to call the data, but it still leaves me with the issue of how do i code a loop that combines the data from each iteration into the same row of a table?

For every subfolder in the Storage System directory both of these log files will always be present.
0
 
djon2003Commented:
The example of Rouneh10 seems good. Though, it uses .NET 4 which I'm not sure the poster is using.

So, I wrote an example using your code which shall be compatible.
For Each curDir As String In IO.Directory.GetDirectories(txtPath.Text & "\StorageSystem\", "*", SearchOption.AllDirectories)

            Dim slotConfigInfo, versionInfo As String
            Dim file() As String = IO.Directory.GetFiles(curDir, "*_SlotConfigInfo.log", IO.SearchOption.TopDirectoryOnly)
            If file.Length = 0 Then Continue For 'Missing Config file in this dir
            slotConfigInfo = file(0)

            file = IO.Directory.GetFiles(curDir, "*_VersionInfo.log", IO.SearchOption.TopDirectoryOnly)
            If file.Length = 0 Then Continue For 'Missing Version file in this dir
            versionInfo = file(0)

            Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
            Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
            Dim systemName As String = slotConfigData(4).Substring(14)
            Dim systemID As String = slotConfigData(6).Substring(12)
            Dim systemModel As String = slotConfigData(8).Substring(15)
            Dim systemSerialNumber As String = slotConfigData(14).Substring(23)
            Dim cpuCount As String = slotConfigData(30).Substring(23)
            Dim memorySize As String = slotConfigData(32).Substring(14)
            Dim ontapVersion As String = versionInfoData(2).Substring(24, 9)

            'Declare 1 Row for data
            Dim Row1 As DataRow

            Try
                'Declare new row
                Row1 = Table1.NewRow()
                'Fill row with data - Item property is used to set the field value
                Row1.Item(filerName) = systemName
                Row1.Item(filerModel) = systemModel
                Row1.Item(Ontap) = ontapVersion
                Row1.Item(sysID) = systemID
                Row1.Item(sysSerial) = systemSerialNumber
                Row1.Item(sysMem) = memorySize
                Row1.Item(sysCPU) = cpuCount
                Table1.Rows.Add(Row1)

            Catch ex As Exception

            End Try


        Next

Open in new window

0
 
newimagentAuthor Commented:
Thank you for the super thorough answer djon2003!! I am currently not coding in .NET 4 though I could I am using VS2010 now. Both of these are valuable information i'm going to keep for reference. Thank you again!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now