3

First case:

I created a rename text file ren.bat. The contents of the file is this:

ren "a*.pdf" "T*.pdf"

When I run the ren.bat file, the command renames all PDF files in the folder but only the first letter a does not run through the whole file name replacing the letters a.

Can anyone tell me what's wrong?

Second case:

When I run the command:

ren "ã*.pdf" "a*.pdf"

The command does not rename the first letter.

Can anyone tell me what's wrong?

I found a FOR code that worked. But I do not know how to adapt it, so that I can read all the files in the folder without having to identify the file name in question. What you are trying to create is a .bat file that renames the files by removing accents that are indenting the names they are using. Note that this code must specify the file name:

For /F "Tokens=2 Delims=:" %%I In ('Chcp ') Do Set /A _CurCP=%%I
Chcp 1252
ren "méxico.txt" "mexico.txt"
Chcp %_CurCP%

I needed it to be something like that, but that did not work:

ren "áéí*.txt" "aei*.txt"
Mofi
  • 46,139
  • 17
  • 80
  • 143
Ed Dias
  • 175
  • 1
  • 3
  • 11
  • 1
    When using `ren "a*.pdf" "T*.pdf"` the letter `a` is restricted to the 1st position. To change change all `a`s to `T`s you'll need string substitution and a `for` command to iterate the wildcard. 2nd case depending on file system and encoding the `ã` may not be a single letter char. –  Sep 21 '18 at 19:40

1 Answers1

4

First, you should learn that characters can be saved with different code values. This is called character encoding.

There are character encodings on which only one byte with 8 bits is used to represent a character giving a maximum of 2^8 = 256 characters. But there are lots of characters, much more than 256. For that reason there are many code pages. A code page defines which byte value represents which character.

Windows sets a code page for graphical user interface (GUI) and GUI applications like Windows Notepad as well as for console applications like Windows command processor according to configured country.

For Western European countries the code page Windows-1252 is set by default for text encoded with just one byte per character. The code page used on console for Western European countries is OEM 850 as it can be seen on opening a command prompt window and running the command chcp without any parameter. chcp is an abbreviation for change code page.

The Unicode Consortium was formed to stop the definition of more and more code pages, see What is Unicode? This consortium introduced Unicode – the universal code for consistent encoding, representation, and handling of text expressed in most of the world's writing systems. This consortium defined also standards on how to encode characters and symbols. Most popular are UTF-8 and UTF-16.


Second, with having knowledge about character encodings let us take a look on characters like ã and é.

Windows Notepad uses Windows-1252 on saving a batch file with using ANSI for option Encoding in the Save As dialog window with Spain configured as country. ANSI means here one byte per character using a code page and not American National Standards Institute which standardized many code pages.

But Windows command processor cmd.exe interpreting the lines in a batch file uses code page OEM 850.

It is possible to define the character encoding respectively the code page individually for each running process. You might have seen already the two letter language abbreviation on Windows task bar which can be used to change the language for the currently active application, i.e. the code page to use for non Unicode character encoding.

Character ã has decimal code value 227 in code page Windows-1252, but code value 198 in code page OEM 850. Character é as decimal code value 233 in code page Windows-1252, but code value 130 in code page OEM 850.

So how to solve this problem of different character encodings?

One solution is writing the batch file with using code page OEM 850. That is not really easy with Windows Notepad. Other text editors support this better like UltraEdit which I have configured to automatically edit *.bat and *.cmd files using OEM code page as defined by Windows for console applications while all other non Unicode encoded text files with the Windows code page as defined by Windows for GUI applications.

Another solution is writing the batch file with using code page Windows-1252 and change the code page on execution of the batch file first to this code page with command chcp 1252 before executing the other command lines. This is the easier solution for most users.


So what about this batch code using string substitutions?

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

