Differences

This shows you the differences between the selected revisions of the page.

2025-04-10 2026-04-02
simpler message (martin) old revision restored (185.219.83.26) (hidden) (untrusted)
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 7: Line 10:
To run the script manually use: To run the script manually use:
-<code> +&amp;lt;code&amp;gt; 
-powershell.exe -File SearchText.ps1 -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -path "/path" -text "text" +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; 
-</code>+&amp;lt;/code&amp;gt;
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 ''Action on match'' 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 &amp;#039;&amp;#039;Action on match&amp;#039;&amp;#039; block accordingly.
-<code powershell - SearchText.ps1> +&amp;lt;code powershell - SearchText.ps1&amp;gt; 
-# @name        &Search for Text... +# @name        &amp;amp;Search for Text... 
-# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ +# @command      powershell.exe -ExecutionPolicy Bypass -File &amp;quot;%EXTENSION_PATH%&amp;quot; ^ 
-#                  -sessionUrl "!E" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^ +#                  -sessionUrl &amp;quot;!E&amp;quot; -path &amp;quot;!/&amp;quot; -text &amp;quot;%Text%&amp;quot; -wildcard &amp;quot;%Wildcard%&amp;quot; ^ 
-#                  -pause -sessionLogPath "%SessionLogPath%"+#                  -pause -sessionLogPath &amp;quot;%SessionLogPath%&amp;quot;
# @description  Searches recursively for a text in the current remote directory # @description  Searches recursively for a text in the current remote directory
-# @version      8+# @version      7
# @homepage    ~~SELF~~ # @homepage    ~~SELF~~
# @require      WinSCP 5.16 # @require      WinSCP 5.16
-# @option      Text -run textbox "Text:" +# @option      Text -run textbox &amp;quot;Text:&amp;quot; 
-# @option      Wildcard -run textbox "File mask:" "*.*"+# @option      Wildcard -run textbox &amp;quot;File mask:&amp;quot; &amp;quot;*.*&amp;quot;
# @option      SessionLogPath -config sessionlogfile # @option      SessionLogPath -config sessionlogfile
# @optionspage  ~~SELF~~#options # @optionspage  ~~SELF~~#options
Line 31: Line 34:
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 = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/",+    $sessionUrl = &amp;quot;sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/&amp;quot;,
    [Parameter(Mandatory = $True)]     [Parameter(Mandatory = $True)]
    $path,     $path,
    [Parameter(Mandatory = $True)]     [Parameter(Mandatory = $True)]
    $text,     $text,
-    $wildcard = "*.*",+    $wildcard = &amp;quot;*.*&amp;quot;,
    $sessionLogPath = $Null,     $sessionLogPath = $Null,
    [Switch]     [Switch]
Line 46: Line 49:
    if (!$text)     if (!$text)
    {     {
-        throw "No Text was specified."+        throw &amp;quot;No Text was specified.&amp;quot;
    }     }
    # 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 "WinSCPnet.dll")+    Add-Type -Path (Join-Path $assemblyPath &amp;quot;WinSCPnet.dll&amp;quot;)
    # Setup session options     # Setup session options
Line 78: Line 81:
            # matching files, instead of grepping their contents             # matching files, instead of grepping their contents
-            if ($fileInfo.FileType -eq &quot;L")+            Write-Host &quot;File $($fileInfo.FullName) matches mask, searching contents...&amp;quot; 
 +            $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)
            {             {
-                Write-Host &quot;Skipping symlink $($fileInfo.FullName)..."+                # Print error (but continue with other files
 + ···············Write-Host $transferResult.Failures[0].Message
            }             }
            else             else
            {             {
-                Write-Host "File $($fileInfo.FullName) matches mask, searching contents...&quot; +                # Search and print lines containing &amp;quot;text&amp;quot;
-················$tempPath = (Join-Path $env:temp $fileInfo.Name) +                # Use -Pattern instead of -SimpleMatch for regex search 
-                # Download file to temporary directory +                $matchInfo = Select-String -Path $tempPath -SimpleMatch $text 
-               $filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) +                # Print the results 
-                $transferResult = $session.GetFiles($filePath, $tempPath) +                foreach ($match in $matchInfo)
-                # Did the download succeeded? +
-                if (!$transferResult.IsSuccess)+
                {                 {
-                    # Print error (but continue with other files) +                    Write-Host ($fileInfo.FullName + &amp;quot;:&amp;quot; + $match.LineNumber + &amp;quot;:&amp;quot; + $match.Line)
-····················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 121: Line 117:
catch catch
{ {
-    Write-Host "Error: $($_.Exception.Message)"+    Write-Host &amp;quot;Error: $($_.Exception.Message)&amp;quot;
    $result = 1     $result = 1
} }
Line 128: Line 124:
if ($pause) if ($pause)
{ {
-    Write-Host "Press any key to exit..."+    Write-Host &amp;quot;Press any key to exit...&amp;quot;
    [System.Console]::ReadKey() | Out-Null     [System.Console]::ReadKey() | Out-Null
} }
exit $result exit $result
-</code>+&amp;lt;/code&amp;gt;
===== [[options]] Options ===== ===== [[options]] Options =====
-&screenshotpict(extension_recursive_search_text)+&amp;amp;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 146: Line 142:
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 185.219.83.26