Differences
This shows you the differences between the selected revisions of the page.
| library_example_recursive_search_text 2022-06-16 | library_example_recursive_search_text 2025-04-10 (current) | ||
| Line 21: | Line 21: | ||
| # -pause -sessionLogPath "%SessionLogPath%" | # -pause -sessionLogPath "%SessionLogPath%" | ||
| # @description Searches recursively for a text in the current remote directory | # @description Searches recursively for a text in the current remote directory | ||
| - | # @version 7 | + | # @version 8 |
| # @homepage ~~SELF~~ | # @homepage ~~SELF~~ | ||
| # @require WinSCP 5.16 | # @require WinSCP 5.16 | ||
| Line 78: | Line 78: | ||
| # matching files, instead of grepping their contents | # matching files, instead of grepping their contents | ||
| - | Write-Host "File $($fileInfo.FullName) matches mask, searching contents..." | + | if ($fileInfo.FileType -eq "L") |
| - | $tempPath = (Join-Path $env:temp $fileInfo.Name) | + | |
| - | # Download file to temporary directory | + | |
| - | $filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) | + | |
| - | $transferResult = $session.GetFiles($filePath, $tempPath) | + | |
| - | # Did the download succeeded? | + | |
| - | if (!$transferResult.IsSuccess) | + | |
| { | { | ||
| - | # Print error (but continue with other files) | + | Write-Host "Skipping symlink $($fileInfo.FullName)..." |
| - | ···············Write-Host $transferResult.Failures[0].Message | + | |
| } | } | ||
| else | else | ||
| { | { | ||
| - | # Search and print lines containing "text". | + | Write-Host "File $($fileInfo.FullName) matches mask, searching contents..." |
| - | # Use -Pattern instead of -SimpleMatch for regex search | + | $tempPath = (Join-Path $env:temp $fileInfo.Name) |
| - | $matchInfo = Select-String -Path $tempPath -SimpleMatch $text | + | ···············# Download file to temporary directory |
| - | # Print the results | + | $filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) |
| - | foreach ($match in $matchInfo) | + | ···············$transferResult = $session.GetFiles($filePath, $tempPath) |
| + | # Did the download succeeded? | ||
| + | if (!$transferResult.IsSuccess) | ||
| { | { | ||
| - | Write-Host ($fileInfo.FullName + ":" + $match.LineNumber + ":" + $match.Line) | + | # 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 | ||
| } | } | ||
| - | # Delete temporary local copy | ||
| - | Remove-Item $tempPath | ||
| } | } | ||
| } | } | ||