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

后端 未结 9 572
暗喜
暗喜 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
    
    0 讨论(0)
  • 2020-12-09 17:38

    In my batch file I'm doing a comparsion between %USERNAME% and a CSV file.

    The program would not work if user was logged in UperCase username.

    Ex:
    Login : GB2NOGU // Won't work
    Login : gb2nogu // Works

    Here I could solve my problem doing a insensitive comparison.

    if /i %USERNAME%==gb2nogu (
         // Code here
    )
    

    The parameter /i tells the cmd to do a insensitive case comparison, so it'll ignore the difference between lowercase and uppercase letters.

    0 讨论(0)
  • 2020-12-09 17:41

    This is the same answer /by @It Wasn't Me here


    For a predictive character set, this substring Set !var:A=a! works, and only working with predefined substring in bat/cmd.

    For this type of task, why not get a little help with c#, which can make it possible to work with unconventional accents and consonants è, È, ä, Ä, ñ, Ñ, ç, Ç etc.

    Where the bat/cmd will generate c# code, it will be compiled and executed at run time ....

    Which solves possible user inputs, in which the sequence comes with accents and the vowels/consonants are different from the conventional ones [a-z] and/or [A_Z]


    @echo off & setlocal EnableDelayedExpansion 
    
    cd /d "%~dp0" && title <nul && title ...\%~dpnx0 /// !time:~0,8! !date!
    
    if exist "%tmp%\ToUpLower.cs" 2>nul >nul del /q /f "%tmp%\ToUpLower.cs" 
    set "_where=%__appdir__%where.exe" && set "_csc=%windir%\Microsoft.NET"
    >"%temp%\ToUpLower.cs"  ( 
    echo= using System; namespace SUQ1522019 ^{class Program ^{static void Main(string[] args^) ^{
    echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-l"^) ^{Console.WriteLine(args[1].ToLower(^)^);^} 
    echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-u"^) ^{Console.WriteLine(args[1].ToUpper(^)^);^}^}^}^}
    )
    
    set "_arg=/t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu "
    for /f tokens^=* %%i in ('!_where! /r "!_csc!" "csc.exe"^|findstr /lic:"k\v2\." 
    ')do "%%~i" !_arg! /unsafe+ /w:0 /o /nologo
    
    for /f tokens^=* %%U in ('"%tmp%\ToUpLower.exe" -u %USERNAME%')do set "_up_case=%%U"
    for /f tokens^=* %%l in ('"%tmp%\ToUpLower.exe" -l %USERNAME%')do set "_low_case=%%l"
    
    echo/  Your username upcase is: !_up_case!
    echo/ Your username lowcase is: !_low_case!
    
    echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "." 
    del /q /f "%tmp%\ToUpLower.*" >nul 2>nul && endlocal & goto :EOF
    

    • Outputs for %USERNAME%
     Your username upcase is: USERNAME
    Your username lowcase is: username
    

    The ToUpLower.cs c# code with no escaping:


     using System; namespace SUQ1522019 {class Program {static void Main(string[] args) {
     if (args.Length==2 && args[0].ToLower()=="-l") {Console.WriteLine(args[1].ToLower());} 
     if (args.Length==2 && args[0].ToLower()=="-u") {Console.WriteLine(args[1].ToUpper());}}}}
    

    The ToUpLower.cs c# code with no escaping and indented:


    using System
    namespace SUQ1522019 
    {
       class Program 
       {
          static void Main(string[] args)
          {
             if (args.Length==2 && args[0].ToLower()=="-l") 
             {
                Console.WriteLine(args[1].ToLower());
             } 
    
             if (args.Length==2 && args[0].ToLower()=="-u") 
             {
                Console.WriteLine(args[1].ToUpper());
             }
          }
       }
    }
    

    • This c# code was compiled/tested on csc.exe versions:
    c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
    c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
    c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
    c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
    c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
    

    • This is the command line used to compile the c# code:
    c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo 
    

    • ToUpLower.exe usage UPPER to -> lower
    ToUpLower.exe -l STRING 
    
    :: or ..
    
    ToUpLower.exe -L STRING
    


    • ToUpLower.exe usage lower to -> UPPER

    ToUpLower.exe -u string
    
    :: or ..
    
    ToUpLower.exe -U string
    


    To keep ToUpLower.exe, remove echo/ from copy command:


    echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "." 

    This command line will copy ToUpLower.exe from %temp% to same the same folder where your bat is running.


    Programming Guide C#:

    • Args

    • ToLower

    • ToUpper


    Sorry my limited English

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