Solved

How can I force the file content search to include all subdirectories?

Posted on 2016-11-21
4
26 Views
Last Modified: 2016-11-21
Hi Expert,

I manage to lookup and display the full line of content of all txt file (based on the criteria below).

However, I would like:

1) the searches to include all subdirectories (not just from the root)
2) output both the the respective subfoldername * filename of detected file which contents the target keyword (other than just the text line)

My current ps1 script:-

$Content = Get-Content E:\*.txt
$Content | Foreach {
                    if ($_ -like "CurrentEdition=*,*") {Write-Output $_}
                    }


Please help.
0
Comment
Question by:Brian Smith
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41895772
Try this:
Get-ChildItem E:\*.txt -Recurse | ForEach-Object {If (Select-String -Path $_.FullName -Pattern 'CurrentEdition=.*,.*') {$_}} | Select-Object DirectoryName,
Name

Open in new window

1
 

Author Comment

by:Brian Smith
ID: 41896009
Hi oBdA,

Thank you for your suggestion.

Your ps1 commandline given did list down all the *.txt filename + the respective directory (including sub-directories).

So now, how can I combined both of my script and your's together, so that the end result will show me both (the entire line of file content that met the search criteria together with the respective filename + the path location) info simultaneously?
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41896084
Get-ChildItem "E:\*.txt" -Recurse | ForEach-Object {
	$FileItem = $_
	Select-String -Path $FileItem.FullName -Pattern 'CurrentEdition=.*,.*' |
		Select-Object -Property `
			@{Name='DirectoryName'; Expression={$FileItem.DirectoryName}},
			@{Name='Name'; Expression={$FileItem.Name}},
			LineNumber,
			Line
}

Open in new window

1
 

Author Closing Comment

by:Brian Smith
ID: 41896105
Perfect! Thank you so much for making my day  :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now