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

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

提交回复
热议问题