无法使用 colorEcho 批量正确打印 ä、ç 或 ã
Can't get the ä, ç, or ã to print correctly using colorEcho in batch
所以我想用黄色打印 Jäger 炸弹和 maçã,而该行的其余部分是标准白色。我的代码是:
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :colorEcho 0e "Jäger bomb"
echo Jäger bomb
call :colorEcho 0e "maçã"
echo maçã
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
问题是,当它输出时,输出为“J,,ger
”,并给出错误提示“FINDSTR: Cannot open maøa
”。但是使用标准的 echo 命令都可以正常打印。是什么原因造成的,我该如何解决?非常感谢任何帮助!
该错误是 FINDSTR 命令限制的结果,如 所述。这是广泛答案的相关部分,最重要的部分以 粗体斜体显示
Character limits for command line parameters - Extended ASCII transformation
The null character (0x00) cannot appear in any string
on the command line. Any other single byte character can appear in the
string (0x01 - 0xFF). However, FINDSTR converts many extended ASCII
characters it finds within command line parameters into other
characters. This has a major impact in two ways:
1) Many extended ASCII characters will not match themselves if used as
a search string on the command line. This limitation is the same for
literal and regex searches. If a search string must contain extended
ASCII, then the /G:FILE
option should be used instead.
2) FINDSTR may fail to find a file if the name contains extended ASCII characters and the file name is specified on the command line.
If a file to be searched contains extended ASCII in the name, then the
/F:FILE
option should be used instead.
所以可以通过将文件名写入文件并使用/F:file
选项来解决问题。
请注意,无需为每次调用都编写一个带有退格符的新文件。如果将字符串视为文件夹,则可以使用名为 @ 的常量文件,然后附加 \..\@
。您的 @ 文件内容应包含 6 个退格字符以返回所需的字符串。
"files.txt" 文件中的文件路径不能用引号引起来。如果您的字符串包含 &
这样的有害字符,这可能会导致问题。所以我将字符串存储在引号内的变量中,然后使用不带引号的延迟扩展来保留毒字符。
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in (
'"prompt #$H#$E# & echo on & for %%b in (1) do rem"'
) do (
set "DEL=%%a"
)
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%" > "@"
call :colorEcho 0e "Jäger bomb"
echo(
call :colorEcho 0e "maçã"
echo(
call :colorEcho 0e "this & that"
echo(
pause
exit /b
:colorEcho
setlocal
set "str=%~2"
>files.txt echo !str!\..\@
findstr /v /a:%1 /f:files.txt /r "^$"
这段代码解决了您眼前的问题,但代码仍然不是很健壮。不用想太多,我知道会导致它失败的 3 件事
\
字符串中的任意位置
/
字符串中的任意位置
:
第2位
我确定还有许多其他潜在问题。
您可以在 How to have multiple colors in a Windows batch file? 找到更强大的代码。
查看所有答案,因为主题有很多变化。
你可以选择你最喜欢的:-)
您需要在批处理脚本的开头添加 chcp 命令,告诉您脚本文件编码的代码页。
如果您将脚本保存为 utf-8 添加:chcp 65001 > nul
如果您将脚本保存为 ansi 添加:chcp 1252 > nul
cmd 需要使用声明的代码页而不是系统代码页来解释批处理文件的扩展字符。
(用notepad++保存下一个脚本):
它产生:
@Echo Off
::This script file is encoded using utf-8
chcp 65001 >nul
::
::Call :Color A "######" \n E "" C " 23 " E "!" \n B "#&calc" \n
Call :Color 0e "Jäger bomb" \n
call :Color 0e "maçã" \n
Pause >Nul
Exit /B
:Color
:: v23c
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color
所以我想用黄色打印 Jäger 炸弹和 maçã,而该行的其余部分是标准白色。我的代码是:
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :colorEcho 0e "Jäger bomb"
echo Jäger bomb
call :colorEcho 0e "maçã"
echo maçã
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
问题是,当它输出时,输出为“J,,ger
”,并给出错误提示“FINDSTR: Cannot open maøa
”。但是使用标准的 echo 命令都可以正常打印。是什么原因造成的,我该如何解决?非常感谢任何帮助!
该错误是 FINDSTR 命令限制的结果,如 所述。这是广泛答案的相关部分,最重要的部分以 粗体斜体显示
Character limits for command line parameters - Extended ASCII transformation
The null character (0x00) cannot appear in any string on the command line. Any other single byte character can appear in the string (0x01 - 0xFF). However, FINDSTR converts many extended ASCII characters it finds within command line parameters into other characters. This has a major impact in two ways:
1) Many extended ASCII characters will not match themselves if used as a search string on the command line. This limitation is the same for literal and regex searches. If a search string must contain extended ASCII, then the
/G:FILE
option should be used instead.2) FINDSTR may fail to find a file if the name contains extended ASCII characters and the file name is specified on the command line. If a file to be searched contains extended ASCII in the name, then the
/F:FILE
option should be used instead.
所以可以通过将文件名写入文件并使用/F:file
选项来解决问题。
请注意,无需为每次调用都编写一个带有退格符的新文件。如果将字符串视为文件夹,则可以使用名为 @ 的常量文件,然后附加 \..\@
。您的 @ 文件内容应包含 6 个退格字符以返回所需的字符串。
"files.txt" 文件中的文件路径不能用引号引起来。如果您的字符串包含 &
这样的有害字符,这可能会导致问题。所以我将字符串存储在引号内的变量中,然后使用不带引号的延迟扩展来保留毒字符。
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in (
'"prompt #$H#$E# & echo on & for %%b in (1) do rem"'
) do (
set "DEL=%%a"
)
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%" > "@"
call :colorEcho 0e "Jäger bomb"
echo(
call :colorEcho 0e "maçã"
echo(
call :colorEcho 0e "this & that"
echo(
pause
exit /b
:colorEcho
setlocal
set "str=%~2"
>files.txt echo !str!\..\@
findstr /v /a:%1 /f:files.txt /r "^$"
这段代码解决了您眼前的问题,但代码仍然不是很健壮。不用想太多,我知道会导致它失败的 3 件事
\
字符串中的任意位置/
字符串中的任意位置:
第2位
我确定还有许多其他潜在问题。
您可以在 How to have multiple colors in a Windows batch file? 找到更强大的代码。
查看所有答案,因为主题有很多变化。
你可以选择你最喜欢的:-)
您需要在批处理脚本的开头添加 chcp 命令,告诉您脚本文件编码的代码页。
如果您将脚本保存为 utf-8 添加:chcp 65001 > nul
如果您将脚本保存为 ansi 添加:chcp 1252 > nul
cmd 需要使用声明的代码页而不是系统代码页来解释批处理文件的扩展字符。
(用notepad++保存下一个脚本):
它产生:
@Echo Off
::This script file is encoded using utf-8
chcp 65001 >nul
::
::Call :Color A "######" \n E "" C " 23 " E "!" \n B "#&calc" \n
Call :Color 0e "Jäger bomb" \n
call :Color 0e "maçã" \n
Pause >Nul
Exit /B
:Color
:: v23c
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color