我们每天都会收到许多客户的zipfile。文件名包含以下内容:
data_clientname_timestamp.zip
其中"数据"始终是相同的文字,"客户名称"可以是任何东西,"时间戳"是文件创建日期。
文件始终位于同一目录中。客户端名称总是事先知道,所以我知道应该接收哪些文件。
脚本应该执行以下操作:
到目前为止我所拥有的:
$folder='C:\data'
Get-ChildItem $folder -recurse -include @("*.zip") |
Where-Object {($_.CreationTime -gt (Get-Date).Date )} | select name | out-file $folder\result.txt
但是如何检查文件中是否有丢失的文件?
编辑: TESTDATA:
$Timestamp = (Get-Date).tostring(“yyyyMMddhhmmss”)
New-Item c:\Data -type Directory
New-Item c:\Data\Data_client1_$Timestamp.zip -type file
New-Item c:\Data\Data_client2_$Timestamp.zip -type file
New-Item c:\Data\Data_client3_$Timestamp.zip -type file
New-Item c:\Data\Data_client5_$Timestamp.zip -type file
New-Item c:\Data\Data_client6_$Timestamp.zip -type file
New-Item c:\Data\Data_client7_$Timestamp.zip -type file
exit
脚本:
$folder='C:\Data'
$clients = @("client1", "client2", "client3", "client4", "client5", "client6", "client7")
$files = Get-ChildItem $folder -recurse -include @("*.zip") |
Where-Object {($_.CreationTime -gt (Get-Date).Date )}
$files | Select-Object Name | Out-File $folder\result.txt
$files | Where-Object { ($_.Name -replace '.+?_([^_]+).*', '$1') -notin $clients} | Out-File $folder\result2.txt
答案 0 :(得分:3)
首先定义您期望的客户列表:
$clients = @("client1", "client2")
然后检索所有zip文件并将其保存到变量:
$files = Get-ChildItem $folder -recurse -include @("*.zip") |
Where-Object {($_.CreationTime -gt (Get-Date).Date )}
将现有文件导出到result.txt:
$files | Select-Object Name | Out-File $folder\result.txt
现在,您可以使用带有抓取客户端名称的正则表达式的Where-Object
cmdlet确定每个缺少的客户端:
$fileClients = $files | ForEach-Object { ($_.Name -replace '.+?_([^_]+).*', '$1') }
Compare-Object $clients $fileClients | select -ExpandProperty InputObject | Out-File $folder\result2.txt
答案 1 :(得分:2)
您需要在某处(例如名为clients.csv的CSV文件中)列出您的客户端列表,然后您可以遍历该列表以检查是否为每个客户端找到了文件。例如:
codova.file.dataDirectory