简体   繁体   中英

Group-Object folder by folder for deletion

I have a copyscript that copies QVD files from a QlikView structure, which was fine for one enviroment, now in the second one the file structure is much more complex and I can't use the same groupcode for grouping the files before deleting them.

Below you will see the code for case one that works.

I tried a few version with -Include , -Exclude , -Recurse , -Properties basename , directory, etc. without any progress.

$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

The folder structure is quite long, so one folder can contain twenty subfolders, and files in folder 1 and 18 in one chain under d:\\QlikView Storage\\PrivateData\\ can contain files with same name.

So when grouping objects, it has do it folder by folder only, so if folder 1 and subfolder contains objects with same name they still have to be grouped diferently.

In the picture below you see one ap as an example, and some subfolders in that contain .QVD files, then subfodlers to subfolders can contain .QVD files as well.

This should make it more clear.

在此处输入图片说明

I solved the problem this morning after an intrestring dream about it.

The code below fixes it.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM