在批处理文件中循环通过ASCII代码 [英] Loop through ASCII codes in batch file
问题描述
我想回声A到Z,而不输入 for %% J 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 echo %% J
这么长的和不完整的命令。所以我想知道我是否可以循环通过ASCII代码。像对于%% J在(ASCII的A ie.65到Z)中做的事情做回声%% J
I want to echo A to Z without typing for %%J 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 echo %%J
such long and unfruitful commands out. So I was wondering if i can cycle through ASCII codes. Something like for %%J in (ASCII of A ie.65 to Z) do echo %%J
任何帮助将不胜感激。
推荐答案
令人惊讶的是,有一个解决方案使用未记录的内置环境变量,名为 = ExitCodeAscii
,其中包含ASCII当前退出代码 1的字符( ErrorLevel
):
Surprisingly, there is a solution that makes use of an undocumented built-in environment variable named =ExitCodeAscii
, which holds the ASCII character of the current exit code1 (ErrorLevel
):
@echo off
for /L %%A in (65,1,90) do (
cmd /C exit %%A
call echo %%^=ExitCodeAscii%%
)
代表/ L
循环遍历 A
到 Z
的(十进制)字符代码。 cmd / C exit %% A
设置当前迭代代码的返回代码( ErrorLevel
), c $ c> echo 作为一个字符。 call
与double - %
一起引入命令行的第二个解析阶段,以获取当前值 = ExitCodeAscii
,而不是在执行整个循环之前出现的值一个简单的命令行如
echo%= ExitCodeAscii%
)。或者,也可以使用延迟展开。
The for /L
loop walks through the (decimal) character codes of A
to Z
. cmd /C exit %%A
sets the return code (ErrorLevel
) to the currently iterated code, which is echo
-ed as a character afterwards. call
, together with the double-%
-signs introduce a second parsing phase for the command line in order to get the current value of =ExitCodeAscii
rather than the one present before the entire for /L
loop is executed (this would happen with a simple command line like echo %=ExitCodeAscii%
). Alternatively, delayed expansion could be used also.
基本思路归功于 rojo ,并在此帖子中应用:如何在批处理中获得随机字母输出。
The basic idea is credited to rojo and applied in this post: How do i get a random letter output in batch.
1)退出代码)不一定与 ErrorLevel
值相同。但是,命令行 cmd / C exit 1
将这两个值设置为 1
。要确保退出代码等于 ErrorLevel
,请使用 cmd / C exit%ErrorLevel%
。
1) The exit code (or return code) is not necessarily the same thing as the ErrorLevel
value. However, the command line cmd /C exit 1
sets both values to 1
. To ensure that the exit code equals ErrorLevel
, use something like cmd /C exit %ErrorLevel%
.
这篇关于在批处理文件中循环通过ASCII代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!