find row text in files

This topic contains 4 replies, has 3 voices, and was last updated by  Darren 2 years, 7 months ago.

  • Author
  • #30709


    Hi All,
    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" |
    ForEach-Object {
    (Get-Content $_.FullName | Select-String "H: \\") | out-file c:\coll1.txt


  • #30711

    Flynn Bundy

    Hey Mate,

    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.

  • #30712

    Curtis Smith

    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
  • #30724


    Thank you both for the great tips!
    yep, the spaces throw me for a loop 🙂 it works now after i added //// Thanks again!

  • #30725


    My apologies I meant to say Curtis

You must be logged in to reply to this topic.