Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert to uppercase

823 views
Skip to first unread message

Tom Del Rosso

unread,
Feb 19, 2011, 10:34:52 PM2/19/11
to

No immediate need, but I was musing about how to do this.


This converts to uppercase even though drive x: doesn't exist. Then you
just have to remove the colon.

for %%a in (x:) do echo %%~da


Oddly, this (with backslash) does nothing (it returns the value in the same
case in which it was given), whether or not the drive exists.

for %%a in (x:\) do echo %%~da


But this converts to uppercase if the name exists.

for %%a in (x:\name) do echo %%~da

Now how can you do it to lowercase?


--

Reply in group, but if emailing add one more
zero, and remove the last word.


Timo Salmi

unread,
Feb 20, 2011, 12:40:32 PM2/20/11
to
On 20.02.2011 05:34 Tom Del Rosso wrote:
> Now how can you do it to lowercase?

Converting the case of a variable is covered in
http://www.netikka.net/tsneti/info/tscmd039.htm
Works both ways.

Also http://www.netikka.net/tsneti/info/tscmd.php#namedown

All the best, Timo

--
Prof. Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php

01MDM

unread,
Feb 20, 2011, 3:56:32 PM2/20/11
to
> But this converts to uppercase if the name exists.

What input and output such:

http://storage1.static.itmages.ru/i/11/0220/h_1298234783_a6b2a373c3.png

billious

unread,
Feb 20, 2011, 7:23:36 PM2/20/11
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:4d615210$0$2861$9188...@news.netikka.fi...

> On 20.02.2011 05:34 Tom Del Rosso wrote:
>> Now how can you do it to lowercase?
>
> Converting the case of a variable is covered in
> http://www.netikka.net/tsneti/info/tscmd039.htm
> Works both ways.
>

A little tickle:

set var_=%%%1%%

endlocal&set %1=%var_%&goto :eof

(and remove the SET var_=%var_:"=% )

will allow

CALL :toupper somevar

to change somevar's contents to upper-case


foxidrive

unread,
Feb 20, 2011, 7:36:19 PM2/20/11
to


*scratches head* I Am very tired - this doesn't seem to work here.


@echo off

set a=abc

call :toupper a
echo %a%

call :toupper %a%
echo %a%

pause
goto :EOF

:toupper
set var_=%%%1%%

endlocal&set %1=%var_%&goto :eof


--
Regards,
Mic

01MDM

unread,
Feb 20, 2011, 11:13:18 PM2/20/11
to
If we are discussing the convertation of the case in general, that is,
such a method to uppercase

@echo off
setlocal

set "var=qwerty"

for /f "tokens=2 delims=:" %%i in ('

2^>^&1 find /c /v "" "%var%"

') do echo %%i


billious

unread,
Feb 21, 2011, 1:05:29 AM2/21/11
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:asi8p.14188$4g3....@newsfe04.iad...

More likely I'm being too vague. Also, that should have been "CALL set
var_=%%%1%%"

In the Book of Timo, Chapter 39, verse 1 we find:

@echo off & setlocal enableextensions
set myvar_=My Test String! וצה
echo %myvar_%
call :ToUpcase "%myvar_%" myvar_
echo %myvar_%
endlocal & goto :EOF
::
:: ======================
:ToUpcase
setlocal enableextensions
set var_=%1
set var_=%var_:a=A%
set var_=%var_:b=B%
[snip[*]]
set var_=%var_:z=Z%
set var_=%var_:"=%
endlocal & set %2=%var_%& goto :EOF

[*] : there are Scandinavian conversions after "z" that have been snipped
also

In the above, if you change
set var_=%1 TO call set var_=%%%1%%
endlocal & set %2=%var_%& goto :EOF TO endlocal&set %1=%var_%&goto :eof


and remove the SET var_=%var_:"=%

we get: (just the meat...)

:ToUpcase
setlocal enableextensions
set var_=%%%1%%
set var_=%var_:a=A%
set var_=%var_:b=B%

...
set var_=%var_:z=Z%
REM set var_=%var_:"=%
endlocal & set %1=%var_%& goto :EOF


and with a routine where executing

CALL :toupcase myvar_

should render myvar_ to upper-case

demo: (converting only A,B,Z for brevity...)

This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]setlocal
[3]set var=AaBbZz%%abz%%+!zba!+(z,b,a)"baz"
[4]echo before=%var%=
[5]call :toup var
[6]echo after =%var%=
[7]goto :eof
[8]
[9]::
[10]:: convert %1 to upper-case (abbreviated)
[11]::
[12]:toup
[13]setlocal enableextensions
[14]call set var_=%%%1%%
[15]set var_=%var_:a=A%
[16]set var_=%var_:b=B%
[17]set var_=%var_:z=Z%
[18]endlocal&set %1=%var_%&goto :eof
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

So - whereas Timo's routine uppercases a string to a variable, if the string
is already in a variable, this modified routine will convert it to
upper-case, which I find a little more convenient.
- It obviates the need to quote the subject string if separators are
required
- It does not strip-out quotes

In fact, warming to the theme

[19]:toup
[20]setlocal enableextensions
[21]call set var_=%%%1%%
[22]if not defined var_ goto todone
[23]set var_=%var_:a=A%
[24]set var_=%var_:b=B%
[25]set var_=%var_:z=Z%
[26]:todone
[27]endlocal&set %1=%var_%
[28]if not "%2"=="" shift&goto toup
[29]goto :eof

would allow

CALL :toup some series of variables

to upper-case the variables "some","series","of" and "variables"

foxidrive

unread,
Feb 21, 2011, 1:09:25 AM2/21/11
to


I changed the delims to * and see this:

File not found - QWERTY

so perhaps there is a locality difference.
This works:


@echo off
setlocal

set "var=qwerty DE NoP"

for /f "tokens=4,*" %%i in ('

2^>^&1 find /c /v "" "%var%"

') do echo %%j


--
Regards,
Mic

billious

unread,
Feb 21, 2011, 1:19:16 AM2/21/11
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:pkn8p.25354$6J5....@newsfe05.iad...

Umm - doesn't work if the target string is an existing filename...or is an
afn that hits a target...


foxidrive

unread,
Feb 21, 2011, 1:47:37 AM2/21/11
to

An easy fix. :)


@echo off
setlocal

set "var=qwerty DE NoP"

for /f "tokens=2 delims=>" %%i in ('

2^>^&1 find /c /v "" ">%var%"

') do echo %%i


--
Regards,
Mic

billious

unread,
Feb 21, 2011, 3:09:53 AM2/21/11
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:gUn8p.48288$w57....@newsfe13.iad...

Closer - poison characters are ? and * (at least) - no case-conversion and
'"' affixed.


I'm_HERE

unread,
Feb 21, 2011, 4:51:55 AM2/21/11
to

01MDM

unread,
Feb 21, 2011, 9:52:13 AM2/21/11
to
If religion does not forbid, you can use POSH

@echo off
setlocal

set "var=qwerty"
echo %var%

for /f %%i in ('echo "%var%".toupper^(^) ^| powershell -c -') do set
"var=%%i"
echo %var%

for /f %%i in ('echo "%var%".tolower^(^) ^| powershell -c -') do set
"var=%%i"
echo %var%

0 new messages