如何从文件名中的字符中删除西班牙口音?

时间:2018-09-21 18:53:47

标签: windows file batch-file rename

第一种情况:

我创建了一个重命名文本文件ren.bat。该文件的内容是这样的:

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

当我运行ren.bat文件时,该命令将重命名该文件夹中的所有PDF文件,但是整个文件名中只有第一个字母a不会替换字母a

谁能告诉我怎么了?

第二种情况:

当我运行命令时:

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

该命令不会重命名第一个字母。

谁能告诉我怎么了?

我发现一个有效的FOR代码。但是我不知道如何适应它,这样我就可以读取文件夹中的所有文件,而不必识别相关文件名。您要创建的是一个.bat文件,该文件通过删除带有缩进的重音来重命名文件。请注意,此代码必须指定文件名:

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

我需要这样的东西,但是那没用:

ren "áéí*.txt" "aei*.txt"

1 个答案:

答案 0 :(得分:3)

首先,您应该了解可以使用不同的代码值保存字符。这称为character encoding

有些字符编码上仅使用一个8位字节来表示最大2 ^ 8 = 256个字符的字符。但是有很多字符,远远超过256个。因此,有许多code pages。代码页定义哪个字节值代表哪个字符。

Windows根据配置的国家/地区为图形用户界面(GUI)和GUI应用程序(如Windows记事本)以及控制台应用程序(如Windows命令处理器)设置代码页。

对于西欧国家/地区,默认情况下将代码页Windows-1252设置为每个字符仅编码一个字节的文本。西欧国家/地区控制台上使用的代码页为OEM 850,因为打开命令提示符窗口并运行不带任何参数的命令chcp可以看到它。 chcp ch ange c ode p age的缩写。

The Unicode Consortium的形成是为了停止定义越来越多的代码页,请参阅What is Unicode?。这个联盟引入了Unicode uni 通用的代码用于在世界上大多数书写系统中表达的文本进行一致的编码,表示和处理。该联盟还定义了有关如何编码字符和符号的标准。最受欢迎的是UTF-8UTF-16


第二,在了解字符编码之后,我们来看一下ãé之类的字符。

Windows记事本使用Windows-1252在配置了西班牙的另存为对话框窗口中使用 ANSI 选项 Encoding 来保存批处理文件。作为国家。 ANSI在这里表示使用代码页每个字符一个字节,而不是American National Standards Institute来标准化许多代码页。

但是Windows命令处理器cmd.exe解释批处理文件中的行使用代码页OEM 850。

可以为每个正在运行的进程分别定义字符编码和代码页。您可能已经在Windows任务栏上看到了两个字母的语言缩写,可用于更改当前活动应用程序的语言,即用于非Unicode字符编码的代码页。

字符ã在Windows 1252的代码页中具有十进制代码值227,但在代码页OEM 850中的代码值为198。字符é在Windows 1252的代码页中作为十进制的代码值233,但是代码页OEM850中的代码值130。

那么如何解决不同字符编码的问题?

一种解决方案是使用代码页OEM 850编写批处理文件。使用Windows记事本确实不那么容易。其他文本编辑器对此提供了更好的支持,例如UltraEdit,我已配置为使用Windows为控制台应用程序定义的OEM代码页自动编辑* .bat和* .cmd文件,而所有其他非Unicode编码的Windows代码页定义的非Unicode文本文件则由Windows for GUI应用程序。

另一种解决方案是使用Windows-1252代码页编写批处理文件,并在执行其他命令行之前先使用命令chcp 1252将执行批处理文件时的代码页更改为该代码页。对于大多数用户来说,这是更简单的解决方案。


那使用字符串替换的批处理代码呢?

@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

嗯,看起来很有希望。但有一个问题。 Windows命令处理器执行的字符串替换始终不区分大小写。这意味着Šš在第一个字符串替换命令行上均被s替换。

但是以下批处理文件使字符替换区分大小写。

@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

与使用批处理文件的Windows命令处理器执行此文件重命名任务相比,使用任何其他编程或脚本语言会更好,因为它非常慢并且在文件名包含带有代码的Unicode字符的情况下仍然容易出错值在Windows-1252代码页中不可用。

批处理文件会使用 IF 命令针对文件名中的每个字符运行很多区分大小写的字符串比较,这使得该解决方案对于当前目录中的许多文件名而言非常慢。

当然可以使用 FOR 循环来优化此代码,以减少命令行的使用量,但是我认为这样做并不会减少所有区分大小写的文件重命名所需的时间。


有五种解决方案可以使文件在特定文件夹而不是当前目录上重命名。

第一个解决方案:
使用命令 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

与上面的代码相比,子例程RenameFile中没有任何更改。

默认情况下,此解决方案不适用于UNC路径,除非修改了特殊的注册表值,并且使用的Windows版本完全支持此注册表值。但是,不建议更改此注册表值,请参见下面的更好的解决方案,也可以使用UNC路径。

执行 SETLOCAL 后,命令 ENDLOCAL 将当前目录更改回初始当前目录。

第二个解决方案:
使用命令 PUSHD 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

与上面的代码相比,子例程RenameFile中没有任何更改。

此解决方案还可以使用UNC路径,因为命令 PUSHD 将驱动器号映射到网络资源,并将带有该驱动器号的目录作为当前目录。 POPD 命令将还原初始目录并删除驱动器号的映射。

如果使用网络资源的UNC路径并且该网络资源当前在执行命令时不可用或指定的目录根本不存在,则

PUSHD 可能会失败。

第三种解决方案:
文件夹路径已分配给环境变量,并用于命令 DIR REN 以及新文件存在检查:

@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

这也适用于UNC路径。

如果指定的文件夹不存在或当前不可访问,则子例程RenameFile永远不会执行。

第四种解决方案:
文件夹路径用在命令 DIR 上,并与文件名一起传递给子例程,该子例程主要与没有路径的文件名一起起作用:

@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

这也适用于UNC路径。

如果指定的文件夹不存在或当前不可访问,则子例程RenameFile永远不会执行。

第五种解决方案:
文件夹路径在命令 DIR 上使用,该命令与选项/S一起执行,还可以在指定目录的子目录中搜索,从而输出具有完整路径的文件名。分配给循环变量I的每个完全合格的文件名都传递给子例程,该子例程主要与没有路径的文件名一起工作,如第四种解决方案:

@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

这也适用于UNC路径。

如果指定的文件夹不存在或当前不可访问,则子例程RenameFile永远不会执行。

该子例程必须具有与第四种解决方案相同的命令行。


要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

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

另请参阅: