首先,您应该了解可以使用不同的代码值保存字符。这称为character encoding。
有些字符编码只使用一个 8 位字节来表示一个字符,最多 2^8 = 256 个字符。但是有很多字符,远远超过 256 个。因此有很多 code pages。代码页定义了哪个字节值代表哪个字符。
Windows 根据配置的国家/地区为图形用户界面 (GUI) 和 GUI 应用程序(如 Windows 记事本)以及控制台应用程序(如 Windows 命令处理器)设置代码页。
对于西欧国家,代码页 Windows-1252 默认设置为每个字符仅编码一个字节的文本。西欧国家控制台上使用的代码页是OEM 850,因为它可以在打开命令提示符窗口并运行不带任何参数的命令chcp 时看到。 chcp 是 change code page 的缩写。
The Unicode Consortium 的成立是为了停止定义越来越多的代码页,请参阅What is Unicode? 该联盟引入了Unicode - universal code 以保持一致世界上大多数书写系统中表达的文本的编码、表示和处理。该联盟还定义了有关如何编码字符和符号的标准。最受欢迎的是UTF-8 和UTF-16。
其次,在了解字符编码之后,让我们看看ã 和é 等字符。
Windows 记事本使用 Windows-1252 保存批处理文件,并在配置了西班牙的 另存为 对话框窗口中使用 ANSI 选项 编码作为国家。 ANSI 在这里表示使用代码页每个字符一个字节,而不是标准化许多代码页的American National Standards Institute。
但 Windows 命令处理器 cmd.exe 解释批处理文件中的行使用代码页 OEM 850。
可以为每个正在运行的进程分别定义字符编码和代码页。您可能已经在 Windows 任务栏上看到了两个字母的语言缩写,它可用于更改当前活动应用程序的语言,即用于非 Unicode 字符编码的代码页。
字符 ã 在代码页 Windows-1252 中具有十进制代码值 227,但在代码页 OEM 850 中具有代码值 198。字符 é 在代码页 Windows-1252 中作为十进制代码值 233,但在代码页 Windows-1252 中具有代码值 130代码页 OEM 850。
那么如何解决这个不同字符编码的问题呢?
一种解决方案是使用代码页 OEM 850 编写批处理文件。使用 Windows 记事本并不容易。其他文本编辑器更好地支持这一点,例如 UltraEdit,我已将其配置为使用 Windows 为控制台应用程序定义的 OEM 代码页自动编辑 *.bat 和 *.cmd 文件,而所有其他非 Unicode 编码的文本文件使用 Windows 代码页定义用于 GUI 应用程序的 Windows。
另一种解决方案是使用代码页 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 路径。
ENDLOCAL命令在执行SETLOCAL时将当前目录改回初始当前目录。
第二种解决方案:
使用命令 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 恢复初始目录并删除驱动器号的映射。
PUSHD 如果使用网络资源的 UNC 路径并且在执行命令时该网络资源当前不可用或指定的目录根本不存在,则可能会失败。
第三种解决方案:
文件夹路径分配给环境变量,并用于命令 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 /?
另见: