65,000 files batch-renamed in less than a minute!
How to quickly and easily find and replace characters in file or directory names in Windows?
To bulk rename files and folders should be easy.
I wanted a quick solution, that didn’t require that I have great coding skills, and where I wouldn’t have to wrestle with installing a feature-rich program and wrestling with its interface. Just something I can do, easily, now.
It turns out that I can batch rename file and folder names with PowerShell.
The Problem
I am uploading images into OneDrive, and then I get this error
OneDrive needs your attention – 314 items can’t be synced
This message will also pop up when right clicking on OneDrive and checking “view sync problems”
Investigating a little deeper I discover that in each file name there is a hash # which OneDrive doesn’t like
It turned out that there were 65,000 of these files with a hash in the file name
The Solution – using PowerShell
With this simple code I can find the hash in any file name in directories and subdirectories and replace it with an underscore.
$dir = "C:\Users\Alan\OneDrive - CalmIT\Photos"
cd $dir
Get-Childitem -Recurse | `
Where-Object {$_.Name -match '#'} | `
Rename-Item -Newname { $_.Name -replace '#','_' }
- Specify the target directory = C:\Users\Alan\OneDrive\Photos
- Specify the search string to be found in both the “find” and “replace” sections of the code = #
- Specify the string to be replaced = _
- Copy and Paste into PowerShell and hit Enter
- 65,000 instances batch-renamed in less than a minute
Thank you to Jamie Hutchinson’s YouTube video
If you can improve on this please add your suggestions in the comments.
No comments yet.