Search code examples
batch-filetitle-case

Make the first letter of user input a capital in a batch script


This is the batch script I use to make the folders for a new client:

@ECHO OFF
SET /p clientLast=Enter Client's Last Name: 
SET /p clientFirst=Enter Client's First Name:  
ECHO Making Folders...
MKDIR "%clientLast%, %clientFirst%"
MKDIR "%clientLast%, %clientFirst%"\Budget
MKDIR "%clientLast%, %clientFirst%"\"Business Registration"
MKDIR "%clientLast%, %clientFirst%"\Correspondence
MKDIR "%clientLast%, %clientFirst%"\"Financial Info"
MKDIR "%clientLast%, %clientFirst%"\Forms
MKDIR "%clientLast%, %clientFirst%"\Illustrations
MKDIR "%clientLast%, %clientFirst%"\"Loans & Investments"
MKDIR "%clientLast%, %clientFirst%"\"Personal Info"
MKDIR "%clientLast%, %clientFirst%"\Recommendations
MKDIR "%clientLast%, %clientFirst%"\"Tax Misc"
TREE "%clientLast%, %clientFirst%"
ECHO DONE~~~~~~~~~~~~~~~
PAUSE

I want to be able to add the ability to automatically uppercase the first letter of each word.

I found a way to do it by replacing every letter with a space in front of it with it's capital, which looks something like:

FOR %%i 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 CALL SET "%1=%%%1:%%~i%%"

But this does not capitalize the first word...

Any ideas?


Solution

  • Or with pure batch...

    @echo off
    setlocal EnableDelayedExpansion
    call :FirstUp result hello
    echo !result!
    
    call :FirstUp result abc
    echo !result!
    
    call :FirstUp result zynx
    echo !result!
    goto :eof
    
    :FirstUp
    setlocal EnableDelayedExpansion
    set "temp=%~2"
    set "helper=##AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ"
    set "first=!helper:*%temp:~0,1%=!"
    set "first=!first:~0,1!"
    if "!first!"=="#" set "first=!temp:~0,1!"
    set "temp=!first!!temp:~1!"
    (
        endlocal
        set "result=%temp%"
        goto :eof
    )
    

    The function :FirstUp use the trick of searching for the first character in the helper string with the %var:*x=% syntax.

    This removes all chars before the first occurrence (therefore I double all chars) So, in first you got for the word "vox", "VWWXXYYZZ", then I simply take the first char of %first% to get the capital and append the rest of the original string without after the first char.