Link to home
Start Free TrialLog in
Avatar of Ken H.
Ken H.

asked on

File Opening Logic

I need to parse data from specific logs files.
These log files when generated are stored in a dynamic path ex. ( c:\log_collector\storage\<system_name>\log.log

Sometimes there may be multiple <system_name> folders in the storage directory depending on how many systems the data collection was run against.

Regardless of how many are there I need to figure out how to write and If or Do Loop statement that says open all folders inside the storage folder, open the log.log in each of those folders, read the data from specific lines, create dynamic controls and place the data in these controls.

I think I'm pretty good with the rest of it except how to get the files open when i don't know the folder name.
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nasir Razzaq
Or you can simply do

Dim MyFiles As String() = IO.Directory.GetFiles("c:\log_collector\storage\", "*.log")
For each File In MyFiles
      'code to process the file.
Next
@CodeCruiser, that will only find the files in the "storage" folder. It won't search sub-folders as indicated in the question.
Avatar of Ken H.
Ken H.

ASKER

My test code for this is throwing an error of "illegal characters in path" it looks like because of my wildcard attempt *Results.log

I just realized in this section of the data collection the log files are <system_name>_Results.log ... i guess that adds a little complexity to it.
'Loop through the Storage System sub-folders and grab data
        Dim storage As String = txtPath.Text & "\StorageSystem\"
        For Each folder As String In Directory.GetDirectories(storage)
            For Each storageSystemName As String In Directory.GetFiles(folder)
                Dim storageData As String() = IO.File.ReadAllLines(storage & "*Results.log")
                Dim storageName As String = storageData(6).Substring(28)

                txtTest.Text = storageName
            Next
        Next

Open in new window

Avatar of Ken H.

ASKER

In testing this code it looks like I need to define an array for both variables, but I'm not sure how I should do that.

When i test the output given by both for statements each one only returns the first value. In other words the folder variable returns the first subfolder in the directory and storageSystemName returns the first log file inside of the above directory.
'Loop through the Storage System sub-folders and grab data
        Dim storage As String = txtPath.Text & "\StorageSystem\"
        For Each folder As String In Directory.GetDirectories(storage)
            For Each storageSystemName As String In Directory.GetFiles(folder)
                txtTest.Text = storageSystemName
            Next
        Next

Open in new window

txtTest.Text &= storageSystemName
Avatar of Ken H.

ASKER

For some reason instead of listing the 3 subdirectories inside the "StorageSystem" folder as I was expecting it looks like it's giving me each directory as many times as there are log files inside of the directory. Is there a way to filter this down to just the 3 subdirectory names and how can I pick those storage system names out of the path and place them into separate variables?

StorageSystemName returns full paths to all of the log files in all 3 directories inside the "StorageSystem" folder. How do pick one of those files for the next step of reading the file and parsing the data?

Thanks for your help with this. I have had a very hard time finding the answers to this type of logic.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>How do pick one of those files for the next step of reading the file and parsing the data?

It returns you a string array of all file paths. Just use that path to do your further processing.
Avatar of Ken H.

ASKER

Ok so to step back a second the code to get the directory list is returning multiple lines of the same value.

In the storage system directory there are 3 folders:
10.10.1.200
10.10.1.201
storage_system1

The code below returns the following output in the text box:

C:\dc\StorageSystem\10.10.1.200
C:\dc\StorageSystem\10.10.1.200
C:\dc\StorageSystem\10.10.1.201
C:\dc\StorageSystem\10.10.1.200
C:\dc\StorageSystem\10.10.1.201
C:\dc\StorageSystem\storage_system1

Why would it return so many of the same value?
Dim storage As String = txtPath.Text & "\StorageSystem\"
        Dim folderString As String = ""
        For Each folder As String In Directory.GetDirectories(storage)
            folderString &= folder.ToString
            folderString &= vbCrLf
            txtTest.Text &= folderString
        Next

Open in new window

Avatar of Ken H.

ASKER

ok I got that piece worked out. had an &= in the text box control line.

How I can pick a specific folder value out of the array.
So you want to loop through folders and then loop through each folder to get all its files rather than just get the list of all files from all folders?
Avatar of Ken H.

ASKER

Yes. I'm not sure exactly how i'm going to do it but the plan is one of two things. The number of storage systems can be anywhere from 1 -6 so either I create a bunch of controls to display their data and count the number of folders and make that many controls visible or I dynamically generate the controls that will display the content based on the number of folders.
Avatar of Ken H.

ASKER

Ok I've got both pieces of code working now and i'm able to successfully output them to a text box to view the array contents. I am going to split the points between you since each of you gave me half the answer i was looking for. Thanks!
Thanks for appreciating our help with that grade :-)