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

String case conversion in Batch

40 views
Skip to first unread message

John Stockton

unread,
Aug 21, 2021, 8:15:14 AM8/21/21
to
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

unread,
Aug 21, 2021, 8:31:07 AM8/21/21
to
On Saturday, 21 August 2021 at 13:15:14 UTC+1, John Stockton wrote:
> 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.

...

> 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.

JJ

unread,
Aug 21, 2021, 9:53:04 AM8/21/21
to
On Sat, 21 Aug 2021 05:15:13 -0700 (PDT), John Stockton wrote:
> 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
0 new messages