Discussion:
String case conversion in Batch
(too old to reply)
John Stockton
2021-08-21 12:15:13 UTC
Permalink
I enter "z filename" on lower case on the Command Line, and there should be a file "FILENAME.TXT" for "Z.BAT" to use - OK so far.

But I want to use "FILENAME", not "filename", within the Batch file, so that files created by the Batch have using the case of the first part of the .TXT file.

I do this with

:: Copy %1 as FILENAME case into %ZQ%
@dir /b %1.TXT | COLS '@SET * 'ZQ= 1-8 > ZQ.BAT & call ZQ.BAT & del ZQ.BAT

in which COLS.EXE is something I wrote long ago (' starts a space-less string, * gives a space, 1-8 gives that range of the piped input]. %1 is not used after that; %ZQ% is used instead.

Is there a better way of doing it, preferably in pure Batch - I know I could use MiniTrue instead?

The above relies on the case of "FILENAME", which is best for me; but, for interest, could one force upper-case?
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
John Stockton
2021-08-21 12:31:06 UTC
Permalink
Post by John Stockton
I enter "z filename" on lower case on the Command Line, and there should be a file "FILENAME.TXT" for "Z.BAT" to use - OK so far.
...
Post by John Stockton
The above relies on the case of "FILENAME", which is best for me; but, for interest, could one force upper-case?
P.S. I now see that COLS would do that, if asked.
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
JJ
2021-08-21 13:53:01 UTC
Permalink
Post by John Stockton
I enter "z filename" on lower case on the Command Line, and there should
be a file "FILENAME.TXT" for "Z.BAT" to use - OK so far.
But I want to use "FILENAME", not "filename", within the Batch file, so
that files created by the Batch have using the case of the first part of
the .TXT file.
I do this with
:: Copy %1 as FILENAME case into %ZQ%
@dir /b %1.TXT | COLS '@SET * 'ZQ= 1-8 > ZQ.BAT & call ZQ.BAT & del ZQ.BAT
in which COLS.EXE is something I wrote long ago (' starts a space-less
string, * gives a space, 1-8 gives that range of the piped input]. %1 is
not used after that; %ZQ% is used instead.
Is there a better way of doing it, preferably in pure Batch - I know I
could use MiniTrue instead?
The above relies on the case of "FILENAME", which is best for me; but,
for interest, could one force upper-case?
Because batch file can't do substring search, doing it manually is slow.
(long text warning)

@echo off
setlocal
set inp=Hello 2 World!
echo inp=%inp%
echo pure batch:
call :upcase1 "%inp%" out1
echo out1=%out1%
echo external tool:
call :upcase2 "%inp%" out2
echo out2=%out2%
goto :eof

:upcase1 str varout
set ucl=abcdefghijklmnopqrstuvwxyz
set ucu=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set "ucs=%~1"
set uco=
set uci=0
:ucc process char
call set ucc=%%ucs:~%uci%,1%%
if "%ucc%" == "" (
set %2=%uco%
goto :eof
)
set ucj=-1
set uck=0
rem find char in locase table
:ucm
call set ucd=%%ucl:~%uck%,1%%
if "%ucd%" == "" goto ucx
if "%ucd%" == "%ucc%" (
set ucj=%uck%
goto ucx
)
set/a uck+=1
goto ucm
rem if found, use char at same index in upcase table.
rem otherwise use source char.
:ucx
if %ucj% geq 0 (
call set uco=%uco%%%ucu:~%ucj%,1%%
) else call set uco=%uco%%%ucs:~%uci%,1%%
set/a uci+=1
goto ucc

:upcase2 str varout
for /f "usebackq delims=" %%A in (`mshta.exe "javascript:(function(){(new ActiveXObject('scripting.filesystemobject')).getstandardstream(1).writeline('%~1'.toUpperCase());close()})()"`) do @set "%2=%%A"
goto :eof

Loading...