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

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.

Leave a Comment