How to alter WinSCP script for recursively searching for text to print just a folder of the first match and exit
<p>I want to be able to look for a text string in all folders on a remote FTP server. Once found, I need to know the name of the folder which contains the text document BUT <strong>not</strong> the name of the file.  </p>

<p>How can I make changes to <em>"Action on match"</em> to make this script (<a href="https://winscp.net/eng/docs/library_example_recursive_search_text" rel="nofollow noreferrer">WinSCP Extension Search recursively for text in remote directory / Grep files over SFTP/FTP protocol</a>) run silently (without displaying anything in the terminal) and once the match is found simply stop and display ONLY the name of the folder (containing the file with the text string)? Also is it possible to display the result in red? Here is the <em>"Action on match"</em> portion of the script (I tried to include the whole thing but cannot do it for some reason).</p>

<p>I can do this with the help of the latest WinSCP custom <em>Search for Text</em> button (please see .ps1 script below which makes this search function possible). However instead of just stopping upon match, the search goes on until the last folder. What is worse is that in order to find the result I need to scroll all the way back up and check every entry. Only one of them will have the name of my folder listed so that is a long procedure.</p>

<pre><code>{
    # Action on match

    # Modify the code below if you want to do another task with
    # matching files, instead of grepping their contents

    Write-Host ("File {0} matches mask, searching contents..." -f $fileInfo.FullName)
    $tempPath = (Join-Path $env:temp $fileInfo.Name)
    # Download file to temporary directory
    $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) 
    $transferResult = $session.GetFiles($sourcePath, $tempPath)
    # Did the download succeeded?
    if (!$transferResult.IsSuccess)
    {
        # Print error (but continue with other files)
        Write-Host $transferResult.Failures[0].Message
    }
    else
    {
        # Search and print lines containing "text".
        # Use -Pattern instead of -SimpleMatch for regex search
        $matchInfo = Select-String -Path $tempPath -SimpleMatch $text
        # Print the results
        foreach ($match in $matchInfo)
        {
            Write-Host ($fileInfo.FullName + ":" + $match.LineNumber + ":" + $match.Line)
        }
        # Delete temporary local copy
        Remove-Item $tempPath
    }
}
</code></pre>