我想读取一个文本文件,并将文本文件中的内容数据与文件名进行匹配,然后记录输出。 到目前为止,这是我目前无法使用的内容:
$Source = "S:\*.*"
$Missing_Files = "C:\Temp\Powershell Scripts\Missing Files Script\Results\"
foreach ($ID in Get-Content 'C:\Temp\Powershell Scripts\Missing Files
Script\IDS.txt') {
$ID
$Count = 0
Get-Childitem $Source -ErrorAction SilentlyContinue | where {$_.name -
notmatch $ID} | foreach {
}
if ($Count -eq 0) {
Write-Host $_.name "No File Found"
Write-Output $ID "No File Found" | Out-File $Missing_Files\$(Get-Date -f
yyyy-MM-dd)_Missing_Files_Results.txt -append
}
}
答案 0 :(得分:0)
如果我正确理解了这个问题,则要搜索文本文件中未列出的文件
C:\Temp\Powershell Scripts\Missing Files Script\IDS.txt
。
任何未找到但未在该文件中列出的文件都被视为无关文件,如果该脚本没有找到其他文件,则希望脚本创建(或附加到)结果文件。
在这种情况下,这应该可以工作:
$SourcePath = "S:\"
$OutputPath = "C:\Temp\Powershell Scripts\Missing Files Script\Results"
$OutputFile = Join-Path -Path $OutputPath -ChildPath ('{0:yyyy-MM-dd}_Missing_Files_Results.txt' -f (Get-Date))
$ListedFiles = Get-Content 'C:\Temp\Powershell Scripts\Missing Files Script\IDS.txt' | Sort-Object -Unique
# get an array of filenames that are NOT listed in 'C:\Temp\Powershell Scripts\Missing Files Script\IDS.txt'
$files = @(Get-Childitem $SourcePath -File -ErrorAction SilentlyContinue |
Where-Object { $ListedFiles -notcontains $_.Name })
if ($files.Count) {
# we found files that are NOT listed
Write-Host "$($file.Count) Files found that are not listed in '$ListedFiles'"
Add-Content -Path $OutputFile -Value ($files -join [Environment]::NewLine)
}
else {
# no files found
Write-Host "No files found"
Add-Content -Path $OutputFile -Value "No file found."
}
希望有帮助