-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenameUnidcode - ISE
26 lines (23 loc) · 1.04 KB
/
RenameUnidcode - ISE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$targetDirectory = "%APPDATA%" # Set your target directory here
# Function to remove non-ASCII characters
function Remove-NonAsciiCharacters {
param ([string]$InputString)
$OutputString = $InputString -replace "[^\x00-\x7F]", ""
return $OutputString
}
# Get all files in the target directory recursively
$files = Get-ChildItem -Path $targetDirectory -Recurse -File
foreach ($file in $files) {
$newName = Remove-NonAsciiCharacters -InputString $file.Name
# Check if the new name is different and not empty
if ($newName -ne $file.Name -and $newName -ne "") {
$newFullPath = Join-Path -Path $file.DirectoryName -ChildPath $newName
# Check if a file with the new name already exists
if (-Not (Test-Path -Path $newFullPath)) {
Rename-Item -Path $file.FullName -NewName $newName
Write-Output "Renamed `"$($file.Name)`" to `"$newName`""
} else {
Write-Output "Cannot rename `"$($file.Name)`" to `"$newName`" because a file with the new name already exists."
}
}
}