[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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.
0
newimagent
Asked:
newimagent
  • 7
  • 5
  • 3
2 Solutions
 
strickddCommented:
You can iterate through the directories like this:

foreach folder As String in Directory.GetDirectories("C:\log_collector\storage")

Loop

Next you can loop through the files in that directory in a similar manner:

foreach fileName as String in Directory.GetFiles(folder)
'Parse file info here. The path to the file is "fileName" variable.
loop
0
 
CodeCruiserCommented:
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
0
 
strickddCommented:
@CodeCruiser, that will only find the files in the "storage" folder. It won't search sub-folders as indicated in the question.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
newimagentAuthor Commented:
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

0
 
newimagentAuthor Commented:
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

0
 
strickddCommented:
txtTest.Text &= storageSystemName
0
 
newimagentAuthor Commented:
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.
0
 
CodeCruiserCommented:
oops.

Change

Dim MyFiles As String() = IO.Directory.GetFiles("c:\log_collector\storage\", "*.log")

to

Dim MyFiles As String() = IO.Directory.GetFiles("c:\log_collector\storage\", "*.log", SearchOption.AllDirectories)

And you can use the criteria in second parameter where I use *.log

http://msdn.microsoft.com/en-us/library/ms143316.aspx


for searching in all the subdirectories.
0
 
CodeCruiserCommented:
>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.
0
 
newimagentAuthor Commented:
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

0
 
newimagentAuthor Commented:
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.
0
 
CodeCruiserCommented:
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?
0
 
newimagentAuthor Commented:
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.
0
 
newimagentAuthor Commented:
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!
0
 
CodeCruiserCommented:
Thanks for appreciating our help with that grade :-)
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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