How to convert string text to uppercase?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to convert string text to uppercase?

#1 Post by pstein » 16 Dec 2013 02:11

As far as I found out there is no easy (!) way to convert a text string to uppercase.

The only way is by a procedure like shown on this page:

http://www.dostips.com/DtTipsStringOperations.php

Is this correct?

Is there really no function similar to:

set sometext ="abcd"
set uc = toUpper %sometext%

Peter

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to convert string text to uppercase?

#2 Post by foxidrive » 16 Dec 2013 04:35

Nothing so simple.

This is a function that can help - poison characters may affect it.
http://www.dostips.com/DtTipsStringOper ... on.toUpper

A VBS script will be a bit simpler and more robust but you'll be using the script.

There's a kludge which could be used, depending on the txt. For a single word it could work well, and multiple words would need to be well defined.

Type this and you'll see the effect:

Code: Select all

fc "abcdef ghi" nul

brianadams
Posts: 5
Joined: 14 Dec 2013 10:12

Re: How to convert string text to uppercase?

#3 Post by brianadams » 16 Dec 2013 06:35

DOS/batch is a shell. A shell lets you run commands, whether they are compiled or needs to be run by an "interpreter". So it doesn't matter. Unless you cannot use it explicitly, VBScript comes preinstalled in most Windows OS and is close to doing what you needed in a more convenient sense. Modern Windows OS also have the powershell. All these are better than DOS batch in terms of rapid development. So try to learn them if possible.

for myself I prefer Perl

Code: Select all

my $string = "test"
print uc($string)


DOS/batch is dead.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to convert string text to uppercase?

#4 Post by foxidrive » 16 Dec 2013 07:52

brianadams wrote:DOS/batch is dead.


Did you check out the theme of this forum? :)

brianadams
Posts: 5
Joined: 14 Dec 2013 10:12

Re: How to convert string text to uppercase?

#5 Post by brianadams » 16 Dec 2013 08:02

foxidrive wrote:
brianadams wrote:DOS/batch is dead.


Did you check out the theme of this forum? :)

the theme says DOS batch. that does not mean only pure internal DOS commands only. Right? :) You seem knowledgeable, i am sure you know what i mean.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to convert string text to uppercase?

#6 Post by aGerman » 16 Dec 2013 12:18

FC is a nice proposal!

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "string=;%%a^b/.\?=c!: &d*e"
set lf=^


set "upper="&rem Two empty lines required!
for /f skip^=1^ delims^=^ eol^= %%i in (
  '2^>^&1 cmd /von /c "fc "^!lf^!^!string^!^!lf^!" nul"'
) do if not defined upper set "upper=%%i"

setlocal EnableDelayedExpansion
echo !upper!

pause

I was not able to get it to work with quotation marks in the string though :?

Regards
aGerman

brianadams
Posts: 5
Joined: 14 Dec 2013 10:12

Re: How to convert string text to uppercase?

#7 Post by brianadams » 16 Dec 2013 18:07

aGerman wrote:FC is a nice proposal!

don't think its suitable for production. maybe more suitable for a fun college programming class.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to convert string text to uppercase?

#8 Post by foxidrive » 16 Dec 2013 21:58

Thanks for looking into it further aGerman.

This works with redirection and pipes too. The 16,-25 works in an English version of FC (Win 8.1 32 bit)
Using the LF to separate the terms is pretty clever.

Code: Select all

@echo off
set "string=;<>|%%a^b/.\?=c!: &d*e"
for /f "delims=" %%a in ('fc "%string%" nul 2^>^&1') do set "string=%%a"
set "upper=%string:~16,-25%"
echo "%upper%"
pause


brianadams wrote:don't think its suitable for production. maybe more suitable for a fun college programming class.


Batch is mostly about fun programming. :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to convert string text to uppercase?

#9 Post by aGerman » 17 Dec 2013 03:31

Thanks :)

Code: Select all

fc "%string%" nul

... prduces ...

Code: Select all

FC: Kann ;<>|%A^B/.\?=C!: &D*E nicht öffnen - Datei oder Ordner nicht vorhanden

... on my German Win7.
Although the greatest advantage of using line feeds is that they belong to the "file name". Thus you don't have any risk to find such a real file even if the string is "test.txt" or whatever :wink:

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to convert string text to uppercase?

#10 Post by dbenham » 17 Dec 2013 20:58

aGerman wrote:FC is a nice proposal!

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "string=;%%a^b/.\?=c!: &d*e"
set lf=^


