I’ve got close to a million files and folders on a Windows server with file names that are polluted with Apple MacOS special characters in random places. Some of these characters create robocopy errors when archiving.
The offending special characters are:
- Zero Width Space (0x200b)
- U+F028 Unicode character
I can find these files in Windows File Explorer by using these commands in the Search tool.
- name:~”“
- name:~”“
I wish to rename them, deleting the special character.
I have Googled at length and PowerShell seems to be the way forward.
I’ve devised a script that I could run from the root path of the drive, but I am not sure if this script is optimised (or a wise choice).
#Set location to run from
set-location -Path "P:"
#Rename folders containing Zero Width Spaces
$folders = Get-ChildItem -Recurse -Directory | Sort-Object
$folders | ForEach-Object {
set-location $_.Parent.FullName
Rename-Item $_.Name ($_.Name -replace("", ""))
}
#Rename folders containing U+F028
$folders = Get-ChildItem -Recurse -Directory | Sort-Object
$folders | ForEach-Object {
set-location $_.Parent.FullName
Rename-Item $_.Name ($_.Name -replace("", ""))
}
#Rename files containing Zero Width Spaces
$files = Get-ChildItem -File -Recurse
$files | ForEach-Object {
Rename-Item -Path $_.PSPath -NewName $_.Name.replace("","")
}
#Rename files containing U+F028
$files = Get-ChildItem -File -Recurse
$files | ForEach-Object {
Rename-Item -Path $_.PSPath -NewName $_.Name.replace("","")
}
I believe the logic is correct.
My artificial test scenario (some demo files and folders) works.
But am I about to crash a server by running this at scale?!
3