Skip to content

Instantly share code, notes, and snippets.

@mshroyer
Created July 30, 2010 06:59
Show Gist options
  • Save mshroyer/500051 to your computer and use it in GitHub Desktop.
Save mshroyer/500051 to your computer and use it in GitHub Desktop.
# PowerShell command to delete editor (Emacs, Vim, etc.) tilde backup files
# from any directories passed to it either as a parameter or through the
# pipeline.
function Remove-EditorBackups() {
param([string[]]$Paths, [switch]$Recurse, [switch]$Force, [switch]$Verbose)
begin {
$script:foundInput = $false
function delete($fileinfo) {
if ( $Verbose ) {
Write-Output $fileinfo.FullName
}
$fileinfo.Delete()
}
function promptToDelete($fileinfo) {
if ( $Force ) {
delete($fileinfo)
} else {
$in = Read-Host ( "Delete " + $fileinfo.FullName + "? [y/N]" )
if ( $in.ToLower().StartsWith("y") ) {
delete($fileinfo)
}
}
}
function deleteBackupsFrom($folder) {
$script:foundInput = $true
if ( $Recurse ) {
$files = Get-ChildItem $folder -Recurse
} else {
$files = Get-ChildItem $folder
}
if ( $files ) {
$files `
| Where-Object { $_.Name -match "~$" } `
| Where-Object { ! $_.PSIsContainer } `
| ForEach-Object {
promptToDelete($_)
}
}
}
}
process {
if ($_) {
deleteBackupsFrom($_)
}
}
end {
if ( $Paths ) {
foreach ($path in $Paths) {
deleteBackupsFrom($path)
}
}
if ( ! $script:foundInput ) {
$workdir = Get-Location
deleteBackupsFrom($workdir)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment