@ECHO Off
SETLOCAL
SET "betabet=abcdefghijklmnopqrstuvwxyz1234567890!*$^&^^+=-\^|^>;'.,/?^<"
ECHO %betabet%
>u:\betabet.file ECHO %betabet%
CALL :upper betabet
ECHO %betabet%
CALL :upcase u:\betabet.file
GOTO :EOF
:upper
FOR %%a IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO CALL SET "%1=%%%1:%%a=%%a%%%"
GOTO :EOF
:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
set "line=%%a"
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "line=!line:%%b=%%b!"
)
echo !line!
)
endlocal
GOTO :eof
Aacini在Windows批处理文件读取文本文件并全部转换为大写的方法中提到的方法对于某些字符失败,在上述批处理程序中有所证明。
如果字符串中包含某些字符(例如%:
),则此批处理程序也会失败-它将转换字母,但删除%
和:
。
上述批处理程序首先建立一个包含杂项字符的字符串,显示它,保存它为一个文件,然后使用:upper
进行转换。
为了比较,该文件随后使用来自链接响应的:upcase
函数进行处理。
根据Aacini的有效评论和进一步调查,这里介绍一些技术(包括“从文件读取”的演示)
:showline
例程存在以在delayedexpansion模式下显示line
来缩短代码。
@ECHO Off
SETLOCAL
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
SETLOCAL enabledelayedexpansion
>u:\betabet.file ECHO "!line!"
endlocal
CALL :showline
ECHO --- show conversion using "upper - caret disappears
CALL :upper line
CALL :showline
ECHO ------------------------------
ECHO --- show actual file contents ^& data read from file
type u:\betabet.file
CALL :upcase u:\betabet.file
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "inline-conversion - caret PRESERVED
setlocal ENABLEDELAYEDEXPANSION
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set "line=!line:%%b=%%b!"
echo "!line!" ^<--- in inline conversion
endlocal&SET "line=%line:^=^^%"
CALL :showline
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "upcase2 - caret disappears
CALL :upcase2 line
CALL :showline
GOTO :EOF
:upper
FOR %%a IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO CALL SET "%1=%%%1:%%a=%%a%%%"
GOTO :EOF
:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
ECHO read%%a
set "line=%%a"
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "line=!line:%%b=%%b!"
)
echo conv!line!
)
endlocal
GOTO :eof
:upcase2
setlocal ENABLEDELAYEDEXPANSION
for %%b in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set "%1=!%1:%%b=%%b!"
endlocal&CALL SET "%1=%%%1%%"
GOTO :eof
:showline
SETLOCAL enabledelayedexpansion
ECHO "!line!"
endlocal
GOTO :eof