:RenameFile
set "FileName=%~1"
set "FileName=%FileName:Š=S%"
set "FileName=%FileName:Ž=Z%"
set "FileName=%FileName:š=s%"
set "FileName=%FileName:ž=z%"
set "FileName=%FileName:Ÿ=Y%"
set "FileName=%FileName:À=A%"
set "FileName=%FileName:Á=A%"
set "FileName=%FileName:Â=A%"
set "FileName=%FileName:Ã=A%"
set "FileName=%FileName:Ä=A%"
set "FileName=%FileName:Å=A%"
set "FileName=%FileName:È=E%"
set "FileName=%FileName:É=E%"
set "FileName=%FileName:Ê=E%"
set "FileName=%FileName:Ë=E%"
set "FileName=%FileName:Ì=I%"
set "FileName=%FileName:Í=I%"
set "FileName=%FileName:Î=I%"
set "FileName=%FileName:Ï=I%"
set "FileName=%FileName:Ñ=N%"
set "FileName=%FileName:Ò=O%"
set "FileName=%FileName:Ó=O%"
set "FileName=%FileName:Ô=O%"
set "FileName=%FileName:Õ=O%"
set "FileName=%FileName:Ö=O%"
set "FileName=%FileName:Ù=U%"
set "FileName=%FileName:Ú=U%"
set "FileName=%FileName:Û=U%"
set "FileName=%FileName:Ü=U%"
set "FileName=%FileName:Ý=Y%"
set "FileName=%FileName:à=a%"
set "FileName=%FileName:á=a%"
set "FileName=%FileName:â=a%"
set "FileName=%FileName:ã=a%"
set "FileName=%FileName:ä=a%"
set "FileName=%FileName:å=a%"
set "FileName=%FileName:è=e%"
set "FileName=%FileName:é=e%"
set "FileName=%FileName:ê=e%"
set "FileName=%FileName:ë=e%"
set "FileName=%FileName:ì=i%"
set "FileName=%FileName:í=i%"
set "FileName=%FileName:î=i%"
set "FileName=%FileName:ï=i%"
set "FileName=%FileName:ñ=n%"
set "FileName=%FileName:ò=o%"
set "FileName=%FileName:ó=o%"
set "FileName=%FileName:ô=o%"
set "FileName=%FileName:õ=o%"
set "FileName=%FileName:ö=o%"
set "FileName=%FileName:ù=u%"
set "FileName=%FileName:ú=u%"
set "FileName=%FileName:û=u%"
set "FileName=%FileName:ü=u%"
set "FileName=%FileName:ý=y%"
set "FileName=%FileName:ÿ=y%"

rem Is it necessary to rename the file?
if "%FileName%" == "%~1" goto :EOF

rem Is there already a file with new file name?
if exist "%FileName%" goto :EOF

echo Renaming "%~1" to "%FileName%"
ren "%~1" "%FileName%"
goto :EOF

Well, looks promising. But there is a problem. Windows command processor executes string substitutions always case-insensitive. This means Š and š are replaced both by s on first string substitution command line.

But the following batch file makes the character substitutions case-sensitive.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

:RenameFile
set "FileName=%~1"
set "NewName="
:NextChar
if not defined FileName goto CompareNames
set "Char=%FileName:~0,1%"
set "FileName=%FileName:~1%"
if "%Char%" == "Š" set "NewName=%NewName%S" & goto NextChar
if "%Char%" == "Ž" set "NewName=%NewName%Z" & goto NextChar
if "%Char%" == "š" set "NewName=%NewName%s" & goto NextChar
if "%Char%" == "ž" set "NewName=%NewName%z" & goto NextChar
if "%Char%" == "Ÿ" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "À" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Á" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Â" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ã" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ä" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Å" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "È" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "É" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ê" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ë" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ì" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Í" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Î" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ï" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ñ" set "NewName=%NewName%N" & goto NextChar
if "%Char%" == "Ò" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ó" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ô" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Õ" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ö" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ù" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ú" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Û" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ü" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ý" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "à" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "á" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "â" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ã" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ä" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "å" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "è" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "é" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ê" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ë" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ì" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "í" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "î" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ï" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ñ" set "NewName=%NewName%n" & goto NextChar
if "%Char%" == "ò" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ó" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ô" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "õ" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ö" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ù" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ú" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "û" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ü" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ý" set "NewName=%NewName%y" & goto NextChar
if "%Char%" == "ÿ" set "NewName=%NewName%y" & goto NextChar
set "NewName=%NewName%%Char%" & goto NextChar

:CompareNames
rem Is it necessary to rename the file?
if "%~1" == "%NewName%" goto :EOF

