Page 1 of 2

How to convert string text to uppercase?

Posted: 16 Dec 2013 02:11
by pstein
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

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 04:35
by foxidrive
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

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 06:35
by brianadams
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.

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 07:52
by foxidrive
brianadams wrote:DOS/batch is dead.


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

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 08:02
by brianadams
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.

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 12:18
by aGerman
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

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 18:07
by brianadams
aGerman wrote:FC is a nice proposal!

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

Re: How to convert string text to uppercase?

Posted: 16 Dec 2013 21:58
by foxidrive
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

Re: How to convert string text to uppercase?

Posted: 17 Dec 2013 03:31
by aGerman
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

Re: How to convert string text to uppercase?

Posted: 17 Dec 2013 20:58
by dbenham
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

Re: How to convert string text to uppercase?

Posted: 17 Dec 2013 21:00
by dbenham
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

Re: How to convert string text to uppercase?

Posted: 17 Dec 2013 21:36
by foxidrive
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.

Re: How to convert string text to uppercase?

Posted: 17 Dec 2013 23:22
by dbenham
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

Re: How to convert string text to uppercase?

Posted: 18 Dec 2013 00:05
by foxidrive
Sure. :) When I said quick I meant least code.

Re: How to convert string text to uppercase?

Posted: 18 Dec 2013 00:26
by Aacini
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