我有一个文本文件目录,我正在尝试处理,排除,复制和组合。我试图将最后一天的所有文本文件复制到临时文件夹,不包括基于文本文件中找到的唯一字符串的文本文件。我无法想出一个好方法来排除文本文件并将正确的文本文件复制到临时目录。
$excludematch = "unique: string","unique: string2"
foreach ($i in Get-ChildItem $dir\*.txt) {
if ($i.CreationTime -gt ($(Get-Date).AddHours(-24))) {
if (Get-Content $i.FullName | Select-String -Pattern $stringmatch){ ...do something with matched files... }}}
#combine all text files in the temp folder into a text file named for that day
Get-Content -path $temp_copy\*.txt | Set-Content -path $output\$(get-date -f MM-dd-yyyy).txt
这就是我想要获取要排除的文件列表的内容。我不想删除文件,但我不希望它们进入组合文本文件。
提前致谢。
答案 0 :(得分:0)
如果您希望将过滤后的txt文件直接转发到当天的一个文件,您可以这样做:
$excludematch = "unique: string","unique: string2"
foreach ($i in Get-ChildItem $dir\*.txt) {
if ($i.CreationTime -gt ($(Get-Date).AddHours(-24))) {
if (!(Get-Content $i.FullName | Select-String -Pattern $excludematch)){
Get-Content $i.FullName | Out-File -Append -FilePath ".\$(get-date -f MM-dd-yyyy).txt"
}}}
如果您想在创建最终的.txt之前执行额外步骤并将所有已过滤的文件放入单独的文件夹中,您可以这样做:
$excludematch = "unique: string","unique: string2"
foreach ($i in Get-ChildItem $dir\*.txt) {
if ($i.CreationTime -gt ($(Get-Date).AddHours(-24))) {
if (!(Get-Content $i.FullName | Select-String -Pattern $excludematch)){
Copy-Item $i -Destination $tmpPath
}}}
foreach ($item in Get-ChildItem $tmpPath){
Get-Content $item.FullName | Out-File -Append -FilePath "D:\tmp\$(get-date -f MM-dd-yyyy).txt"
}