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

前端 未结 2 1493
闹比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:21

    Personally I'd rewrite it as a python or vbscript:

    Quick / crude vbscript concept code, not particularly efficient but hopefully read-able:

    Function MakeDirectories (strRootFolder, strParentFolder, strArrayFolderNames)
    
        on error resume next
        err.clear
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        strParentFolder = strRootFolder & "\" & strParentFolder
    
        if not objFSO.FolderExists(strParentFolder) then
            objFSO.CreateFolder(strParentFolder)
        end if
    
        if err.number then
            MakeDirectories = false    
            exit function
        end if
    
        dim strNewFolder
    
        for each strfolderName in strArrayFolderNames
            strNewFolder = strParentFolder & "\" & ProperNames(strFolderName)
            if not objFSO.FolderExists(strNewFolder) then
                objFSO.CreateFolder(strNewFolder)
            end if
        next
    
        if err.number then
            MakeDirectories = false    
        else    
            MakeDirectories = True
        end if
    
    End function
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    function Proper ( strText )
    
        Proper = ucase(left(strText,1)) & lcase(mid(strText,2))
    
    end function    
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Function ProperNames ( strText )
    
        if instr(strText," ") > 0 then
            dim temp, i
            temp = split(strText, " ")
            for i = lbound(temp) to ubound(temp)
                temp(i) = Proper(temp(i))
            next    
            ProperNames = join(temp, " ")
        else
            ProperNames = Proper(strText)
        end if    
    
    End Function    
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Sub Main ( )
    
        dim strLastName, strFirstName
    
        strLastName = InputBox("Please enter the client's last name:")
        if strLastName = "" then exit sub
    
        strFirstName = InputBox("Please enter the client's first name:")
        if strLastName = "" then exit sub
    
        ''  a better alternative might be to put the desired folder 
        ''  into a text file and have the program read said data
        dim strArrayFolderNames(9)
        strArrayFolderNames(0) = "Budget"
        strArrayFolderNames(1) = "Business Registration"
        strArrayFolderNames(2) = "Correspondence"
        strArrayFolderNames(3) = "Financial Info"
        strArrayFolderNames(4) = "Forms"
        strArrayFolderNames(5) = "Illustrations"
        strArrayFolderNames(6) = "Loans & Investments"
        strArrayFolderNames(7) = "Personal Info"
        strArrayFolderNames(8) = "Recommendations"
        strArrayFolderNames(9) = "Tax Misc"
    
        dim strDelimeter, strRootFolder, strParentFolder
    
        strDelimeter     = "-" '' I suggest avoiding the use of ","
        strRootFolder    = "C:\docs\temp"
        strParentFolder  = Proper(strLastName) & strDelimeter & Proper(strFirstName)
    
        If MakeDirectories(strRootFolder, strParentFolder, strArrayFolderNames) then
            wscript.echo ("Folders all made.")
        else
            wscript.echo ("Error: one or more folders was not created.")
        end if
    
    End Sub
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Main ()
    

    Finally, I suggest you don't use commas in folder names, it will save you admin grief down the road.

    Michael.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题