Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 between these two worlds.

Is there any cmd.exe function to convert the value of the Windows system variable %USERNAME% to lower case?

Thanks much in advance!


1 Answers

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.
like image 128
2 revsDharma Leonardi Avatar answered Sep 03 '25 21:09

2 revsDharma Leonardi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!