Batch - Converting variable to uppercase

前端 未结 8 1670
一向
一向 2020-12-02 01:39

How would I go about changing the destl variable to uppercase before it is used. I assume some sort of character swap, however I couldn\'t get it working. C

相关标签:
8条回答
  • 2020-12-02 02:36

    If you have any POSIX utilities installed on Windows, like many developers (e.g., GNUWin utilities at https://sourceforge.net/projects/gnuwin32/)

    You could use the tr.exe utility, which has: [:upper:]

    @echo off
    SETLOCAL EnableDelayedExpansion
    echo Before: SCEINST is: %SCEINST%
    set upper=
    for /f "usebackq delims=" %%g in (`echo %SCEINST% ^| tr [:lower:] [:upper:]`) do (
        SET upper=%%g
        :: get rid of any leading whitespace
        set upper=!upper: =!
        goto :done
    )
    :done
    echo After: SCEINST (in upper) is now: !upper!
    

    ----------OUTPUT----------

    Before: SCEINST is: scprd
    After: SCEINST (in upper) is now: SCPRD
    
    0 讨论(0)
  • 2020-12-02 02:39

    This is probably the fastest way to convert string to upper case using batch file - it uses a macro ,it stores only the upper case letters in the loop using the way of how batch replaces strings and stores the produced string in a variable called result:

    @echo off
    
    set UpperCaseMacro=for /L %%n in (1 1 2) do if %%n==2 (for %%# 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 set "result=!result:%%#=%%#!") else setlocal enableDelayedExpansion ^& set result=
    
    set "string=SOme STrinG WiTH lowerCAse letterS and UPCase leTTErs"
    %UpperCaseMacro%%string%
    
    echo %result%
    
    0 讨论(0)
提交回复
热议问题