rem Is there already a file with new file name?
if exist "%NewName%" goto :EOF

echo Renaming "%~1" to "%NewName%"
ren "%~1" "%NewName%"
goto :EOF

It would be better to use any other programming or scripting language than doing this file renaming task with Windows command processor using a batch file as it is extremely slow and still error prone in case of a file name contains Unicode characters with a code value not available in code page Windows-1252.

The batch file runs for each character in file name lots of case-sensitive string comparisons with command IF which makes this solution very slow for many file names in current directory.

It is of course possible to optimize this code with using a FOR loop for less command lines, but I think that does not much reduce the time required to do all the case-sensitive file renames.


There are five solutions to make the file renames on a specific folder instead of current directory.

First solution:
The directory with the files to rename is set temporarily as current directory using the command CD:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
cd /D "C:\Temp\Folder with files to rename" 2>nul
if not errorlevel 1 for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

There is nothing to change in subroutine RenameFile in comparison to code above.

This solution does not work with a UNC path by default except a special registry value is modified and used version of Windows supports this registry value at all. However, the change of this registry value is not recommended, see better solutions below working also with a UNC path.

The command ENDLOCAL changes the current directory back to initial current directory on execution of SETLOCAL.

Second solution:
The directory with the files to rename is set temporarily as current directory using the commands PUSHD and POPD:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
pushd "C:\Temp\Folder with files to rename" 2>nul
if not errorlevel 1 (
    for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do call :RenameFile "%%I"
    popd
)
endlocal
goto :EOF

There is nothing to change in subroutine RenameFile in comparison to code above.

This solution works also wit a UNC path as the command PUSHD maps a drive letter to network resource and makes the directory with the drive letter the current directory. The command POPD restores initial directory and deletes the mapping of the drive letter.

PUSHD can fail if a UNC path of a network resource is used and this network resource is currently not available on execution of the command or the specified directory does not exist at all.

