Differences
This shows you the differences between the selected revisions of the page.
library_example_recursive_search_text 2018-02-20 | library_example_recursive_search_text 2025-04-10 (current) | ||
Line 8: | Line 8: | ||
<code> | <code> | ||
- | powershell.exe -File SearchText.ps1 -path "/path" -text "text" | + | powershell.exe -File SearchText.ps1 -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -path "/path" -text "text" |
</code> | </code> | ||
- | See also [[library_example_listing_files_matching_wildcard|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 ''Action on match'' block accordingly. | ||
Line 18: | Line 18: | ||
# @name &Search for Text... | # @name &Search for Text... | ||
# @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ | ||
- | # -sessionUrl "!S" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^ | + | # -sessionUrl "!E" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^ |
# -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 5 | + | # @version 8 |
# @homepage ~~SELF~~ | # @homepage ~~SELF~~ | ||
- | # @require WinSCP 5.13 | + | # @require WinSCP 5.16 |
# @option Text -run textbox "Text:" | # @option Text -run textbox "Text:" | ||
# @option Wildcard -run textbox "File mask:" "*.*" | # @option Wildcard -run textbox "File mask:" "*.*" | ||
Line 31: | 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 = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
[Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
$path, | $path, | ||
Line 44: | Line 44: | ||
try | try | ||
{ | { | ||
+ | if (!$text) | ||
+ | { | ||
+ | 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 } | ||
Line 73: | 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 | ||
} | } | ||
} | } | ||
Line 131: | Line 143: | ||
In the //File mask// box, specify a [[file_mask|file mask]] to select files. The option is available when executing the extension only. | In the //File mask// box, specify a [[file_mask|file mask]] to select files. The option is available when executing the extension only. | ||
- | In the //Session log file// you can specify a path to a [[logging|session log file]]. The option is available on the [[ui_pref_commands|Preferences dialog]] only. | + | In the //Session log file//, you can specify a path to a [[logging|session log file]]. 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. |