Renk Software
Renk Software

Reputation: 174

How to limit a batch variable's length

Is it any way to limit the length of a batch variable? I mean, if it is possible to program a variable that only admits between 0 and x characters? So, for an instance, if I entered 123456 and the max length was 4 it wouldn't proceed to continue. I hope you can understand my question. Thanks in advance.

Upvotes: 2

Views: 4873

Answers (2)

Mofi
Mofi

Reputation: 49127

Demonstration batch code according to suggestions of aschipfl and rojo:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
:UserPrompt
cls
set "UserInput="
set /P "UserInput=Enter string with a length between 1 and 4: "
if not defined UserInput goto UserPrompt
if not "!UserInput:~4!" == "" goto UserPrompt
echo/
echo String entered: !UserInput!
echo/
endlocal
pause

!UserInput:~4! is replaced by command processor on execution of the batch file by the string from user input starting with fifth character. First character of a string value has index value 0 which is reason for number 4 for fifth character. This string is empty if user entered a string not longer than 4 characters, otherwise this substring is not empty resulting in user must input again a string.

Delayed expansion is used to avoid an exit of batch processing caused by a syntax error if the user enters a string containing an odd number of double quotes.

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

  • cls /?
  • echo /?
  • endlocal /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

Upvotes: 3

Aacini
Aacini

Reputation: 67216

If you mean "limit the length of a batch variable when it is read via SET /P command", then you may use the ReadLine subroutine described at this post, that emulates SET /P command using pure Batch file commands, and just insert the maximum length restriction.

@echo off
setlocal

call :ReadNChars string4="Enter 4 characters maximum: " 4
echo String read: "%string4%"
goto :EOF


:ReadNChars var="prompt" maxLen

rem Read a line emulating SET /P command
rem Antonio Perez Ayala

rem Initialize variables
setlocal EnableDelayedExpansion
echo > _
for /F %%a in ('copy /Z _ NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"

rem Show the prompt and start reading
set /P "=%~2" < NUL
set "input="
set i=0

:nextKey
   set "key="
   for /F "delims=" %%a in ('xcopy /W _ _ 2^>NUL') do if not defined key set "key=%%a"

   rem If key is CR: terminate input
   if "!key:~-1!" equ "!CR!" goto endRead

   rem If key is BS: delete last char, if any
   set "key=!key:~-1!"
   if "!key!" equ "!BS!" (
      if %i% gtr 0 (
         set /P "=!BS! !BS!" < NUL
         set "input=%input:~0,-1%"
         set /A i-=1
      )
      goto nextKey
   )

   rem Insert here any filter on the key
   if %i% equ %3 goto nextKey

   rem Else: show and accept the key
   set /P "=.!BS!%key%" < NUL
   set "input=%input%%key%"
   set /A i+=1

goto nextKey

:endRead
echo/
del _
endlocal & set "%~1=%input%"
exit /B

However, if you want to limit the length of a Batch variable in other cases, like SET /A or plain SET commands, then there is no way to do that. Of course, you may execute such commands and then cut the variable value to the maximum length, but that process is an entirely different thing.

Upvotes: 1

Related Questions