【问题标题】:find child item script (conversion from powershell)查找子项脚本(从 powershell 转换)
【发布时间】:2017-10-31 16:09:41
【问题描述】:

我必须在相当大的文件窗口文件共享中搜索从我公司发送和接收的传真。我是 Python 2.7 的新手,但我确实找到了这个 powershell find-child-item 脚本,它工作得很好。

该脚本允许我输入搜索路径,然后搜索关键字或短语。 (当我搜索可能包含 400k 到 100 万个传真文件的文件夹时,这非常有用,这些文件的文件名中都有发件人/收件人编号。

但脚本运行缓慢,我不太关心 powershell。谁能帮我转换一下?

谢谢!

#>
"`n"
write-Host "---------------------------------------------" -ForegroundColor Yellow
$filePath = Read-Host "Please Enter File Path to Search"
write-Host "---------------------------------------------" -ForegroundColor Green
$fileName = Read-Host "Please Enter File Name to Search"
write-Host "---------------------------------------------" -ForegroundColor Yellow
"`n"

Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue |
    Where-Object { ($_.PSIsContainer -eq $false) -and
                   ( $_.Name -like "*$fileName*") } |
    Select-Object Name,Directory |
    Format-Table -AutoSize *

write-Host "------------END of Result--------------------" -ForegroundColor Magenta

pause

# end of the script

【问题讨论】:

  • 它运行缓慢,因为您没有在Get-ChildItem 上使用-Filter-File。学习口头禅:向左过滤,向右格式化。
  • @TheIncorrigible1 虽然有些人没有使用最新的 PowerShell。所以他们可能无法在他们的命令中使用-Filter-File
  • @ShawnEsterman -Filter 从 v1.0 开始就存在了。 -File 在 v3.0 中引入。

标签: python-2.7 file powershell finder


【解决方案1】:

在 PowerShell 中进行了重大改进:

#Requires -Version 3
Write-Host "---------------------------------------------" -ForegroundColor 'Yellow'
$filePath = Read-Host "Please Enter File Path to Search"
Write-Host "---------------------------------------------" -ForegroundColor 'Green'
$fileName = Read-Host "Please Enter File Name to Search"
Write-Host "---------------------------------------------`r`n" -ForegroundColor 'Yellow'

$Params = @{
    Path = $filePath
    Filter = "*$fileName*"
    File = $True
    Recurse = $True
    Force = $True #not sure why you included this. It forces finding hidden folders
    ErrorAction = 'SilentlyContinue'
}
Get-ChildItem @Params |
    Select-Object -Property @('Name','Directory') |
    Format-Table -Property '*' -AutoSize

Write-Host '------------END of Result--------------------' -ForegroundColor 'Magenta'

Pause

【讨论】:

  • 我想我需要更熟悉 python 语法。每次我在 python 中运行时都会出现语法错误。
  • @bdub2841 如果您的目标不是交叉兼容性,py 没有理由这样做
猜你喜欢
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多