CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-05-24
by 12kadir12

Original Post

Original - Posted on 2013-10-11
by deadlydog



            
Present in both answers; Present only in the new answer; Present only in the old answer;

From this [answer][1] , you can use this script below. If you want to get detailed information about powershell flags go this [link][2]
$limit = (Get-Date).AddDays(-1) $path = "C:\Some\Path" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # Delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
When adding days, you can also add hours minutes etc. for example *AddHours(-1)*
[1]: https://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell [2]: https://theochem.ru.nl/~pwormer/teachmat/PS_cheat_sheet.html
The given answers will only delete files (which admittedly is what is in the title of this post), but here's some code that will first delete all of the files older than 15 days, and then recursively delete any empty directories that may have been left behind. My code also uses the `-Force` option to delete hidden and read-only files as well. Also, I chose to not use aliases as the OP is new to PowerShell and may not understand what `gci`, `?`, `%`, etc. are.
$limit = (Get-Date).AddDays(-15) $path = "C:\Some\Path" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # Delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
And of course if you want to see what files/folders will be deleted before actually deleting them, you can just add the `-WhatIf` switch to the `Remove-Item` cmdlet call at the end of both lines.
If you only want to delete files that haven't been updated in 15 days, vs. created 15 days ago, then you can use `$_.LastWriteTime` instead of `$_.CreationTime`.
The code shown here is PowerShell v2.0 compatible, but I also show this code and the faster PowerShell v3.0 code as [handy reusable functions on my blog][1].

[1]: http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/

        
Present in both answers; Present only in the new answer; Present only in the old answer;