This topic contains 4 replies, has 3 voices, and was last updated by
October 12, 2015 at 5:02 pm #30709
I am trying to find rows in test files that contain the text "H: \\". If match write the row(s) that contain "H: \\" to out-file
I tried the following but not having much luck, no results. Please help > Thanks in advance
$Path = "C:\files"
# Get all files in $Path that end in ".txt".
Get-ChildItem $Path -Filter "*.txt" |
(Get-Content $_.FullName | Select-String "H: \\") | out-file c:\coll1.txt
October 12, 2015 at 5:54 pm #30711ParticipantPoints: 0Rank: Member
This should work fine for you.
Get-ChildItem C:\Folder\*.txt -Recurse | Select-String 'H: \\' | Export-Csv C:\Matchinglines.csv -nti
-Recurse allows us to go through all of the files and folders in this directory. In this case we just simply grab any file with a .txt extension. Select-String allows us to specify what we want to find within our text files. After we've gathered that we Export that to a CSV.
As with anything in PowerShell there are a million ways to do things, but hopefully this helps.
Edit: as Curtis said, make sure you know what you're searching for as \ is the escape character in regex.
October 12, 2015 at 6:08 pm #30712ParticipantPoints: 0Rank: Member
your code does work, but have some clarification questions.
1) Do you actually expect the space between the : and the first \?
2) Are you actually wanting to test for two "\" or just one? Search-String uses RegEx match patterns, so a match pattern of "H: \\" will match "H: \". This is because "\" is a speacial metacharacter. If you literally want two "\" then your search pattern should be "H: \\\\"
and lastly, in general, you don't need to find all the txt files first using get-childitem. Get-Content can use wildcards in the path parameter
$Path = "C:\files" # Get all files in $Path that end in ".txt". Get-Content -Path "$Path\*.txt" | Select-String "H: \\\\" | Out-File c:\coll1.txt
October 13, 2015 at 3:03 am #30724
Thank you both for the great tips!
yep, the spaces throw me for a loop 🙂 it works now after i added //// Thanks again!
October 13, 2015 at 3:07 am #30725
My apologies I meant to say Curtis
The topic ‘find row text in files’ is closed to new replies.