Wildcard in path parameter not working as expected

Hi,

I’ve been testing the following functions in order to create a housekeeping script for a filer:

function Remove-EmptyDirectories([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty, [DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue, [DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue, [switch] $OutputDeletedPaths, [switch] $WhatIf)
{
    Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get-ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } | 
        Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | 
        ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -Recurse -WhatIf:$WhatIf }
 
    # If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it.
    if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and
        ((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and ((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate))
    { if ($OutputDeletedPaths) { Write-Output $Path } Remove-Item -Path $Path -Force -WhatIf:$WhatIf }
}

# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $DateTime, [switch] $DeletePathIfEmpty, [switch] $OutputDeletedPaths, [switch] $WhatIf)
{
    Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | 
        ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf }
    Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OutputDeletedPaths:$OutputDeletedPaths -WhatIf:$WhatIf
}

This is courtesy of PowerShell Functions To Delete Old Files And Empty Directories - Daniel Schroeder’s Programming Blog

I have the path “C:\Octopus\Applications\New Folder” & “New Folder (2)”

My command is

Remove-FilesCreatedBeforeDate -Path “C:\Octopus\Applications” -DateTime ((Get-Date).AddDays(-0)) -Whatif -OutputDeletedPaths

Running the above command works fine and all folders under the “Applications” dir are checked and removed if empty but this is just a test environment and the actual hierarchy will have multiple folders under the “Applications” dir that I don’t want to be removed but too numerous to list as separate commands so want to use a wildcard as in “C:\Octopus\Applications*” but this only seems to run on “New Folder (2)”. I could comment out the call to the function “Remove-EmptyDirectories” but then this would stop this feature altogether which isn’t what I want.

Am I missing something to be able to use a wildcard as part of the path parameter?

Cheers

No, I don’t think you are missing anything, although contacting the original author would probably be useful. This function just wasn’t designed with an “exception list,” and it seems like that’s something you might want to code into it and perhaps contribute back to the original author.

I have already posted there but hasn’t been much activity, since my posts, which is why I thought I’d check elsewhere. I take it you mean an exception list on the Remove-EmptyDirectories function?

Yeah, I’d probably add a parameter so you could pass in a collection of file paths. Let’s say that’s $ExceptionList declared as [string]. Then before you remove a given file, say its file name is in $candidate, I’d do a “If ($candidate -in $exceptionlist)” check. If the file is in the list, don’t remove it. Something vaguely along those lines.

Ahh good shout, I’ve managed to get round this for now by adding the “-exclude foldername” as a parameter after the Remove-Item cmdlet on line five under the Remove-EmptyDirectories function. As long as the parent folders are unique then this shouldn’t interfere further down the hierarchy.

Thanks for the suggestion.

Ok nice one. Thanks for the advice. I have enough to get it working as I want for now but will look to improve this as you mention later down the line.