Good day, We have a script that removed files older than 7 days and empty folders which are not incoming or outgoing folders.
Unless the path is within the $exclusion variable as shown below. I was under the impression when I did my get-childitem it would ignore the paths I entered with the $exclusion however it seems to grab subfolders within the excluded folders as well as the folders themselves.
For instance the paths shown below in the commented section. I am at a loss, and any help or direction would be greatly appreciated.
$path='\\Root\Test\'
$exclusion= @(Get-Content-LiteralPath'c:\scripts\FileSystem\FolderExclusions.txt')
#Folderexclusions.txt has paths within there like
#\\Root\Test\test1
#\\Root\Test\test2
#Function to clean the folders
Functiondelete-emptyfolders {
param([string] $path)
#get all folders in the path except those in $exclusion and set boolean
$dirs=Get-ChildItem-Path$path-Recurse-Directory | where {$fn=$_.FullName; ($exclusion | where {$fn-like$_}).count-eq 0}
$deldir=$false
#loop to remove directories if they are empty
foreach($dirin$dirs) {
$subitems=Get-ChildItem$dir.PsPath
#check to see if folder is empty and not an incoming or outgoing folder
if(($subitems.count-eq 0) -and (($dir.Name.ToLower() -ne'incoming') -and ($dir.Name.ToLower() -ne'outgoing'))) {
$Error.clear()
$deldir=$trueRemove-Item$dir.PsPath-Force-Recurse
#write to log files
if($?) {
$dir.FullName+' has been deleted' | Out-File$delog-Append
}
else
{
$errorstring='`n'+$ErrorAdd-Content$errlog$errorstring
}
}
}
#check the boolean to process any new empty folders
if($deldir-eq$true) {
#delete old array and call function recursivly
$dirs=$null$dir=$null$subitems=$null
delete-emptyfolders$path
}
}
Unless the path is within the $exclusion variable as shown below. I was under the impression when I did my get-childitem it would ignore the paths I entered with the $exclusion however it seems to grab subfolders within the excluded folders as well as the folders themselves.
For instance the paths shown below in the commented section. I am at a loss, and any help or direction would be greatly appreciated.
$path='\\Root\Test\'
$exclusion= @(Get-Content-LiteralPath'c:\scripts\FileSystem\FolderExclusions.txt')
#Folderexclusions.txt has paths within there like
#\\Root\Test\test1
#\\Root\Test\test2
#Function to clean the folders
Functiondelete-emptyfolders {
param([string] $path)
#get all folders in the path except those in $exclusion and set boolean
$dirs=Get-ChildItem-Path$path-Recurse-Directory | where {$fn=$_.FullName; ($exclusion | where {$fn-like$_}).count-eq 0}
$deldir=$false
#loop to remove directories if they are empty
foreach($dirin$dirs) {
$subitems=Get-ChildItem$dir.PsPath
#check to see if folder is empty and not an incoming or outgoing folder
if(($subitems.count-eq 0) -and (($dir.Name.ToLower() -ne'incoming') -and ($dir.Name.ToLower() -ne'outgoing'))) {
$Error.clear()
$deldir=$trueRemove-Item$dir.PsPath-Force-Recurse
#write to log files
if($?) {
$dir.FullName+' has been deleted' | Out-File$delog-Append
}
else
{
$errorstring='`n'+$ErrorAdd-Content$errlog$errorstring
}
}
}
#check the boolean to process any new empty folders
if($deldir-eq$true) {
#delete old array and call function recursivly
$dirs=$null$dir=$null$subitems=$null
delete-emptyfolders$path
}
}