how to convert filenames with special characters to valid filenames?
file, filenames, powershell, recursion, sharepoint
Solution
I haven't tested this code, but I think you probably want something similar to this:
Function Rename-Files($path)
{
Get-ChildItem -path $path |
Foreach-Object {
$newName = $_.name -replace '[^A-Za-z0-9-_ \.\[\]]', ''
if (-not ($_.name -eq $newname)){
Rename-Item -Path $_.fullname -newname ($newName)
}
}
} #end function
Rename-Files -path "C:\somepath"
effectively, just loop through the directory (modify the function to be recursive) and then replace any inappropriate (modify the regex for what is appropo) characters and replace them with nothing (effectively strip them out).
Problem
I would like to know whether it is possible to REMOVE special characters recursively in powershell from all files in an entire directory structure? for example if I have a file called: ``` my presentation (fix).xlsx ``` I would like to rename it to: ``` my presentation fix.xlsx ``` thank you for your guidance. the reason we need to do this is we are migrating many files into sharepoint 2010 and sharepoint doesnt like files with special characters!