Batch - Converting variable to uppercase

前端 未结 8 1669
一向
一向 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:12

    This is pointless since env var names and file names are both case insensitive. But if you insist you can do it like this:

    @echo off
    
    setlocal enabledelayedexpansion
    set lower=john smith 123
    set alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZ
    set i=0
    :letter
    set !alpha:~%i%,1!_=!alpha:~%i%,1!
    set /a i += 1
    if %i% LSS 26 goto letter
    set i=0
    set upper=
    :uppercase
    set let=!lower:~%i%,1!
    if "%let%" == "" goto done
    if defined %let%_ (set upper=%upper%!%let%_!) else (set upper=%upper%%let%)
    set /a i += 1
    goto uppercase
    :done
    echo %upper%
    
    0 讨论(0)
  • 2020-12-02 02:12

    I used 2 files, in the batch I called an external js and in the bat I assign the response to an internal var:

    Bat:

    @echo off
    for /f %%i in ('cscript //nologo //E:jscript jsfile.js %1') do set VAR=%%i
    echo %Var%
    

    Js (jsfile.js in same directory):

    WScript.echo(WSH.Arguments(0).toUpperCase())
    
    0 讨论(0)
  • 2020-12-02 02:22

    Thanks for the responses guys, I solved it using this -

    if not defined %~1 EXIT /b
    for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
            "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
            "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
            "ö=Ö" "ü=Ü") do (
    call set %~1=%%%~1:%%~a%%
    )
    EXIT /b
    

    I'm sure your responses are far neater and more efficient, but as mine is doing the trick and I don't want to break anything I will leave it as is!

    Thank you for your input!

    0 讨论(0)
  • 2020-12-02 02:26

    The shortest way (without requiring 3rd party downloads) would be to use PowerShell.

    set "str=The quick brown fox"
    for /f "usebackq delims=" %%I in (`powershell "\"%str%\".toUpper()"`) do set "upper=%%~I"
    

    A faster way but still using less code than any pure batch solution would be to employ WSH.

    @if (@CodeSection == @Batch) @then
    @echo off & setlocal
    
    set "str=The quick brown fox"
    for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%str%"') do set "upper=%%~I"
    set upper
    goto :EOF
    
    @end // end Batch / begin JScript hybrid
    WSH.Echo(WSH.Arguments(0).toUpperCase());
    

    And of course, you can easily make either a function so you can call it multiple times as needed.

    @if (@CodeSection == @Batch) @then
    @echo off & setlocal
    
    call :toUpper upper1 "The quick brown fox"
    call :toUpper upper2 "jumps over the lazy dog."
    set upper
    goto :EOF
    
    :toUpper <return_var> <str>
    for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%~2"') do set "%~1=%%~I"
    goto :EOF
    
    @end // end Batch / begin JScript hybrid
    WSH.Echo(WSH.Arguments(0).toUpperCase());
    

    Or if you want to be really hacksy about it, you could abuse the tree command's error message like this:

    @echo off & setlocal
    
    set upper=
    set "str=Make me all uppercase!"
    for /f "skip=2 delims=" %%I in ('tree "\%str%"') do if not defined upper set "upper=%%~I"
    set "upper=%upper:~3%"
    echo %upper%
    
    0 讨论(0)
  • 2020-12-02 02:26

    I've expanded upon the great answer by the OP and made it into a function that can be called throughout the batch file.

    @echo off
    
    set "str=Make meeeee all uppercaseeeee!"
    echo %str% (Old variable)
    call :TOUPPERCASE str
    echo %str%
    goto :eof
    
    rem FUNCTIONS AT VERY BOTTOM
    :TOUPPERCASE
    if not defined %~1 exit /b
    for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä" "ö=Ö" "ü=Ü") do (
    call set %~1=%%%~1:%%~a%%
    )
    goto :eof
    

    Result

    Make meeeee all uppercaseeeee! (Old variable)
    MAKE MEEEEE ALL UPPERCASEEEEE!
    
    0 讨论(0)
  • 2020-12-02 02:30
    @ECHO Off
    SETLOCAL
    SET "betabet=abcdefghijklmnopqrstuvwxyz1234567890!*$^&^^+=-\^|^>;'.,/?^<"
    ECHO %betabet%
    >u:\betabet.file ECHO %betabet%
    CALL :upper betabet
    ECHO %betabet%
    CALL :upcase u:\betabet.file 
    GOTO :EOF
    
    :upper
    FOR %%a 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 CALL SET "%1=%%%1:%%a=%%a%%%"
    GOTO :EOF
    
    :upcase
    setlocal EnableDelayedExpansion
    for /F "delims=" %%a in (%1) do (
       set "line=%%a"
       for %%b 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 "line=!line:%%b=%%b!"
       )
       echo !line!
    )
    endlocal
    GOTO :eof
    

    The method referred to by Aacini at Windows batch file read text file and convert all to uppercase fails for some characters, as is demonstrated by the above batch.

    This batch will also fail if the string in question contains certain characters (eg %:) - it will convert the alphas, but delete % and :.

    The above batch first establishes a string containing miscellaneous characters, displays it, saves it as a file, then converts it using :upper.

    For comparison, the file is then procesed using the :upcase function (derived from the linked response).


    Following Aacini's valid comment and further investigation, here are some techniques (including a demo of the 'read-from-file' method)

    The :showline routine exists to shorten the code by displaying line in delayedexpansion mode

    @ECHO Off
    SETLOCAL
    set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
    SETLOCAL enabledelayedexpansion
    >u:\betabet.file ECHO "!line!"
    endlocal
    CALL :showline
    ECHO --- show conversion using "upper - caret disappears
    CALL :upper line
    CALL :showline
    ECHO ------------------------------
    ECHO --- show actual file contents ^& data read from file
    type u:\betabet.file 
    CALL :upcase u:\betabet.file 
    ECHO ------------------------------
    set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
    CALL :showline
    ECHO --- show conversion using "inline-conversion - caret PRESERVED
    setlocal ENABLEDELAYEDEXPANSION
    for %%b 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 "line=!line:%%b=%%b!"
    echo "!line!"   ^<--- in inline conversion
    endlocal&SET "line=%line:^=^^%"
    CALL :showline
    ECHO ------------------------------
    set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
    CALL :showline
    ECHO --- show conversion using "upcase2 - caret disappears
    CALL :upcase2 line
    CALL :showline
    GOTO :EOF
    
    :upper
    FOR %%a 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 CALL SET "%1=%%%1:%%a=%%a%%%"
    GOTO :EOF
    
    :upcase
    setlocal EnableDelayedExpansion
    for /F "delims=" %%a in (%1) do (
     ECHO read%%a
       set "line=%%a"
       for %%b 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 "line=!line:%%b=%%b!"
       )
       echo conv!line!
    )
    endlocal
    GOTO :eof
    
    :upcase2
    setlocal ENABLEDELAYEDEXPANSION
    for %%b 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 "%1=!%1:%%b=%%b!"
    endlocal&CALL SET "%1=%%%1%%"
    GOTO :eof
    
    :showline
    SETLOCAL enabledelayedexpansion
    ECHO "!line!"
    endlocal
    GOTO :eof
    
    0 讨论(0)
提交回复
热议问题