Skip to content

Instantly share code, notes, and snippets.

@ijprest
Created September 10, 2011 01:52
Show Gist options
  • Select an option

  • Save ijprest/1207818 to your computer and use it in GitHub Desktop.

Select an option

Save ijprest/1207818 to your computer and use it in GitHub Desktop.
CMD Batch file implementation of toupper() and tolower()
@echo off & SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET STR=Test String
SET STR
call :tolower STR
SET STR
call :toupper STR
set STR
goto :EOF
:: toupper & tolower; makes use of the fact that string
:: replacement (via SET) is not case sensitive
:toupper
for %%L 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 %1=!%1:%%L=%%L!
goto :EOF
:tolower
for %%L 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 %1=!%1:%%L=%%L!
goto :EOF
@guntari

guntari commented Apr 28, 2016

Copy link
Copy Markdown

nice work

@Ares-87

Ares-87 commented Oct 23, 2016

Copy link
Copy Markdown

Hello, I suggest you a fix, if the string is empty at the time of conversion will return this as result "a=a" or the first element of array.
To prevent this, add in the two arrays, "^^" without quotes:

:tolower (a b c d...) and :toupper (A B C D...)
:tolower (^^ a b c d...) and :toupper (^^ A B C D...)

I hope can be useful. Bye

@s7646984

s7646984 commented Jun 1, 2018

Copy link
Copy Markdown

Thank you, works well

@CookieSnippets

Copy link
Copy Markdown

This needs ENABLEDELAYEDEXPANSION in the main code.
(which can cause additional errors when strings contain "!" characters)

So, to do the same WITHOUT the need for delayed expansion enabled, use this:
(note that this also takes empty strings in account!)

set test=Hello World!!
echo '%test%'
call :toupper test
echo '%test%'
call :tolower test
echo '%test%'
goto :eof

:toupper
if defined %1 for %%U 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 call set %1=%%%1:%%U=%%U%%
goto :eof

:tolower
if defined %1 for %%L 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 call set %1=%%%1:%%L=%%L%%
goto :eof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment