Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created December 5, 2022 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metadaddy/a9924431bab19fc20cb0c85494f5a3e3 to your computer and use it in GitHub Desktop.
Save metadaddy/a9924431bab19fc20cb0c85494f5a3e3 to your computer and use it in GitHub Desktop.
<# .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