locked
Assist with stopping script deleting parent folder along with it's children... RRS feed

  • Question

  • Hi everyone.

    I'll stick my hand straight up and state that I've only dabbled in PowerShell so far.

    Purpose of script: Delete all child folders and contents in a directory that are older than 14 days.

    Script Details: I found the original script online here, so all credit to the original creator. I then modified as per the comment on that page and then also added in the two lines to stop it deleting any child folders named "webserver" and "installer".

    Problem: The script deletes the child folders, leaving any with the excluded names, but then on a second run deletes the parent folder, ie DelTest - this is not desired.

    The Script:

    $Now = Get-Date
    $Days = "14"
    $TargetFolder = "C:\Scratch\DelTest"
    $LastWrite = $Now.AddDays(-$Days)

    $Folders = Get-ChildItem -path $TargetFolder |
    Where {$_.psIsContainer -eq $true} |
    Where {$_.LastWriteTime -le "$LastWrite"} |
    Where {$_.name -ne "webserver"} |
    Where {$_.name -ne "installer"}

        foreach ($Folder in $Folders)
        {
        $Folder = $TargetFolder + "\" + $Folder
        write-host "Deleting $Folder" -foregroundcolor "Red"
        Remove-Item $Folder -recurse -Confirm:$false
        }

    So, any ideas?

    Tuesday, November 22, 2011 7:39 PM

Answers

  • Ok, so I did some more testing and information digging.

    I found that one the second run, due to the line added from the comments of

    $Folder = $TargetFolder + "\" + $Folder

    $Folder was null so this then set $Folder to equal the $TargetFolder and therefore delete it.

    Through some digging I found that I could reference $Folder.FullName thus removing the need for that problematic line. I added a throw in to exit when #Folder equals null as well as changing the exclusions to variables. Also renamed some variables for personal taste. So the final code if anyone looks at this thread in years to come:

    $Today = Get-Date
    $DaysToKeep = "14"
    $TargetFolder = "E:\Program Files\Research In Motion\Blackberry Enterprise Server\Logs"
    $LastWrite = $Today.AddDays(-$DaysToKeep)
    $FldrExclusion1 = "webserver"
    $FldrExclusion2 = "installer"

    $Folders = Get-ChildItem -path $TargetFolder |
    Where {$_.psIsContainer -eq $true} |
    Where {$_.LastWriteTime -le "$LastWrite"} |
    Where {$_.name -ne $FldrExclusion1} |
    Where {$_.name -ne $FldrExclusion2}


        foreach ($Folder in $Folders)
        {
            if ($Folder -eq $null) {
                throw "No further folders to delete"
            }
            else {
                write-host "Deleting $Folder.FullName" -foregroundcolor "Red"
                Remove-Item $Folder.FullName -recurse -Confirm:$false
            }
        }

    Note: this was created to clear out the BES log folders hence the "webserver" and "installer" folder exclusions, these can obviously be changed / added too if you want more or different.







    • Edited by Greg Steer Wednesday, November 23, 2011 10:43 PM
    • Marked as answer by Greg Steer Wednesday, November 23, 2011 11:40 PM
    Wednesday, November 23, 2011 10:30 PM
  • One reason it would delete the $targetFolder itself is if the $folder variable is empty.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Marked as answer by Greg Steer Wednesday, November 23, 2011 11:40 PM
    Tuesday, November 22, 2011 8:14 PM

All replies

  • Suggestion: using debugging techniques, determine the value of $folders on the second run. If it is $null or empty, then the foreach loop shouldn't execute, but it's worth checking.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Tuesday, November 22, 2011 8:11 PM
  • One reason it would delete the $targetFolder itself is if the $folder variable is empty.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Marked as answer by Greg Steer Wednesday, November 23, 2011 11:40 PM
    Tuesday, November 22, 2011 8:14 PM
  • One reason it would delete the $targetFolder itself is if the $folder variable is empty.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


    I'll take a look later today Bigteddy

    Cheers

    Greg

    Wednesday, November 23, 2011 5:47 PM
  • Ok, so I did some more testing and information digging.

    I found that one the second run, due to the line added from the comments of

    $Folder = $TargetFolder + "\" + $Folder

    $Folder was null so this then set $Folder to equal the $TargetFolder and therefore delete it.

    Through some digging I found that I could reference $Folder.FullName thus removing the need for that problematic line. I added a throw in to exit when #Folder equals null as well as changing the exclusions to variables. Also renamed some variables for personal taste. So the final code if anyone looks at this thread in years to come:

    $Today = Get-Date
    $DaysToKeep = "14"
    $TargetFolder = "E:\Program Files\Research In Motion\Blackberry Enterprise Server\Logs"
    $LastWrite = $Today.AddDays(-$DaysToKeep)
    $FldrExclusion1 = "webserver"
    $FldrExclusion2 = "installer"

    $Folders = Get-ChildItem -path $TargetFolder |
    Where {$_.psIsContainer -eq $true} |
    Where {$_.LastWriteTime -le "$LastWrite"} |
    Where {$_.name -ne $FldrExclusion1} |
    Where {$_.name -ne $FldrExclusion2}


        foreach ($Folder in $Folders)
        {
            if ($Folder -eq $null) {
                throw "No further folders to delete"
            }
            else {
                write-host "Deleting $Folder.FullName" -foregroundcolor "Red"
                Remove-Item $Folder.FullName -recurse -Confirm:$false
            }
        }

    Note: this was created to clear out the BES log folders hence the "webserver" and "installer" folder exclusions, these can obviously be changed / added too if you want more or different.







    • Edited by Greg Steer Wednesday, November 23, 2011 10:43 PM
    • Marked as answer by Greg Steer Wednesday, November 23, 2011 11:40 PM
    Wednesday, November 23, 2011 10:30 PM
  • I was getting the null issue too and actually lost some folders :(

    But the little tweaks you put in ( if ($Folder -eq $null) ) worked beautifully, thanks for sharing!


    • Edited by Crosswalk Saturday, February 18, 2012 12:35 AM Clarifying the earlier comment.
    Saturday, February 18, 2012 12:34 AM
  • I was able to setup this script in the test environment without an issue but not in the production environment.  I get the message no further folders to delete.  In the production environment, the only difference is that the BESlogs are on the d:drive.

    Thanks for the awesome PSH script.

    Cheers.

    Wednesday, July 25, 2012 10:23 PM
  • Hi,

    i have been trying this but cant seem to get it to work.

    My folder strcucture is as follows:

    G:\Folder1\Folder2

    Within folder 2 there are 80 folder

    Under Folder 1 i have 7 additional folders. Folders 3..4..5..6 etc

    When i run the script it ignores folder 2 and its subfolders.

    Ideally i need it to delete all folders over 10 days old under Folder1. Any ideas

    Thursday, August 6, 2015 10:30 AM
  • This topic is closed.  Please open a new topic with your question.

    \_(ツ)_/

    Thursday, August 6, 2015 12:00 PM