Python 2.7 查找子项脚本(从powershell转换)

Python 2.7 查找子项脚本(从powershell转换),python-2.7,file,powershell,finder,Python 2.7,File,Powershell,Finder,我必须在相当大的文件windows文件共享中搜索从我的公司发送和接收的传真。我是Python2.7新手,但我确实发现了这个powershell查找子项脚本,它的工作方式非常简单 脚本允许我输入搜索路径,然后搜索关键字或短语。(当我搜索一个可能包含400k到100万个传真文件的文件夹时,这非常有用,这些文件的文件名中都有发送者/接收者编号 但是脚本运行缓慢,我不太喜欢powershell。有人能帮我转换这个吗 谢谢 #> "`n" write-Host "------------------

我必须在相当大的文件windows文件共享中搜索从我的公司发送和接收的传真。我是Python2.7新手,但我确实发现了这个powershell查找子项脚本,它的工作方式非常简单

脚本允许我输入搜索路径,然后搜索关键字或短语。(当我搜索一个可能包含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

在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

它运行得很慢,因为您没有在
获取ChildItem
上使用
-Filter
-File
。学习这句咒语:左过滤,右格式化。@不可纠正1但有些人没有使用最新的PowerShell。因此他们可能无法在命令中使用
-Filter
-File
。@ShawnEsterman
-Filter
从v1.0开始就存在。
-File
是在v3.0中引入的。我想我需要更熟悉python语法。每次我在python中运行时,都会遇到语法错误。@bdub2841如果你不是为了实现交叉兼容性,那么在
py
中几乎没有理由这样做