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) simpler message (martin)
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:
-&amp;lt;code&amp;gt; +<code> 
-powershell.exe -File SearchText.ps1 -sessionUrl &amp;quot;sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/&amp;quot; -path &amp;quot;/path&amp;quot; -text &amp;quot;text&amp;quot; +powershell.exe -File SearchText.ps1 -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -path "/path" -text "text" 
-&amp;lt;/code&amp;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 &amp;#039;&amp;#039;Action on match&amp;#039;&amp;#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.
-&amp;lt;code powershell - SearchText.ps1&amp;gt; +<code powershell - SearchText.ps1> 
-# @name        &amp;amp;Search for Text... +# @name        &Search for Text... 
-# @command      powershell.exe -ExecutionPolicy Bypass -File &amp;quot;%EXTENSION_PATH%&amp;quot; ^ +# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ 
-#                  -sessionUrl &amp;quot;!E&amp;quot; -path &amp;quot;!/&amp;quot; -text &amp;quot;%Text%&amp;quot; -wildcard &amp;quot;%Wildcard%&amp;quot; ^ +#                  -sessionUrl "!E" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^ 
-#                  -pause -sessionLogPath &amp;quot;%SessionLogPath%&amp;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 &amp;quot;Text:&amp;quot; +# @option      Text -run textbox "Text:" 
-# @option      Wildcard -run textbox &amp;quot;File mask:&amp;quot; &amp;quot;*.*&amp;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 = &amp;quot;sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/&amp;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 = &amp;quot;*.*&amp;quot;,+    $wildcard = "*.*",
    $sessionLogPath = $Null,     $sessionLogPath = $Null,
    [Switch]     [Switch]
Line 49: Line 46:
    if (!$text)     if (!$text)
    {     {
-        throw &amp;quot;No Text was specified.&amp;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 &amp;quot;WinSCPnet.dll&amp;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 &quot;File $($fileInfo.FullName) matches mask, searching contents...&amp;quot; +            if ($fileInfo.FileType -eq &quot;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 &quot;Skipping symlink $($fileInfo.FullName)..."
- ···············Write-Host $transferResult.Failures[0].Message+
            }             }
            else             else
            {             {
-                # Search and print lines containing &amp;quot;text&amp;quot;+                Write-Host "File $($fileInfo.FullName) matches mask, searching contents...&quot; 
-                # 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 + &amp;quot;:&amp;quot; + $match.LineNumber + &amp;quot;:&amp;quot; + $match.Line)+                    # Print error (but continue with other files) 
 +····················Write-Host $transferResult.Failures[0].Message 
 +               } 
 + ···············else 
 +                { 
 +                    # Search and print lines containing &quot;text&quot;. 
 +                   # Use -Pattern instead of -SimpleMatch for regex search 
 +                    $matchInfo = Select-String -Path $tempPath -SimpleMatch $text 
 +                    # Print the results 
 +                    foreach ($match in $matchInfo
 +                   { 
 + ·······················Write-Host &quot;$($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 &amp;quot;Error: $($_.Exception.Message)&amp;quot;+    Write-Host "Error: $($_.Exception.Message)"
    $result = 1     $result = 1
} }
Line 124: Line 128:
if ($pause) if ($pause)
{ {
-    Write-Host &amp;quot;Press any key to exit...&amp;quot;+    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null     [System.Console]::ReadKey() | Out-Null
} }
exit $result exit $result
-&amp;lt;/code&amp;gt;+</code>
===== [[options]] Options ===== ===== [[options]] Options =====
-&amp;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 

Last modified: by martin