Forums before death by AOL, social media and spammers... "We can't have nice things"
|    alt.msdos.batch.nt    |    Fun with Windows NT batch files    |    68,980 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 67,758 of 68,980    |
|    JJ to John Stockton    |
|    Re: String case conversion in Batch    |
|    21 Aug 21 20:53:01    |
   
   From: jj4public@gmail.com   
      
   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   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca