Created
December 5, 2022 21:11
-
-
Save metadaddy/a9924431bab19fc20cb0c85494f5a3e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# .SYNOPSIS | |
Create a marker file in each empty directory under a given root directory | |
.DESCRIPTION | |
This script recursively finds empty directories under the given root | |
directory and creates a marker file with the given filename in each one. | |
.NOTES | |
Author : Pat Patterson | |
#> | |
param ( | |
[Parameter(Mandatory, HelpMessage='Specifies the root directory from which to start.')] | |
[string] | |
$RootDirectory, | |
[Parameter(Mandatory, HelpMessage='Specifies the name of the marker file.')] | |
[string] | |
$Filename | |
) | |
Write-Host "These folders are empty and a file will be created:" | |
# Initialize array for marker file paths | |
$paths = @() | |
# Recurse from the given root directory | |
Get-ChildItem -Path $RootDirectory -Recurse | | |
# Is the item a directory? | |
? {$_.PSIsContainer -eq $True} | | |
# Is the directory empty? | |
? {$_.GetFileSystemInfos().Count -eq 0} | | |
ForEach-Object { | |
# Make the marker file path and append it to the array | |
$path = $_.FullName + "/" + $Filename | |
$paths += $path | |
# Output the marker file path | |
Write-Host "$($path)" | |
} | |
if ($PSCmdlet.ShouldContinue("Proceed?", "Create Marker Files")) { | |
ForEach ($path in $paths) { | |
# Create the marker file | |
New-Item -Path $path -ItemType "file" -Confirm:$False | Out-Null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment