如何更改 WinSCP 脚本以递归搜索文本以仅打印第一个匹配项的文件夹并退出
How to alter WinSCP script for recursively searching for text to print just a folder of the first match and exit
我希望能够在远程 FTP 服务器上的所有文件夹中查找文本字符串。找到后,我需要知道包含文本文档的文件夹的名称,但 而不是 文件的名称。
如何更改 "Action on match" 以使此脚本 (WinSCP Extension Search recursively for text in remote directory / Grep files over SFTP/FTP protocol) 运行 静默(不在终端中显示任何内容) ),一旦找到匹配项,只需停止并仅显示文件夹的名称(包含带有文本字符串的文件)?也可以用红色显示结果吗?这是脚本的 "Action on match" 部分(我试图包括整个内容但由于某种原因无法做到)。
我可以借助最新的 WinSCP 自定义 搜索文本 按钮(请参阅下面的 .ps1 脚本来实现此搜索功能)。然而,搜索并没有在匹配时停止,而是一直持续到最后一个文件夹。更糟糕的是,为了找到结果,我需要一直向上滚动并检查每个条目。只有其中一个会列出我的文件夹的名称,因此这是一个很长的过程。
{
# Action on match
# Modify the code below if you want to do another task with
# matching files, instead of grepping their contents
Write-Host ("File {0} matches mask, searching contents..." -f $fileInfo.FullName)
$tempPath = (Join-Path $env:temp $fileInfo.Name)
# Download file to temporary directory
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$transferResult = $session.GetFiles($sourcePath, $tempPath)
# Did the download succeeded?
if (!$transferResult.IsSuccess)
{
# 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
}
}
{
# Action on match
# Modify the code below if you want to do another task with
# matching files, instead of grepping their contents
$tempPath = (Join-Path $env:temp $fileInfo.Name)
# Download file to temporary directory
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$transferResult = $session.GetFiles($sourcePath, $tempPath)
# Did the download succeeded?
if (!$transferResult.IsSuccess)
{
# Print error (but continue with other files)
Write-Host $transferResult.Failures[0].Message
}
else
{
# Use -Pattern instead of -SimpleMatch for regex search
$matchInfo = Select-String -Path $tempPath -SimpleMatch $text
Remove-Item $tempPath
if ($matchInfo)
{
# Print a path to the file's folder and exit
Write-Host ($fileInfo.FullName -replace "/[^/]*$")
exit
}
}
}
我希望能够在远程 FTP 服务器上的所有文件夹中查找文本字符串。找到后,我需要知道包含文本文档的文件夹的名称,但 而不是 文件的名称。
如何更改 "Action on match" 以使此脚本 (WinSCP Extension Search recursively for text in remote directory / Grep files over SFTP/FTP protocol) 运行 静默(不在终端中显示任何内容) ),一旦找到匹配项,只需停止并仅显示文件夹的名称(包含带有文本字符串的文件)?也可以用红色显示结果吗?这是脚本的 "Action on match" 部分(我试图包括整个内容但由于某种原因无法做到)。
我可以借助最新的 WinSCP 自定义 搜索文本 按钮(请参阅下面的 .ps1 脚本来实现此搜索功能)。然而,搜索并没有在匹配时停止,而是一直持续到最后一个文件夹。更糟糕的是,为了找到结果,我需要一直向上滚动并检查每个条目。只有其中一个会列出我的文件夹的名称,因此这是一个很长的过程。
{
# Action on match
# Modify the code below if you want to do another task with
# matching files, instead of grepping their contents
Write-Host ("File {0} matches mask, searching contents..." -f $fileInfo.FullName)
$tempPath = (Join-Path $env:temp $fileInfo.Name)
# Download file to temporary directory
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$transferResult = $session.GetFiles($sourcePath, $tempPath)
# Did the download succeeded?
if (!$transferResult.IsSuccess)
{
# 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
}
}
{
# Action on match
# Modify the code below if you want to do another task with
# matching files, instead of grepping their contents
$tempPath = (Join-Path $env:temp $fileInfo.Name)
# Download file to temporary directory
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$transferResult = $session.GetFiles($sourcePath, $tempPath)
# Did the download succeeded?
if (!$transferResult.IsSuccess)
{
# Print error (but continue with other files)
Write-Host $transferResult.Failures[0].Message
}
else
{
# Use -Pattern instead of -SimpleMatch for regex search
$matchInfo = Select-String -Path $tempPath -SimpleMatch $text
Remove-Item $tempPath
if ($matchInfo)
{
# Print a path to the file's folder and exit
Write-Host ($fileInfo.FullName -replace "/[^/]*$")
exit
}
}
}