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

前端 未结 2 1494
闹比i
闹比i 2021-01-24 03:53

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 F         


        
2条回答
  •  清歌不尽
    2021-01-24 04:24

    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.

提交回复
热议问题