Skip to content

Instantly share code, notes, and snippets.

@francisoud
Created December 23, 2015 13:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save francisoud/a214aac5b07047a631e7 to your computer and use it in GitHub Desktop.
Save francisoud/a214aac5b07047a631e7 to your computer and use it in GitHub Desktop.
A powershell script to remove spaces and special char from filenames
Function Rename-Files($path)
{
Get-ChildItem -path $path |
Foreach-Object {
if($_ -is [System.IO.DirectoryInfo]) {
echo "directory: $_"
Rename-Files -path $_.FullName
} else {
$newName = $_.name -replace '[^A-Za-z0-9-_ \.\[\]]', ''
$newName = $newName -replace ' ', '_'
if (-not ($_.name -eq $newname)){
echo "renaming: $_.Name to: $newName"
Rename-Item -Path $_.fullname -newname ($newName)
}
}
}
} #end function
Rename-Files -path "C:\path\to\dir\"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment