Removing files for period of time

Asked

Viewed 1,438 times

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 or A it removes the files?

  • To stop receiving the confirmation request, pass -recurse at the Remove-Item. However you should check why and that you have directories in the files in the file collection.

  • 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.

  • 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

  • @Regarding the error, it seems that the - is not being recognized. Have you tried using -force to ensure that files are deleted?

  • Yes, and it’s the same mistake.

  • Do you have permissions to delete these files? If you run powershell session as admin the error continues?

  • 1

    Yes, when I put a specific folder without having another folder inside it works normally.

  • 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....

  • I confirm the results of @lost, tested in Win7 and Server2008 R2 and deleted as should.

  • 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

  • @Omni, I tested in Win8 with already saved ps1 file. The confirmation prompt did not appear.

  • 1

    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áriosantana, put this as an answer and explain better, it can help more people with the same problem.

Show 9 more comments

1 answer

1

I was able to solve, a parameter was missing: Solution replacing the Where by the script below:

Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -le $Date}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.