Search code examples
if-statementbatch-filevariables

How do I script for "If %variable%==*ANYTHING* perform task"?


I am trying to add to a script that I have been working on (and Compo has been a big help with) to incorporate user input. I currently have:

SET /p KIOSK=Enter NEW account here or leave blank to QUERY for current account  _
ECHO. 
IF "%KIOSK%"==* @%SystemRoot%\System32\reg.exe ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" /D %KIOSK% /F 1>NUL && ECHO Kiosk account changed to %KIOSK% || ECHO Kisok account NOT changed 
IF "%KIOSK%"=="" ECHO Querying for current account . . .

What I want is for @%SystemRoot%\System32\reg.exe ADD to change the DefaultUserName to whatever the user enters. The KIOSK will look something like this: "K#######" where # could be any number 0-9. So basically, IF %KIOSK%== literally anything, I want the DefaultUserName changed to that value. I am not sure how to get this result. And yes, I know it has to be run as Admin.


Solution

  • I suggest the following commented code to process the user entered string:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    :PromptUser
    set "KIOSK="
    set /P "KIOSK=Enter NEW account here or leave blank to QUERY for current account: "
    if not defined KIOSK goto QueryAccount
    
    rem Remove all double quotes from user entered string.
    set "KIOSK=%KIOSK:"=%"
    rem Prompt the user again on having only double quotes entered.
    if not defined KIOSK goto PromptUser
    rem Prompt the user again if the first characeter is not an upper case K.
    if not "%KIOSK:~0,1%" == "K" goto PromptUser
    rem Prompt the user again if there is not at least one more character.
    if "%KIOSK:~1%" == "" goto PromptUser
    rem Prompt the user again if the user entered string contains
    rem other characters than digits after the first character K.
    for /F "tokens=1* delims=0123456789" %%I in ("%KIOSK%") do if not "%%J" == "" goto PromptUser
    
    set "RegExe=%SystemRoot%\System32\reg.exe"
    if exist "%SystemRoot%\Sysnative\reg.exe" set "RegExe=%SystemRoot%\Sysnative\reg.exe"
    rem if exist "%SystemRoot%\SysWOW64\reg.exe" set "RegExe=%SystemRoot%\SysWOW64\reg.exe"
    %RegExe% ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" /D %KIOSK% /F 1>NUL && echo Kiosk
    goto EndBatch
    
    :QueryAccount
    echo Querying for current account ...
    
    :EndBatch
    endlocal
    

    I am not sure if HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon should be updated with DefaultUserName which does not exist by default on Windows 7 x64 or HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Winlogon which contains by default an empty string value with the name DefaultUserName.

    The code above adds it to registry key for 64-bit applications on Windows AMD64 while using the commented line with SysWOW64 would add the registry value to registry key for 32-bit applications on Windows AMD64.

    See also the Microsoft documentation pages:

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • reg /?
    • reg add /?
    • rem /?
    • set /?
    • setlocal /?