Differences
This shows you the differences between the selected revisions of the page.
| 2014-12-01 | 2014-12-01 | ||
| removing session log (martin) | parameterized script + custom command suggestion + better output printing (martin) | ||
| Line 2: | Line 2: | ||
| If you do not have your favorite language, use [[library_powershell|PowerShell]]. | If you do not have your favorite language, use [[library_powershell|PowerShell]]. | ||
| + | |||
| + | You can run the script (e.g. ''search.ps1'') from WinSCP GUI using [[custom_command|local custom command]]: | ||
| + | |||
| + | <code> | ||
| + | powershell.exe -File search.ps1 -path "!/" -text !?Text:?! | ||
| + | </code> | ||
| See also [[library_example_listing_files_matching_wildcard|Listing files matching wildcard]]. | See also [[library_example_listing_files_matching_wildcard|Listing files matching wildcard]]. | ||
| <code powershell> | <code powershell> | ||
| + | param ( | ||
| + | [Parameter(Mandatory)] | ||
| + | $path, | ||
| + | [Parameter(Mandatory)] | ||
| + | $text | ||
| + | $wildcard = "*.*", | ||
| + | ) | ||
| + | |||
| function SearchDirectory ($session, $path, $wildcard, $text) | function SearchDirectory ($session, $path, $wildcard, $text) | ||
| { | { | ||
| Line 45: | Line 59: | ||
| # Search and print lines containing "text". | # Search and print lines containing "text". | ||
| # Use -Pattern instead of -SimpleMatch for regex search | # Use -Pattern instead of -SimpleMatch for regex search | ||
| - | Select-String -Path $tempPath -SimpleMatch $text | + | $matchInfo = Select-String -Path $tempPath -SimpleMatch $text |
| + | # Print the results | ||
| + | foreach ($match in $matchInfo) | ||
| + | { | ||
| + | Write-Host ($filePath + ":" + $match.LineNumber + ":" + $match.Line) | ||
| + | } | ||
| # Delete temporary local copy | # Delete temporary local copy | ||
| Remove-Item $tempPath | Remove-Item $tempPath | ||
| Line 73: | Line 92: | ||
| # Connect | # Connect | ||
| $session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
| - | |||
| - | $remotePath = "/home/user" | ||
| - | $wildcard = "*.txt" | ||
| - | $text = "blah" | ||
| # Start recursive search | # Start recursive search | ||
| - | SearchDirectory $session $remotePath $wildcard $text | + | SearchDirectory $session $path $wildcard $text |
| - | · | + | |
| } | } | ||
| finally | finally | ||