Skip to content

Instantly share code, notes, and snippets.

@9999years
Created December 16, 2016 00:12
Show Gist options
  • Save 9999years/6953982f38a5edb02cfc495c0744bb8d to your computer and use it in GitHub Desktop.
Save 9999years/6953982f38a5edb02cfc495c0744bb8d to your computer and use it in GitHub Desktop.
A set of Powershell cmdlets for modifying file names (replacing strings and stripping end/start strings)
function StripStartName {
[CmdletBinding()]
Param(
[String]$Name
)
ls | %{
if($_.Name.StartsWith($Name)) {
ren -LiteralPath "$($_.FullName)" "$($_.Parent.FullName)\$($_.Name.Substring($Name.Length))"
}
}
}
function StripEndName {
[CmdletBinding()]
Param(
[String]$Name
)
ls | %{
if($_.Name.EndsWith($Name)) {
ren -LiteralPath "$($_.FullName)" "$($_.Parent.FullName)\$($_.Name.Substring(0, $_.Name.LastIndexOf($Name)))"
}
}
}
function ReplaceInFileName {
[CmdletBinding()]
Param(
[String]$Replace,
[String]$With
)
ls | %{
if($_.Name.Contains($Replace)) {
ren -LiteralPath "$($_.FullName)" "$($_.Parent.FullName)\$($_.Name.Replace($Replace, $With))"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment