How to convert the value of %USERNAME% to lowercase within a Windows batch script?

后端 未结 9 592
暗喜
暗喜 2020-12-09 17:10

I\'m automating some source control software functionality using a dot bat script but given that our svn repos are hosted in a *NIX box, I\'m facing the eternal case problem

9条回答
  •  执念已碎
    2020-12-09 17:28

    Well, I was browsing for some syntax and stumbled upon this page. I know its old but I thought I'd take a break and give the brain a little kick.

    Here's something a little shorter and manageable. This just "brute forces" all uppercase letters to lowercase letters without regards to whether the actual letter exists in the string or not. Thus the functional loop runs exactly 26 times no matter the length of the string.

    Hope this helps someone.

    @echo off
    cls
    setlocal enabledelayedexpansion
    
    REM ***** Modify as necessary for the string source. *****
    set "_STRING=%*"
    if not defined _STRING set "_STRING=%USERNAME%"
    set _STRING
    REM ***** Modify as necessary for the string source. *****
    
    set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    set "_LCASE=abcdefghijklmnopqrstuvwxyz"
    
    for /l %%a in (0,1,25) do (
       call set "_FROM=%%_UCASE:~%%a,1%%
       call set "_TO=%%_LCASE:~%%a,1%%
       call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
    )
    
    set _STRING
    endlocal
    

    Example:

    E:\OS.ADMIN>LCASE.BAT The Quick Fox Jumps Over The Brown Fence.
    

    Result:

    _STRING=The Quick Fox Jumps Over The Brown Fence.
    _STRING=the quick fox jumps over the brown fence.
    

提交回复
热议问题