set "upper="&rem Two empty lines required!
for /f skip^=1^ delims^=^ eol^= %%i in (
  '2^>^&1 cmd /von /c "fc "^!lf^!^!string^!^!lf^!" nul"'
) do if not defined upper set "upper=%%i"

setlocal EnableDelayedExpansion
echo !upper!

pause

I was not able to get it to work with quotation marks in the string though :?

I got it to work with everything but linefeed, and that could be added easily enough with another substitution, plus the safe return technique.

Code: Select all

@echo off
setlocal disableDelayedExpansion
set "test=test unquoted @^&|<>()! and "quoted @^^^&^|^<^>()!""
call :toUpper test
exit /b

:toUpper  inVar  [outVar]
setlocal enableDelayedExpansion
if not defined %~1 (
  endlocal
  if "%~2" neq "" (set "%~2=") else echo(
  exit /b
)
set ^"LF=^

^"  The above empty line is critical - DO NOT REMOVE
set "str=!%~1!"
set "str=!str:@=@A!"
set "str=!str:"=@Q!"
set "rtn="
setlocal disableDelayedExpansion
for /f "skip=1 delims=" %%A in ('cmd /v:on /c fc "!LF!!str!!LF!" nul 2^>^&1') do (
  if not defined rtn set "rtn=%%A"
)
setlocal enableDelayedExpansion
set "rtn=!rtn:@Q="!"
set "rtn=!rtn:@A=@!"
for /f "delims=" %%A in (""!rtn!"") do (
  endlocal&endlocal&endlocal
  if "%~2" neq "" (set "%~2=%%~A") else echo(%%~A
)
exit /b

BUT... I then tried testing with my PATH variable and it failed. It turns out the FC command only allocates enough memory to support the Windows maximum file path length of 260 bytes.

At around length 260 the FC gives "Out of memory" error. And then at some even longer length it gives two error messages: "Invalid switch" and "Insufficient number of file specifications". So I didn't bother adding LF or safe return support.


Dave Benham
Last edited by dbenham on 17 Dec 2013 23:17, edited 2 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to convert string text to uppercase?

#11 Post by dbenham » 17 Dec 2013 21:00

foxidrive wrote:This is a function that can help - poison characters may affect it.
http://www.dostips.com/DtTipsStringOper ... on.toUpper

That function can easily be adapted to support all characters by switching to delayed expansion and addition of safe return technique.


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to convert string text to uppercase?

#12 Post by foxidrive » 17 Dec 2013 21:36

dbenham wrote:BUT... I then tried testing with my PATH variable and it failed. It turns out the FC command only allocates enough memory to support the Windows maximum file path length of 260 bytes.


So it's not so good as a general purpose routine, but it could be useful for a quick single word lookup.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to convert string text to uppercase?

#13 Post by dbenham » 17 Dec 2013 23:22

foxidrive wrote:
dbenham wrote:BUT... I then tried testing with my PATH variable and it failed. It turns out the FC command only allocates enough memory to support the Windows maximum file path length of 260 bytes.


So it's not so good as a general purpose routine, but it could be useful for a quick single word lookup.


Except it is not "quick", at least not on my machine. The brute force method is ~5 times faster on my machine, after I modify it to use delayed expansion.

Code: Select all

:toUpper  inVar  [outVar]
if not defined %~1 if "%~2" neq "" (set "%~2=") else echo(
setlocal enableDelayedExpansion
set "str=!%~1!"
for %%A in (
  "a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
  "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
  "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä" "ö=Ö" "ü=Ü"
) do set "str=!str:%%~A!"
for /f "delims=" %%A in (""!str!"") do (
  endlocal
  if "%~2" neq "" (set "%~2=%%~A") else echo(%%~A
)
exit /b


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to convert string text to uppercase?

#14 Post by foxidrive » 18 Dec 2013 00:05

Sure. :) When I said quick I meant least code.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to convert string text to uppercase?

#15 Post by Aacini » 18 Dec 2013 00:26

This is slightly less code:

Code: Select all

:toUpper  inVar  [outVar]
if not defined %~1 if "%~2" neq "" (set "%~2=") else echo(
setlocal enableDelayedExpansion
set "str=!%~1!"
for %%A 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 "str=!str:%%A=%%A!"
for /f "delims=" %%A in (""!str!"") do (
  endlocal
  if "%~2" neq "" (set "%~2=%%~A") else echo(%%~A
)
exit /b


Antonio

Post Reply