Third solution:
The folder path is assigned to an environment variable and used on commands DIR and REN and on new file existence check:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
set "FolderPath=C:\Temp\Folder with files to rename"
for /F "eol=| delims=" %%I in ('dir "%FolderPath%\*" /A-D-H /B 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

:RenameFile
set "FileName=%~1"
set "NewName="
:NextChar
if not defined FileName goto CompareNames
set "Char=%FileName:~0,1%"
set "FileName=%FileName:~1%"
if "%Char%" == "Š" set "NewName=%NewName%S" & goto NextChar
if "%Char%" == "Ž" set "NewName=%NewName%Z" & goto NextChar
if "%Char%" == "š" set "NewName=%NewName%s" & goto NextChar
if "%Char%" == "ž" set "NewName=%NewName%z" & goto NextChar
if "%Char%" == "Ÿ" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "À" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Á" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Â" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ã" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ä" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Å" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "È" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "É" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ê" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ë" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ì" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Í" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Î" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ï" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ñ" set "NewName=%NewName%N" & goto NextChar
if "%Char%" == "Ò" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ó" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ô" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Õ" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ö" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ù" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ú" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Û" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ü" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ý" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "à" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "á" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "â" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ã" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ä" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "å" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "è" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "é" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ê" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ë" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ì" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "í" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "î" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ï" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ñ" set "NewName=%NewName%n" & goto NextChar
if "%Char%" == "ò" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ó" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ô" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "õ" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ö" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ù" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ú" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "û" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ü" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ý" set "NewName=%NewName%y" & goto NextChar
if "%Char%" == "ÿ" set "NewName=%NewName%y" & goto NextChar
set "NewName=%NewName%%Char%" & goto NextChar

:CompareNames
rem Is it necessary to rename the file?
if "%~1" == "%NewName%" goto :EOF

rem Is there already a file with new file name?
if exist "%FolderPath%\%NewName%" goto :EOF

echo Renaming "%~1" to "%NewName%"
ren "%FolderPath%\%~1" "%NewName%"
goto :EOF

This works also with a UNC path.

The subroutine RenameFile is never executed if the specified folder does not exist or is currently not accessible.

Fourth solution:
The folder path is used on command DIR and passed together with the file name to the subroutine which works mainly just with name of file without path:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
set "FolderPath=C:\Temp\Folder with files to rename"
for /F "eol=| delims=" %%I in ('dir "%FolderPath%\*" /A-D-H /B 2^>nul') do call :RenameFile "%FolderPath%\%%I"
endlocal
goto :EOF

:RenameFile
set "FileName=%~nx1"
set "NewName="
:NextChar
if not defined FileName goto CompareNames
set "Char=%FileName:~0,1%"
set "FileName=%FileName:~1%"
if "%Char%" == "Š" set "NewName=%NewName%S" & goto NextChar
if "%Char%" == "Ž" set "NewName=%NewName%Z" & goto NextChar
if "%Char%" == "š" set "NewName=%NewName%s" & goto NextChar
if "%Char%" == "ž" set "NewName=%NewName%z" & goto NextChar
if "%Char%" == "Ÿ" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "À" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Á" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Â" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ã" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Ä" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "Å" set "NewName=%NewName%A" & goto NextChar
if "%Char%" == "È" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "É" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ê" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ë" set "NewName=%NewName%E" & goto NextChar
if "%Char%" == "Ì" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Í" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Î" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ï" set "NewName=%NewName%I" & goto NextChar
if "%Char%" == "Ñ" set "NewName=%NewName%N" & goto NextChar
if "%Char%" == "Ò" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ó" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ô" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Õ" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ö" set "NewName=%NewName%O" & goto NextChar
if "%Char%" == "Ù" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ú" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Û" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ü" set "NewName=%NewName%U" & goto NextChar
if "%Char%" == "Ý" set "NewName=%NewName%Y" & goto NextChar
if "%Char%" == "à" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "á" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "â" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ã" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "ä" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "å" set "NewName=%NewName%a" & goto NextChar
if "%Char%" == "è" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "é" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ê" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ë" set "NewName=%NewName%e" & goto NextChar
if "%Char%" == "ì" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "í" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "î" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ï" set "NewName=%NewName%i" & goto NextChar
if "%Char%" == "ñ" set "NewName=%NewName%n" & goto NextChar
if "%Char%" == "ò" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ó" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ô" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "õ" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ö" set "NewName=%NewName%o" & goto NextChar
if "%Char%" == "ù" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ú" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "û" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ü" set "NewName=%NewName%u" & goto NextChar
if "%Char%" == "ý" set "NewName=%NewName%y" & goto NextChar
if "%Char%" == "ÿ" set "NewName=%NewName%y" & goto NextChar
set "NewName=%NewName%%Char%" & goto NextChar

:CompareNames
rem Is it necessary to rename the file?
if "%~nx1" == "%NewName%" goto :EOF

rem Is there already a file with new file name?
if exist "%~dp1%NewName%" goto :EOF

echo Renaming "%~nx1" to "%NewName%"
ren "%~1" "%NewName%"
goto :EOF

This works also with a UNC path.

The subroutine RenameFile is never executed if the specified folder does not exist or is currently not accessible.

Fifth solution:
The folder path is used on command DIR which is executed with option /S to search also in subdirectories of specified directory resulting in output of name of files with full path. Each full qualified file name assigned to loop variable I is passed to the subroutine which works mainly just with name of file without path like in fourth solution:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\chcp.com 1252 >nul
for /F "eol=| delims=" %%I in ('dir "C:\Temp\Folder with files to rename\*" /A-D-H /B /S 2^>nul') do call :RenameFile "%%I"
endlocal
goto :EOF

This works also with a UNC path.

The subroutine RenameFile is never executed if the specified folder does not exist or is currently not accessible.

The subroutine must have the same command lines as in fourth solution.


For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cd /?
  • chcp /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    CMD uses the console codepage when reading from disk files and pipes, which includes a batch script. The console defaults to OEM unless otherwise configured under "HKCU\Console". If you want general Unicode support, switch to codepage 65001 (UTF-8) and save the batch script as UTF-8 without a BOM (byte order mark). Take care with this since the console's support for UTF-8 is buggy, and even for Unicode itself older versions are buggy when using a raster font. I prefer to use ASCII only, except for blocks that temporarily switch to UTF-8 to set environment variables with non-ASCII strings. – Eryk Sun Sep 22 '18 at 14:03