How to convert string text to uppercase?
Moderator: DosItHelp
How to convert string text to uppercase?
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
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?
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:
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
-
- Posts: 5
- Joined: 14 Dec 2013 10:12
Re: How to convert string text to uppercase?
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
DOS/batch is dead.
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?
brianadams wrote:DOS/batch is dead.
Did you check out the theme of this forum?
-
- Posts: 5
- Joined: 14 Dec 2013 10:12
Re: How to convert string text to uppercase?
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?
FC is a nice proposal!
I was not able to get it to work with quotation marks in the string though
Regards
aGerman
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
-
- Posts: 5
- Joined: 14 Dec 2013 10:12
Re: How to convert string text to uppercase?
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?
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.
Batch is mostly about fun programming.
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.
Re: How to convert string text to uppercase?
Thanks
... prduces ...
... 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
Regards
aGerman
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
Regards
aGerman
Re: How to convert string text to uppercase?
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.
Re: How to convert string text to uppercase?
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?
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?
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?
Sure. When I said quick I meant least code.
Re: How to convert string text to uppercase?
This is slightly less code:
Antonio
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