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.
In Vbscript, you can do something like this :
And you can generate it with a batch file like this :
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:
Example output:
The simplest solution is to include this PowerShell one-liner in your bat script:
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.Or if you're more comfortable with JavaScript syntax, here's a Batch + JScript hybrid.
awk provides the functionality.
If we have bash shell available, we could alternatively use a couple of
printf
commands to convert numbers to ascii: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
):The
for /L
loop walks through the (decimal) character codes ofA
toZ
.cmd /C exit %%A
sets the return code (ErrorLevel
) to the currently iterated code, which isecho
-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 entirefor /L
loop is executed (this would happen with a simple command line likeecho %=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 linecmd /C exit 1
sets both values to1
. To ensure that the exit code equalsErrorLevel
, use something likecmd /C exit %ErrorLevel%
.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.
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.