组对象文件夹(逐个文件夹)用于删除

时间:2019-07-02 11:44:47

标签: powershell

我有一个复制脚本,它从QlikView结构复制QVD文件,这对于一种环境来说是很好的,现在在第二种环境中,文件结构要复杂得多,在删除之前,我无法使用相同的组码对文件进行分组他们。

在下面,您将看到有效的情况一的代码。

我尝试了一些-Include-Exclude-Recurse-Properties basename,目录等版本,但没有任何进展。

$global:currenttime = Set-PSBreakpoint -Variable currenttime -Mode Read -Action { $global:currenttime= Get-Date }
$a = Get-Date
$a.ToUniversalTime()
$date = Get-Date -f yyyy-MM-dd
$LocalPath = "d:\QlikView Storage\PrivateData\"

Get-ChildItem *.qvd -Path "D:\QlikView Storage\PrivateData\" -File -Recurse |
    Where-Object { $_.LastWriteTime -ge (Get-Date).AddDays(-1) } |
    Select-Object Name, @{Name="KBytes";Expression={"{0:N0}" -f ($_.Length / 1KB)}} |
    Export-Csv C:\temp\size\$date.qvdsize1.csv

"Kopiering av folders startad $global:currenttime" | Out-File F:\Log\$date.log.txt -Append

Get-ChildItem -Directory $localpath -Recurse -Verbose | Where {
    $_.LastWriteTime -gt (Get-Date).AddDays(-1)
} | foreach {
    $split = $_.FullName -split '\\'
    $DestFile = $split[1..($split.Length - 1)] -join '\'
    $DestFile = "F:\$DestFile"
    if (!(Test-Path -Path $DestFile)) {  
        Copy-Item -Path $_.Fullname -Destination $destfile -Filter {psicontainer} -Recurse -Verbose 4>&1 |
            Out-File -Append F:\Log\$date.log.txt
    }
}

"Kopiering av folders slutförd $global:currenttime" | Out-File F:\Log\$date.log.txt -Append
"Kopiering av filer påbörjad $global:currenttime" | Out-File F:\Log\$date.log.txt -Append

Get-ChildItem -File  "*.qvd" $localpath -Recurse -Verbose | Where {
    $_.LastWriteTime -gt (Get-Date).AddDays(-1)
} | foreach {
    $split = $_.FullName -split '\\'
    $DestFile = $split[1..($split.Length - 1)] -join '\'
    $DestFile = "F:\$DestFile"
    if (!(Test-Path -Path $DestFile)) {  
        Copy-Item -Path $_.Fullname -Include "*.qvd" -Destination "$destfile.$(Get-Date -f yyyy-MM-dd)" -Filter {PSIsContainer} -Recurse -Verbose 4>&1 |
            Out-File -Append F:\Log\$date.log.txt
    }
}
"Kopiering av filer avslutad  $global:currenttime" | Out-File F:\Log\$date.log.txt -Append
 "Börjar borttagninvg av äldre generation $global:currenttime" | Out-File F:\Log\$date.log.txt -Append

$Groups = Get-ChildItem -Path "F:\qlikview Storage\privatedata\" -Recurse |
          Group-Object -Property BaseName |
          Where-Object {$_.Count -gt 2}
foreach ($g in $Groups) {
    $g.Group |
        sort LastWriteTime -Descending |
        select -Skip 2 |
        foreach {del $_.FullName -Force -Verbose 4>&1} |
        Out-File -Append F:\Log\$date.log.txt
}
"Klar med borttagninvg av äldre generation $global:currenttime" | Out-File F:\Log\$date.log.txt -Append

文件夹结构相当长,因此一个文件夹可以包含20个子文件夹,并且d:\ QlikView Storage \ PrivateData \下的链1和18中的文件可以包含相同名称的文件。

因此,在对对象进行分组时,它仅按文件夹进行处理,因此,如果文件夹1和子文件夹包含名称相同的对象,则仍必须将它们进行不同的分组。

在下面的图片中,您以一个ap为例,其中一些子文件夹包含.QVD文件,然后子文件夹的子文件夹也可以包含.QVD文件。

这应该使它更清楚。

enter image description here

1 个答案:

答案 0 :(得分:0)

今天早晨,我梦dream以求地解决了这个问题。

下面的代码将其修复。

$Groups = Get-ChildItem -Path "d:\qlikview Storage\privatedata\" -Recurse  |
        Where-Object { -not $_.PSIsContainer } |
        Group-Object -Property basename, directory |
        Where-Object {$_.Count -gt 2}
foreach ($g in $Groups) {
    $g.Group |
        sort LastWriteTime -Descending |
        select -Skip 2 |
        foreach {del $_.FullName -Force -Verbose 4>&1} |
        Out-File -Append D:\Log\$date.log.txt
相关问题