We want to grab all the files within source folder and its subfolders, and keep the files in its previous locations.
It is required, that files will be named in such a way, allowing to use minus symbols instead of underscores.
Because of that, spaces, and every accented character which resides in the filename will got removed or replaced.
It is not awaited, that one will be sitting by the NB and renaming the files manually. Powershell will be used instead.
Although in file structure of thousands of files, you will not have to wait too long - just few seconds will be needed.
The script takes into consideration lower-case characters in filenames. Each non-standard char will be removed.
# Declaration of the folder containing the files to rename
$rootFolder = "C:\_DEV\inzertnistranky\images"
# The function for removal of accented and non-standard characters within the string (Regular Expressions used - make sure they are escaped properly)
function Remove-AccentedCharactersAndSpaces {
param([string]$inputString)
$normalizedString = $inputString.Normalize('FormD')
$asciiOnly = $normalizedString -creplace "[^\x20-\x7E]", "" #Removal of each accented character in the string.
$noSpaces = $asciiOnly -replace '\s', '-' #The replacement of space character by minus.
$nounderscores = $noSpaces -replace '_', '-' #The replacement of underscore symbol to minus.
$noequales = $nounderscores -replace '\=', '' #Removal of symbol equals to.
$noleftbracket = $noequales -replace '\(', '' #Removal of left bracket.
$norightbracket = $noleftbracket -replace '\)', '' #Removal by right bracket.
$nodoubleminus = $norightbracket -replace '\-\-', '\-' #The removal of the multiminuses and replaced by single minus.
return $nodoubleminus.ToLower() #The function returns the name of the file in lowercase string.
}
# Funkce pro rekurzivní přejmenovávání souborů
function Rename-FilesRecursively {
param([string]$folderPath) #The parameter of the function - the path to the files
# Get each files in the folder as the object
$files = Get-ChildItem -Path $folderPath -File #The files array
# To rename each file
foreach ($file in $files) { #File objects in files array
$newFileName = Remove-AccentedCharactersAndSpaces -inputString $file.BaseName #Call the function for accents and spaces removal
if ($newFileName -ne $file.BaseName) { #If new filename does not comply to the previous filename, generate the variables for renaming
$extension = $file.Extension #extension
$newBaseName = $newFileName + $extension #new filename with extension
$newFilePath = Join-Path -Path $file.Directory.FullName -ChildPath $newBaseName #nová cesta k přejmenovanému souboru
Rename-Item -Path $file.FullName -NewName $newFilePath -Force #Renaming of the file to new name after accents and spaces were replaced
}
}
# Recursive call for each subfolder
$subfolders = Get-ChildItem -Path $folderPath -Directory #The object for subfolders
foreach ($subfolder in $subfolders) {
Rename-FilesRecursively -folderPath $subfolder.FullName #The call of the recursive function for renaming of each file in the subfolder
}
}
# The calling for the function initiating the rename of the files
Rename-FilesRecursively -folderPath $rootFolder