Loop through ASCII codes in batch file

2019-04-06 04:34发布

问题:

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

Any help would be appreciated.

回答1:

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%%
)

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.

The basic idea is credited to rojo and applied in this post: How do i get a random letter output in batch.

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%.



回答2:

Loop through ASCII codes in batch file

Any batch solution to do what you want would be much more complex than what you already have.

Consider using PowerShell instead:

for($i=65;$i -le 90; $i++){[char]$i}

Example output:

PS F:\test> for($i=65;$i -le 90; $i++){[char]$i}
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


回答3:

This answer is a compilation and comparison of the methods given before.

In the question the OP requested to echo A to Z letters in a batch file. If the purpose of the solution is not just show letters, but process the letters in any other way, then the =ExitCodeAscii variable method is the only one that do that with Batch code, although the modification required in the other two methods to do the same is simple.

The code below include the three methods and compare they in the simplest possible way: via the time required by each one to complete.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set "start=%time%"
for /L %%a in (65,1,90) do (
    cmd /C exit %%a
    call echo Batch: %%^=ExitCodeAscii%%
)
echo First method: using =ExitCodeAscii variable
echo Start: %start%
echo End:   %time%
echo/

set "start=%time%"
for /F %%a in ('powershell "65..90 | %%{ [char]$_ }"') do echo PS: %%a
echo Second method: using PowerShell
echo Start: %start%
echo End:   %time%
echo/

set "start=%time%"
for /F %%a in ('cscript //nologo //E:JScript "%~F0"') do echo JScript: %%a
echo Third method: using JScript
echo Start: %start%
echo End:   %time%

goto :EOF

@end

for (var i=65; i<=90; i++) WSH.Echo(String.fromCharCode(i));

Several executions of this code show consistent results: the PowerShell method is the slowest one, the =ExitCodeAscii method run 280% faster than PowerShell, and JScript method run 240% faster than =ExitCodeAscii. These differences would diminish as the whole program grow and perform more things than just show the letters, but in standard/small Batch files, this relation will always be the same: PowerShell is the slowest method and JScript the fastest one. The VBScript method is similar to JScript.



回答4:

In Vbscript, you can do something like this :

For i = 65 To 90
    Car = Car & Chr(i) & vbTab
Next
wscript.echo Car

And you can generate it with a batch file like this :

@echo off
Call :vbs
Pause
exit /b

:vbs
(
    echo For i = 65 To 90
    echo    Car = Car ^& Chr(i^) ^& vbTab
    echo Next
    echo wscript.echo Car
)>"%tmp%\%~n0.vbs"
Cscript /NoLogo "%tmp%\%~n0.vbs"


回答5:

The simplest solution is to include this PowerShell one-liner in your bat script:

powershell "[char[]](65..90)"

It's not necessarily the fastest, though.


Here's a VBScript solution similar to Hackoo's, but in a hybrid format not relying on writing an external .vbs file. Save it with a .bat extension. The cscript line is what triggers execution of the VBScript hybrid code.

<!-- : batch portion
@echo off & setlocal

cscript /nologo "%~f0?.wsf"
goto :EOF

: VBScript -->
<job>
    <script language="VBScript">
        For i = 65 to 90
            WSH.Echo(Chr(i))
        Next
    </script>
</job>

Or if you're more comfortable with JavaScript syntax, here's a Batch + JScript hybrid.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0"
goto :EOF

@end // end Batch / begin JScript

for (var i=65; i<=90; i++) WSH.Echo(String.fromCharCode(i));


回答6:

awk provides the functionality.

asc () { awk -v n=$1 'BEGIN { printf "%c", n }'; }

for n in $(seq 65 90)
do
    echo $(asc $n)
done

If we have bash shell available, we could alternatively use a couple of printf commands to convert numbers to ascii:

asc () { bash -c "printf '%b\n' $(printf '\\\x%x' $1)"; }

for n in $(seq 65 90)
do
    echo $(asc $n)
done


回答7:

This little gem comes to mind as it is not obfuscated and reasonably performant:

@set _ASCII=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

@for %%J in (%_ASCII%) do @echo %%J

Separate the data from the program flow logic and it reads better. Run time is under a microsecond on my system, about one order of magnitude faster than the other schemes.

The only type casting that Windows Batch scripts can do is when it momentarily promotes strings to integer and then back to string again.