Help Creating a Script to Find and Rename Folders/Files Containing the Ampersand Character

I recently started messing with SharePoint Foundation 2013 here at work.  I started copying some folders/files over and almost immediately ran into an issue with folders/files throwing errors due to there being illegal characters in the folder/file names.

I'm now looking for a way to rename any and all folders/files that contain an ampersand in the name.

I'm hoping someone here could help me with creating a script for this.  I'm thinking it would need to consist of 2 "parts" due to some folder/file names having a space before/after the ampersand and other folder/file names having no space before/after the ampersand.

Essentially, I'm looking to have a script created that can do the following on a Windows Server OS:
- search a folder and all sub-folders for any and all folders and filenames containing space&space <-- example file/folder name Alligators & Crocodiles
- rename these folders/files spaceandspace <-- example file/folder name Alligators and Crocodiles

- search the same series of folders for any and all folders and files containing no space&no space <-- example file/folder name Up&Down
- rename these folders/files spaceandspace <-- example file/folder name Up and Down

I'd appreciate any help you could provide me with.
LVL 1
homerslmpsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
PowerShell; adjust the $baseDir variable, save as Whatever.ps1, and run with an account with enough permissions on the file server.
If PowerShell complains about the script not being signed, run
Set-ExecutionPolicy Bypass -Force
The script is in test mode and will only show what it would rename how. Set the variable $testMode to $false to run it for real.
It will do the renaming including surrounding spaces in one go.
$baseDir = 'C:\Test'
$testMode = $true
$verbose = $true

$pattern = '\s*&\s*'
$replaceWith = ' and '
Get-ChildItem -Path $baseDir -File -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Rename-Item -NewName {$_.Name -replace $pattern, $replaceWith} -Verbose:$verbose -WhatIf:$testMode
Get-ChildItem -Path $baseDir -Directory -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Sort-Object -Property @{e={$_.FullName.Split('\').Count}} -Descending |
	Rename-Item -NewName {$_.Name -replace $pattern, $replaceWith} -Verbose:$verbose -WhatIf:$testMode

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
homerslmpsonAuthor Commented:
Thank you so much!
It's perfect!
Bill PrewIT / Software Engineering ConsultantCommented:
For these one time type renaming activities I often use a utility program rather than a script.  They allow you to preview the changes first and have lots of options and flexibility.  There is a learning curve, but once you get the hang of them the next times go faster...

A couple that I keep in my toolbox:



»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.