1
I am with script to remove file leaving only the last ones defined on time
Script:
$Now = Get-Date
$Days = 30
$TargetFolder = "C:\LOG"
$Extension = "*.*"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deletando arquivo $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "Nao ha arquivos a serem excluidos!" -foregroundcolor "Green"
}
}
Turns out that on my computer (Windows 7 64bit) works perfectly already on the server (Win2008 R2 64bit) shows the following error:
Confirm The item at Microsoft.PowerShell.Core Filesystem::E: LOG Maplink.Service.Monitor.Hourlytasks has Children and the Recurse Parameter was not specified. If you continue, all Children will be Removed with the item. Are you sure you wan to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Is it an error or a confirmation message? 'responding'
Y
orA
it removes the files?– rray
To stop receiving the confirmation request, pass
-recurse
at theRemove-Item
. However you should check why and that you have directories in the files in the file collection.– Omni
When confirming Y some files are deleted others are not, I already simplified the code to: $Dir = "E: LOG" $Day = 30 $Date = (get-date) - (new-timespan -day $Day) Get-Childitem $Dir -recurse | Where {$_. Lastwritetime -le $Date} | del error persists.
– Jucimário Santana
When adding -recurse instead of Remove-Item, it presents the following error: Missing Expression after unary Operator '-'. At D: cleanlog.ps1:21 char:7 + - <<<<<recurse $File.Fullnamo | out-null + Categoryinfo : Parseerror: (-:String) [], Parse Exception
– Jucimário Santana
@Regarding the error, it seems that the
-
is not being recognized. Have you tried using-force
to ensure that files are deleted?– Omni
Yes, and it’s the same mistake.
– Jucimário Santana
Do you have permissions to delete these files? If you run powershell session as admin the error continues?
– Omni
Yes, when I put a specific folder without having another folder inside it works normally.
– Jucimário Santana
I took a test as
remove-item $File.Name -Recurse -force | out-null
in the structure:C:\log\pasta1\pasta2
it erased all the files but kept pasta1 and pasta2, in this test I removed this part too| Where {$_.LastWriteTime....
– rray
I confirm the results of @lost, tested in Win7 and Server2008 R2 and deleted as should.
– Omni
I did what you reported and gave this other error: Remove-Item : Cannot find path’D: Utilities Task v4.2' because it does not exist. At D: Utilities Task Cleanlog.ps1:21 char:17 + Remove-Item <<< $File.Name -Recurse -force | out-null + Categoryinfo : Objectnotfound: (D: Utilities Task v4.2:String) [Remove-Item] + Fullyderrorid : Pathnotfound,Microsoft.PowerShell.Commands.Removeitemcommand
– Jucimário Santana
@Omni, I tested in Win8 with already saved ps1 file. The confirmation prompt did not appear.
– rray
I was able to solve, a parameter was missing: Solution replacing Where by the script below: Where-Object {!$.Psiscontainer -and $.Lastwritetime -le $Date} Vlw Personal..
– Jucimário Santana
@Jucimáriosantana, put this as an answer and explain better, it can help more people with the same problem.
– rray