catching remove-item in recursive delete

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

  • Author
    Posts
  • #6325

    by pakgwai at 2013-04-17 10:22:07

    From a unit test code, the the remove-item will fail to delete a read-only file, which i am able to catch it with "$?" this syntax. It returns a False value.
    When using the remove-item in a recursive delete, the "$?" is not returning False value. How to catch it in the recursive delete?

    [code2=powershell]foreach($childItem in (Get-ChildItem $dump_path -Filter $domain -exclude $keepfolder -Recurse))
    {
    if( ($childItem.PSIsContainer) -and (!(Get-ChildItem -Recurse -Path $childItem.FullName)))
    {
    Remove-Item $childItem.FullName -Confirm:$false -verbose

    if ($? -ne $true)
    {
    $sendMailFlag = $true
    }
    }
    }[/code2]

    Any ideas why in line 7 "$?" does not return a False value when a read-only file fails to delete.
    Yes, I have admin rights to delete files, but would like to catch read-only files and sends an email to notify.

    Thanks for the help.
    noob (1st post)

    by DonJ at 2013-04-17 10:26:17

    So... it's a bit tricky. I mean, the way *I* would normally do this is to get all the files and then feed them one at a time to Remove-Item, running it with -ErrorAction Stop so that I could trap each error as they occurred, but still be able to continue looping through the remaining files.

    by pakgwai at 2013-04-18 10:54:05

    What would be another method to get all the files and then feed them one at a time? isnt the Foreach loop is performing that when it is processing one file at time?

    by DonJ at 2013-04-18 11:07:54

    Yeah, so let me suggest:

    foreach($childItem in (Get-ChildItem $dump_path -Filter $domain -exclude $keepfolder -Recurse))
    {
    if( ($childItem.PSIsContainer) -and (!(Get-ChildItem -Recurse -Path $childItem.FullName)))
    {
    Try {
    Remove-Item $childItem.FullName -Confirm:$false -verbose -ErrorAction Stop -ErrorVariable x
    } Catch {
    # do something
    }
    }
    }

    In this arrangement, the Catch block will execute when an error occurs, and $x will contain the error. This is the basic model for error handling in PowerShell. Because you're already doing one file at a time, Remove-Item "exploding" due to an error will only affect that one file; it can then move on to the next one after handling the error. That's just the basic PowerShell error-handling approach.

    You could ALSO just proactively check the attributes of the file prior to deleting it. If it's read-only, don't try to delete it and send your e-mail.

    by pakgwai at 2013-04-19 10:14:29

    DonJ,
    i added that Try Catch and it did not seem to catch the error within the For Each loop. Yet when i added the Try Catch to a simple Remove-item, it worked.
    Back to square one when attempting to delete a file within a recursive delete, are there any other ways to catch the error. The "$?" and Try-Catch worked in a single file delete, yet it did not seem to work within the For Each.

    Any ideas,

    Thanks.

    by DonJ at 2013-04-19 10:21:04

    And you added -ErrorAction to the command? 'Cuz I'm running something real close to your code on my system, and it's working. You're not messing with $ErrorActionPreference in your script, are you?

    by pakgwai at 2013-04-19 13:13:42

    My mistake. The ForEach statement has 2 parts and i updated the deleted folder section only. I added the codes for the deleted file section and it worked.
    Thanks for your help. greatly appreciated.

    by DonJ at 2013-04-19 13:31:53

    Ah, good. Phew. You're welcome!

You must be logged in to reply to this topic.