Loop through ASCII codes in batch file

后端 未结 5 1933
囚心锁ツ
囚心锁ツ 2021-01-04 17:46

How do I loop through ASCII values (here, alphabets) and work on them?

I want to echo A to Z without having to type every character manually like for %%J in (

相关标签:
5条回答
  • 2021-01-04 18:29

    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
    
    0 讨论(0)
  • 2021-01-04 18:29

    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));
    
    0 讨论(0)
  • 2021-01-04 18:39

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

    0 讨论(0)
  • 2021-01-04 18:42

    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"
    
    0 讨论(0)
  • 2021-01-04 18:50

    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.

    0 讨论(0)
提交回复
热议问题