?
Solved

Parse Text

Posted on 2011-03-17
5
Medium Priority
?
356 Views
Last Modified: 2012-05-11
I need to recursively parse all files in a folder and return the values that are present to the right of the text found within.

The text within is always the same:

ram Files\Queue\textfile.txt

I need to get a result set containing the:

file name searched
textfile.txt values within each found file

Thanks!
0
Comment
Question by:CodexK2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35160412
Could you describe your problem more precisely? With some examples.
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 35161234
find different filetypes example:
Get-ChildItem C:\PSTest\ -Recurse -include *.txt, *.exe, *.ini

Read file:
get-content textfile.txt
0
 

Author Comment

by:CodexK2
ID: 35162856
Thank you - I know how to recursively look for files within a folder (and search for strings within these files).

What I need help with is searching within each and every file within a given folder for this text:

Files\Queue\textfile.txt

...and then only returning the value found to the right of 'Files\Queue'

So...in the example above the value returned would be:

'textfile.txt'

Ideally the result set will output the name of the file and the values found.


0
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 35163769
Try this. You can adjust the pattaren in the first line and the place of the text files to search in the 3rd line.

This will result in a table like this (it even gives you the line number within the text file):
linenumber filename      patternfound            
---------- --------      ------------            
         1 parsetext.txt textfile1.txt            
         2 parsetext.txt textfile2.txt            
         1 pars2text.txt subfolder\textfilesub.txt

In my example the 3rd match shows, that it gives you the subfolder path. If you do not need that, only the filenname, tell me, I'll adjust the code.
$pattern = "Files\\Queue\\([^\.]+\.txt)"

Get-ChildItem c:\yourpathtotxtfiles -Recurse -Filter *.txt | Select-String -Pattern $pattern | %{
    $filename = $_.filename
    $linenumber = $_.linenumber
    $_.matches | %{
        new-object -TypeName PSObject -Property @{
            filename = $filename
            patternfound = $_.groups[1].value
            linenumber = $linenumber
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:CodexK2
ID: 35172405
Works like a Champ - thank you very much!!
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question