The file name is invalid or the file is empty. A file name cannot contain any of the following characters: \ / : * ? " < > | # { } % ~ &But what if you have hundreds or thousands of files that DO include one or more symbols, and you need to move them onto SharePoint?
The following PowerShell script will replace a string in a filename with another string you provide. It doesn’t alter file extensions or folders. The script can be executed with command line parameters, however if they are not provided, it will prompt you for input. The following parameters are used:
- The directory e.g. “C:\test”
- The text to find e.g. “&”
- The replacement text e.g “and”
- An optional switch that specifies whether to recurse through subfolders: –recurse
- fileRenamer.ps1 “C:\test” “&” “and” –recurse
Error Handling
The only issue this script handles is when the provisional new file name for a file already exists e.g. you are changing Q&A.doc to QandA.doc, and this filename is already in use. In this instance, the script will notify the user that the rename operation has been cancelled and summary information is added to the end of the script.Script Output
The script prints its progress to the console, and also provides some summary information at the end:Disclaimer
Use at your own risk. I will not be held responsible for any actions and/or results this script may cause. If you choose to use this script; test it first, and only run on directories/files with backups as the implications of renaming large numbers of files are serious and the operation cannot be undone.The Code
Paste this into a new file and save it as “fileRenamer.ps1” or similar.param(
[switch]$recurse,
[string]$directory = $(read-host "Please enter a directory"),
[string]$find = $(read-host "Please enter a string to find"),
[string]$replace = $(read-host "Please enter a replacement string")
)
# Startup text
echo "This script will replace words in files of any type. It will not modify file extensions."
echo ""
echo "WARNING: This action cannot be undone; use at your own risk!"
echo ""
# Setup variables
$modifiedcounter = 0
$counter = 0
$exceptioncounter = 0
$exceptionfilenames = ""
$files = ""
echo "Replacing text '$find' with '$replace' in all filenames..."
echo ""
if ($recurse)
{
# Grab all files recurse
$files = get-childitem $directory -include *.* -recurse
}
else
{
$files = get-childitem $directory *.*
}
foreach ($file in $files) {
$filename = $file.Name
# Only run if this is a file
if ($filename.IndexOf('.') -gt 0)
{
$name_noextension = $filename.SubString(0, $filename.LastIndexOf('.'))
$extension = $filename.SubString($filename.LastIndexOf('.'), $filename.Length-$filename.LastIndexOf('.'))
echo ("Filename: " + $file.FullName)
#echo "Extension removed: $name_noextension"
#echo "Extension: $extension"
# If there is a match then attempt to rename the file
if ([regex]::IsMatch($name_noextension, ".*$find.*")) {
# Change the filename
$name_noextension = $name_noextension.Replace($find, $replace);
$newname = $name_noextension += $extension
#echo "New name: $newname"
# Test to see whether a file already exists
if (Test-Path ($file.DirectoryName + "\" + $newname))
{
# A file already exists with that filename
echo ("Action: A file exists with the name " + ($file.DirectoryName + "\" + $newname) + " so this action has been cancelled.")
$exceptioncounter++
$exceptionfilenames += ($file.DirectoryName + "\" + $newname)
}
else
{
# Rename the file
rename-item $file.FullName -newname $newname
echo "Action: Filename modified from $filename to $newname."
$modifiedcounter++
}
}
else
{
echo "Action: None";
}
$counter++
}
}
# Output the results
echo ""
echo "$modifiedcounter of $counter filenames modified."
echo "Exception count: $exceptioncounter"
# If there were exceptions then output that as well
if ($exceptioncounter -gt 0)
{
echo ""
echo "The following files were not modified as files already existed with their provisional new names:"
echo $exceptionfilenames
}
Read-host "Press enter to quit"