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

后端 未结 9 603
暗喜
暗喜 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:32

    :: UPcase.bat ==> Store in environment variable _UPcase_ the upper case of %1
    :: -> Use quotes "" when the first argument has blanks or special characteres
    ::
    :: Adapted from -> http://www.netikka.net/tsneti/info/tscmd039.htm
    ::
    :: Note that the substitution method is case insensitive, which means that
    :: while working for this application, it is not useful for all character
    :: substitution tasks.
    ::
    :: More concisely, one can capitalize (if you pardon the pun) on the fact
    :: that in for and the substitution lower and upper case source are
    :: equivalent.
    @echo off
    
    :: %~1 -> removes quotes from the first command line argument
    :: http://steve-jansen.github.io/guides/windows-batch-scripting/part-2-variables.html
    @echo off
    ::setlocal EnableExtensions
        :: echo %_UPcase_%
        call :ToUpcaseWithFor "%~1" _UPcase_
        :: echo %_UPcase_% _doit_1_
    ::endlocal & goto :EOF
    goto :EOF
    ::
    :: ======================
    :ToUpcaseWithFor
    setlocal EnableExtensions EnableDelayedExpansion
      set var_=%~1
      for %%c 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 var_=!var_:%%c=%%c!
      )
    endlocal & set %2=%var_%& goto :EOF
    
    :EOF
    :: UPcase.bat ==> EOF
    

提交回复
热议问题