Reputation: 13
So I am currently in the middle of try to migrate a massive amount of end user files from NTFS to MS OneDrive. The problem I am facing is that OneDrive does not support special characters in file names and these files are riddled with them.
So the idea is to copy the file and folder structure to a new destination and run a PS script to rename them all and remove all the special chars from them.
Get-childobject -recurse
seams to be the best option here.
I can rename a single file using something along the lines of:
$newname = $oldname.replace ("&"," and ")
$newname = $newname.replace (" & "," and ")
$newname = $newname.replace ("/"," ")
#etc...
However I am not certain as to how I would either pipe or bracket these together and if it is even possible?
Or is there something I have overlooked that is far simpler?
Any help is greatly appreciated!
Upvotes: 1
Views: 307
Reputation: 4256
To wrap multiple replace operations together, one can either wrap [String]
's Replace()
method like:
$newName = $oldName.Replace("&", "and").Replace("_", "-") #etc.
or use PowerShells builtin -replace
operator:
$newName = $oldname -replace "&", "and" -replace "_", "-" #etc.
To generally fit Strings into conventions there are methods like [uri]::EscapeDataString()
. I'm not sure about OneDrive and their file name conventions, but could imagine they have to be convertable to internet URLs.
To make it more readable one could build a HashTable
like the following:
$replacements = @{
"&" = "and";
"_" = "-";
"/" = "-";
}
$fileName = "Actually/IsSupportedByNeitherNTFSNorOneDrive"
$replacements.Keys | ForEach-Object {$fileName = $fileName.replace($_, $replacements[$_])}
Write-Output $fileName
#Actually-IsSupportedByNeitherNTFSNorOneDriveandsomeOthers
Upvotes: 1