Differences
This shows you the differences between the selected revisions of the page.
| 2026-04-02 | 2026-04-02 (current) | ||
| old revision restored (185.219.83.26) (hidden) (untrusted) | Restored revision 1744283302. Undoing revision 1775125257. (martin) (hidden) | ||
| Line 1: | Line 1: | ||
| - | 317 | ||
| - | |||
| - | |||
| ====== Search recursively for text in remote directory / Grep files over SFTP/FTP protocol ====== | ====== Search recursively for text in remote directory / Grep files over SFTP/FTP protocol ====== | ||
| Line 10: | Line 7: | ||
| To run the script manually use: | To run the script manually use: | ||
| - | &lt;code&gt; | + | <code> |
| - | powershell.exe -File SearchText.ps1 -sessionUrl &quot;sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/&quot; -path &quot;/path&quot; -text &quot;text&quot; | + | powershell.exe -File SearchText.ps1 -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -path "/path" -text "text" |
| - | &lt;/code&gt; | + | </code> |
| See also [[library_example_listing_files_matching_wildcard|*]]. | See also [[library_example_listing_files_matching_wildcard|*]]. | ||
| - | You can alter the script for other tasks, instead of grepping the matching files. You can for example [[library_session_removefiles|remove]] or [[library_session_getfiles|download]] the matching files. Just modify the action in the &#039;&#039;Action on match&#039;&#039; block accordingly. | + | You can alter the script for other tasks, instead of grepping the matching files. You can for example [[library_session_removefiles|remove]] or [[library_session_getfiles|download]] the matching files. Just modify the action in the ''Action on match'' block accordingly. |
| - | &lt;code powershell - SearchText.ps1&gt; | + | <code powershell - SearchText.ps1> |
| - | # @name &amp;Search for Text... | + | # @name &Search for Text... |
| - | # @command powershell.exe -ExecutionPolicy Bypass -File &quot;%EXTENSION_PATH%&quot; ^ | + | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ |
| - | # -sessionUrl &quot;!E&quot; -path &quot;!/&quot; -text &quot;%Text%&quot; -wildcard &quot;%Wildcard%&quot; ^ | + | # -sessionUrl "!E" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^ |
| - | # -pause -sessionLogPath &quot;%SessionLogPath%&quot; | + | # -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 | ||
| - | # @option Text -run textbox &quot;Text:&quot; | + | # @option Text -run textbox "Text:" |
| - | # @option Wildcard -run textbox &quot;File mask:&quot; &quot;*.*&quot; | + | # @option Wildcard -run textbox "File mask:" "*.*" |
| # @option SessionLogPath -config sessionlogfile | # @option SessionLogPath -config sessionlogfile | ||
| # @optionspage ~~SELF~~#options | # @optionspage ~~SELF~~#options | ||
| Line 34: | Line 31: | ||
| param ( | param ( | ||
| # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | ||
| - | $sessionUrl = &quot;sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/&quot;, | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
| [Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
| $path, | $path, | ||
| [Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
| $text, | $text, | ||
| - | $wildcard = &quot;*.*&quot;, | + | $wildcard = "*.*", |
| $sessionLogPath = $Null, | $sessionLogPath = $Null, | ||
| [Switch] | [Switch] | ||
| Line 49: | Line 46: | ||
| if (!$text) | if (!$text) | ||
| { | { | ||
| - | throw &quot;No Text was specified.&quot; | + | throw "No Text was specified." |
| } | } | ||
| # Load WinSCP .NET assembly | # Load WinSCP .NET assembly | ||
| $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } | $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } | ||
| - | Add-Type -Path (Join-Path $assemblyPath &quot;WinSCPnet.dll&quot;) | + | Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll") |
| # Setup session options | # Setup session options | ||
| Line 81: | 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...&quot; | + | 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 &quot;text&quot;. | + | 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 + &quot;:&quot; + $match.LineNumber + &quot;:&quot; + $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 | ||
| } | } | ||
| } | } | ||
| Line 117: | Line 121: | ||
| catch | catch | ||
| { | { | ||
| - | Write-Host &quot;Error: $($_.Exception.Message)&quot; | + | Write-Host "Error: $($_.Exception.Message)" |
| $result = 1 | $result = 1 | ||
| } | } | ||
| Line 124: | Line 128: | ||
| if ($pause) | if ($pause) | ||
| { | { | ||
| - | Write-Host &quot;Press any key to exit...&quot; | + | Write-Host "Press any key to exit..." |
| [System.Console]::ReadKey() | Out-Null | [System.Console]::ReadKey() | Out-Null | ||
| } | } | ||
| exit $result | exit $result | ||
| - | &lt;/code&gt; | + | </code> |
| ===== [[options]] Options ===== | ===== [[options]] Options ===== | ||
| - | &amp;screenshotpict(extension_recursive_search_text) | + | &screenshotpict(extension_recursive_search_text) |
| In the //Text// box, specify the text to look for. The option is available when executing the extension only. | In the //Text// box, specify the text to look for. The option is available when executing the extension only. | ||
| Line 142: | Line 146: | ||
| In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension. The option is available on the [[ui_pref_commands|Preferences dialog]] only. | In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension. The option is available on the [[ui_pref_commands|Preferences dialog]] only. | ||
| - | |||
| - | 1 | ||