Windows 10 x64 系统
我制作了一个批处理文件,用于检查文件夹内的音频文件扩展名。
当我运行在 IF 语句之外发送警报消息的命令时,消息显示没有错误:
IF 语句之外的命令:
@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
for /F %d in ('echo prompt $E ^| cmd') do (set "ESC=%d")
echo %ESC%[91mATTENTION, file extension WAV not found! Do you want to look for another file? (y=return/n=exit)%ESC%[0m
set /p r=
IF /i "%r%"=="n" goto searchfile
goto exit
IF 语句内的命令:
@ECHO OFF
IF NOT EXIST "C:\Users\%username%\Desktop\Audios Files\WAV" (
echo.-------------------------------------------------------------------------------------------------------------------------
echo. WAV extension not found or there was some error in the Audio
echo.-------------------------------------------------------------------------------------------------------------------------
SETLOCAL EnableExtensions DisableDelayedExpansion
for /F %d in ('echo prompt $E ^| cmd') do (set "ESC=%d")
echo %ESC%[91mExtension not found or in error! Do you want to look for another file? (y=return/n=exit)%ESC%[0m
set /p r=""
IF /i "%r%"=="s" goto EXT
goto notfound
)
:notfound
exit
IF 语句内的输出命令:
IF NOT EXIST "C:\Users\%username%\Desktop\Files\WAV" (
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? echo. ATTENTION, file extension WAV not found!
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? @echo off
More? SETLOCAL EnableExtensions DisableDelayedExpansion
More? for /F %d in ('echo prompt $E ^| cmd') do (set "ESC=%d")
Mais? echo %ESC%[91mATTENTION, file not found! Do you want to look for another file? (y=return/n=exit)%ESC%[0m
%ESC%[0m it was unexpected at this time.
命令执行时的错误信息:
%ESC%[0m it was unexpected at this time.
答案1
SETLOCAL
:设置选项以控制环境变量的可见性在批处理文件中。没有效果cmd
……ECHO OFF
:没有效果cmd
……- 你需要转义某些字符;
ECHO
特别是所有右括号…… %ECHO%
变量在解析时被扩展(就像所有其他百分比扩展的变量一样)。声明并定义它前在括号命令内使用,如if
。
使用转义的右括号:
set "ESC="
IF NOT EXIST "C:\Users\%username%\Desktop\Files\WAV" (
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? echo. ATTENTION, file extension WAV not found!
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? REM we need to escape closing parenthesis ↓
More? echo %ESC%[91mATTENTION, … (y=return/n=exit^)%ESC%[0m
More? )
------------------------------------------------------------------------------------------------------------------------- ATTENTION, file extension WAV not found! ------------------------------------------------------------------------------------------------------------------------- %ESC%[91mATTENTION, … (y=return/n=exit)%ESC%[0m
使用正确声明和定义的ECHO
变量:ANSI 转义序列中的文本ATTENTION, … (y=return/n=exit)
以红色显示:
for /F %d in ('echo prompt $E ^| cmd') do (set "ESC=%d")
IF NOT EXIST "C:\Users\%username%\Desktop\Files\WAV" (
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? echo. ATTENTION, file extension WAV not found!
More? echo.-------------------------------------------------------------------------------------------------------------------------
More? REM we need to escape closing parenthesis ↓
More? echo %ESC%[91mATTENTION, … (y=return/n=exit^)%ESC%[0m
More? )
------------------------------------------------------------------------------------------------------------------------- ATTENTION, file extension WAV not found! ------------------------------------------------------------------------------------------------------------------------- ATTENTION, … (y=return/n=exit)
编辑。 当然,在批处理脚本中:
for /F %%d in ('echo prompt $E ^| cmd') do (set "ESC=%%d")
答案2
纠正答案。我删除了 y=return/n=exit 中的括号,警告消息不再出现。
现在,只有当命令位于 If 中时,消息颜色不会改变。我会检查一下。
编辑:
消息中的颜色问题也已通过用户@JosefZ 在这篇文章中的回答得到解决。