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

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?

November 22nd, 2011 7:39pm

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.
Free Windows Admin Tool Kit Click here and download it now
November 22nd, 2011 8:11pm

One reason it would delete the $targetFolder itself is if the $folder variable is empty.
November 22nd, 2011 8:14pm

One reason it would delete the $targetFolder itself is if the $folder variable
Free Windows Admin Tool Kit Click here and download it now
November 23rd, 2011 5:47pm

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
November 23rd, 2011 10:30pm

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
Free Windows Admin Tool Kit Click here and download it now
November 23rd, 2011 10:30pm

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.
February 18th, 2012 12:34am

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.
Free Windows Admin Tool Kit Click here and download it now
February 18th, 2012 12:34am

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.

July 25th, 2012 10:23pm

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

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2015 6:32am

This topic is closed.  Please open a new topic with your question.
August 6th, 2015 8:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics