[英]Search directory for unique path and patterns
我正在嘗試搜索目錄結構和文件,以查找其中存在模式的所有實例。 比我想要將該文件位置記錄在日志文件中,我可以稍后查看。 我看了各種文章,但沒有找到類似的例子。 已審核的帖子包括:
這是我用來在文件夾結構中重新使用的代碼:
#Set variables for paths
$Results = "C:\Results"
$Source = "C:\Test\*"
$Destination = "C:\MyTest\"
#Create file name for each report with date and time of run
$ReportDate = (Get-Date).ToString("dd-MM-yyyy-hh-mm-ss")
$CustomPattern = Read-Host 'What pattern are you looing for?'
$CustomPatternLog = New-Item -itemType File -Path C:\Results -Name $("CustomerPattern_" + $ReportDate + ".txt")
$CustomPattern = foreach ($file in Get-ChildItem -Path $Destination -Recurse | Select-String -pattern $CustomPattern | Select-Object -Unique Path) {$file.path}
$CustomPattern > "$($Results)\$($CustomPatternLog)"
但是,此代碼返回以下錯誤:
Get-ChildItem:指定的路徑,文件名或兩者都太長。 完全限定的文件名必須少於260個字符,目錄名稱必須少於248個字符。 在第19行:char:36 + $ CustomPattern = foreach(Get-ChildItem中的$ file -Path $ Destination -Recurse | S ... + ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ReadError:(C:\\ Test \\ Mor ... ofiles \\ Customer:St ring)[Get-ChildItem], PathTooLongException + FullyQualifiedErrorId:DirIOError,Microsoft.PowerShell.Commands.GetChil dItemCommand
您是否有更好的方法進行相同的操作?
取代這個
$CustomPatternLog = New-Item -itemType File -Path C:\Results -Name $("CustomerPattern_" + $ReportDate + ".txt")
$CustomPattern = foreach ($file in Get-ChildItem -Path $Destination -Recurse | Select-String -pattern $CustomPattern | Select-Object -Unique Path) {$file.path}
$CustomPattern > "$($Results)\$($CustomPatternLog)"
有了這個
$files = Get-ChildItem -Path $Destination -Recurse
#in case you would need the path replace FullName with PsParentPath
$result = ($files | ?{$_.name -like "*$CustomPattern*"}).FullName
$result | out-file ($CustomPattern + "_" + $ReportDate + ".txt")
而且由於它的外殼,您可以用一根內襯做同樣的事情
(Get-ChildItem -Path $Destination -Recurse | ?{$_.name -like "*$CustomPattern*"}).FullName | out-file ($CustomPattern + "_" + $ReportDate + ".txt")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.