Solved

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

Posted on 2016-11-21
4
36 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A brief introduction to what I consider to be the best editor for PowerShell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

21 Experts available now in Live!

Get 1:1 Help Now