Building pauses in-between objects in commands

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5972

    by i255d at 2012-12-03 13:56:01

    I am runnin a script that is deleting backup over 4 days old. The files are so large that it is casuing problems on my storage when it starts to delete to many at once. I want to build a pause into my script inbetween objects ($_.) as they are piped through the commands. I hope I am explainin this well.

    $CURRENTDATE = Get-Date
    $TODATE = $CURRENTDATE.AddDays(-4)
    get-childitem "\\gha01sdp333\BigBackups\" *.bak -recurse | where {$_.LastWriteTime -le "$TODATE"} | Remove-Item
    get-childitem "\\gha01sdp333\BigBackups\" *.trn -recurse | where {$_.LastWriteTime -le "$TODATE"} | Remove-Item

    Each *.bak file is hundreds of GB, so I have been asked to find a way to create a delay between each Item before or after it is put in the "where {$_.LastWriteTime -le "$TODATE"}"

    by MattG at 2012-12-03 14:50:33

    You might consider sleeping in between each deletion:
    Get-ChildItem "\\gha01sdp333\BigBackups\" *.bak -Recurse
    | Where-Object {$_.LastWriteTime -le "$TODATE"}
    | ForEach-Object { Remove-Item $_; Start-Sleep 10 }
    This will cause PowerShell to sleep for ten seconds after each call to Remove-Item.

    by i255d at 2012-12-03 20:23:55

    Thanks for the big help! The only problem is that the path does't get passed through so it fails when it tries to delete it.

    Get-ChildItem -Path \\192.168.1.67\Users\Test -Recurse
    | Where-Object {$_.LastWriteTime -le $ToDate}
    | ForEach-Object { Remove-Item -Path \\192.168.1.67\Users\Test\$_; Start-Sleep -Seconds 10 }

    This seems to work in my testing.

    by nohandle at 2012-12-04 05:10:24

    Hi, responded on your question on powershell com forum also.
    double piping the output with where and foreach is unnecessary.
    try this: Get-ChildItem -Path \\192.168.1.67\Users\Test -Recurse |
    ForEach-Object {
    if ($_.LastWriteTime -le $ToDate)
    {
    Remove-Item -Path $_.fullname -Recurse -Force
    Start-Sleep -Seconds 10
    }
    }

    by nohandle at 2012-12-04 05:14:21

    Just for the record,
    accomplishing the task just by the where-object filter is possible but not recommended solution. The purpose of the command is obfuscated by the unusual usage of the filtering condition.
    Get-childItem . *.txt | where{((-not(sleep 1)) -and ($_.LastWriteTime -le (get-date).adddays(-10)))}

    Explaining how this works on another forum I realized that if the conditions are switched (sleep after the and, not before) the first condition should evaluate to false on files that are not meant to be deleted and the whole condition should therefore evaluate to false without running the sleep command. Hence not waiting on every file but just on files to be deleted. Have to test it though.

    Dips on writing an article about this 😀

    by i255d at 2012-12-04 11:14:02

    Final answer – provided by nohandle

    Get-ChildItem -Path '\\192.168.1.1\QCBACKUPS' -Include *.txt,*.tst -Recurse |
    ForEach-Object{
    if($_.LastWriteTime -le (Get-Date).adddays(-4))
    {
    Remove-Item -Path $_.fullname -Recurse -Force
    Start-Sleep -Seconds 60
    }
    }

You must be logged in to reply to this topic.