Skip to content

Instantly share code, notes, and snippets.

@davidalpert
Created January 24, 2016 09:48
Show Gist options
  • Save davidalpert/56734477d7c1b2bc6542 to your computer and use it in GitHub Desktop.
Save davidalpert/56734477d7c1b2bc6542 to your computer and use it in GitHub Desktop.
Function Add-Prefix([switch]$inputObject,$io) {
BEGIN {
if ($inputObject) {$io | &($MyInvocation.InvocationName) $args; break;}
$args = @($io) + $args
$prefix = $args[0]
Write-host "Adding $prefix as a prefix..."
}
PROCESS {
$name = $_.Name
if ($name.StartsWith($prefix)) {
Write-Host "ignored: $name"
}
else
{
mv $_.Name "$prefix$name"
Write-Host "renamed: $prefix$name".
}
}
END {
$x = 0
#$args | % { $x++; "$x - $_" }
Write-host "End"
}
}
Function Remove-Prefix([switch]$inputObject,$io) {
BEGIN {
if ($inputObject) {$io | &($MyInvocation.InvocationName) $args; break;}
$args = @($io) + $args
$prefix = $args[0]
Write-host "Removing $prefix as a prefix..."
}
PROCESS {
$name = $_.Name
if ($name.StartsWith($prefix)) {
$newName = $name.Substring($prefix.Length);
mv $_.Name "$newName"
Write-Host "renamed: $newName"
}
else
{
Write-Host "ignored: $name".
}
}
END {
$x = 0
#$args | % { $x++; "$x - $_" }
Write-host "End"
}
}
Function Add-Postfix([switch]$inputObject,$io) {
BEGIN {
if ($inputObject) {$io | &($MyInvocation.InvocationName) $args; break;}
$args = @($io) + $args
$postfix = $args[0]
Write-host "Adding $postfix as a postfix..."
}
PROCESS {
$ext = $_.Extension
$name = $_.Name
$stem = $name.Substring(0,$name.Length - $ext.Length)
if ($stem.EndsWith($postfix)) {
Write-Host "ignored: $name"
}
else
{
$newName = "$stem$postfix$ext"
mv $_.Name "$newName"
Write-Host "renamed: $newName"
}
}
END {
$x = 0
#$args | % { $x++; "$x - $_" }
Write-host "End"
}
}
Function Remove-Postfix([switch]$inputObject,$io) {
BEGIN {
if ($inputObject) {$io | &($MyInvocation.InvocationName) $args; break;}
$args = @($io) + $args
$postfix = $args[0]
Write-host "Removing $postfix as a postfix..."
}
PROCESS {
$ext = $_.Extension
$name = $_.Name
$stem = $name.Substring(0,$name.Length-$ext.Length)
if ($stem.EndsWith($postfix)) {
$newStem = $stem.Substring(0, $stem.Length-$postfix.Length);
$newName = "$newStem$ext"
mv $_.Name "$newName"
Write-Host "renamed: $newName"
}
else
{
Write-Host "ignored: $name"
}
}
END {
$x = 0
#$args | % { $x++; "$x - $_" }
Write-host "End"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment