Note that there are some explanatory texts on larger screens.

plurals
  1. PORetain first file for each date
    primarykey
    data
    text
    <p>I want to keep 5 last generated file in my directory, retaining only the first file of each date, because inside my subfolders, I have generated files multiple times in the same day.</p> <p>I have multiple folder with subfolder named "old"</p> <pre><code>C:\test\folder1\old C:\test\toto\old ... </code></pre> <p>So for example in my subfolder I have this : </p> <pre><code> Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 07/06/2013 12:01 231248950 geofi.ry.7.0.0.159940.zip -a--- 07/06/2013 12:33 231248506 geofi.ry.7.0.0.159950.zip -a--- 07/06/2013 14:51 231248957 geofi.ry.7.0.0.159962.zip -a--- 17/06/2013 19:47 231248860 geofi.ry.7.0.0.160871.zip -a--- 18/06/2013 11:03 231248480 geofi.ry.7.0.0.160907.zip -a--- 23/06/2013 07:30 231250266 geofi.ry.7.0.0.161571.zip -a--- 23/06/2013 21:30 231250266 geofi.ry.7.0.0.161563.zip -a--- 04/07/2013 00:42 231249910 geofi.ry.7.0.0.162695.zip -a--- 04/07/2013 16:12 231249910 geofi.ry.7.0.0.162647.zip -a--- 08/07/2013 16:10 231250481 geofi.ry.7.0.0.163046.zip -a--- 10/07/2013 08:40 231250476 geofi.ry.7.0.0.163378.zip -a--- 17/07/2013 10:22 231249418 geofi.ry.7.0.0.164001.zip </code></pre> <p>I want to keep theses files : </p> <pre><code>-a--- 17/07/2013 10:22 231249418 geofi.ry.7.0.0.164001.zip -a--- 10/07/2013 08:40 231250476 geofi.ry.7.0.0.163378.zip -a--- 08/07/2013 16:10 231250481 geofi.ry.7.0.0.163046.zip -a--- 04/07/2013 00:42 231249910 geofi.ry.7.0.0.162695.zip (on this date i have two files, I want to keep the first generated at this date 00:42). -a--- 23/06/2013 07:30 231250266 geofi.ry.7.0.0.161571.zip (on this date i have two files, I want to keep the first generated at this date 07:30). </code></pre> <p>This is a draft but I'm stuck, how can I compare time? </p> <pre><code>$Days = "5" $TargetFolder = "C:\test\" $Extension = "*.zip" $Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | sort-object {$_.LastWriteTime} -Descending | ? { $_.fullname -match "old" } | select-object -First 1 $most_recent_date = $files.LastWriteTime $LastWrite = $most_recent_date.Add(-$Days) $Files2 = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} | ? { $_.fullname -match "old" } foreach ($File in $Files2) { if ($File -ne $NULL) { Remove-Item $File.FullName | out-null } else { Write-Host "No more files to delete!" } } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload