Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何限制批处理变量';s长度_Windows_Variables_Batch File_Limit_Maxlength - Fatal编程技术网

Windows 如何限制批处理变量';s长度

Windows 如何限制批处理变量';s长度,windows,variables,batch-file,limit,maxlength,Windows,Variables,Batch File,Limit,Maxlength,有没有办法限制批处理变量的长度?我的意思是,如果可以编程一个只允许0到x个字符的变量?例如,如果我输入123456,最大长度为4,它就不会继续。我希望你能理解我的问题。 提前感谢。根据和的建议演示批次代码: !用户输入:~4在执行批处理文件时由命令处理器替换为用户输入中以第五个字符开头的字符串。字符串值的第一个字符的索引值为0,这是第五个字符使用数字4的原因。如果用户输入的字符串长度不超过4个字符,则此字符串为空,否则此子字符串不为空,导致用户必须再次输入字符串 延迟扩展用于避免在用户输入包含奇

有没有办法限制批处理变量的长度?我的意思是,如果可以编程一个只允许0到x个字符的变量?例如,如果我输入123456,最大长度为4,它就不会继续。我希望你能理解我的问题。
提前感谢。

根据和的建议演示批次代码:

!用户输入:~4在执行批处理文件时由命令处理器替换为用户输入中以第五个字符开头的字符串。字符串值的第一个字符的索引值为0,这是第五个字符使用数字4的原因。如果用户输入的字符串长度不超过4个字符,则此字符串为空,否则此子字符串不为空,导致用户必须再次输入字符串

延迟扩展用于避免在用户输入包含奇数双引号的字符串时由于语法错误而导致批处理退出

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • cls/?
  • echo/?
  • endlocal/?
  • 如果/?
  • 暂停/?
  • 设置/?
  • setlocal/?
如果您的意思是“在通过SET/p命令读取时限制批处理变量的长度”,则可以使用中描述的ReadLine子程序,该子程序使用纯批处理文件命令模拟
SET/p
命令,只需插入最大长度限制即可

@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
@echo关闭
setlocal
call:ReadNChars string4=“最多输入4个字符:”4
回显字符串读取:“%string4%”
后藤:EOF
:ReadNChars var=“prompt”maxLen
rem读取模拟SET/P命令的行
雷姆·安东尼奥·佩雷斯·阿亚拉
rem初始化变量
setlocal EnableDelayedExpansion
回声>_
对于('copy/Z\NUL')中的/F%%a,请设置“CR=%%a”
对于('echo prompt$H^ | cmd')中的/F%%a,请设置“BS=%%a”
rem显示提示并开始读取
设置/P“=%2”NUL'),如果未定义键集,则执行“键=%%a”
rem如果键为CR:终止输入
如果“!key:~-1!”eq“!CR!”转到endRead
rem如果键为BS:删除最后一个字符(如果有)
设置“键=!键:~-1!”
如果“!key!”等于“!BS!”(
如果%i%gtr 0(
设置/P“=!BS!!BS!”

但是,如果您想在其他情况下限制批处理变量的长度,如
SET/a
或普通
SET
命令,则无法做到这一点。当然,您可以执行这些命令,然后将变量值剪切到最大长度,但这是一个完全不同的过程。

没有办法限制;但是,您可以使用子字符串扩展,如
%variable:~4%
,并检查此子字符串是否为空;如果没有,字符串长度将超过4个字符…请注意:批处理语言不是为像您正在尝试的那样复杂的作业而设计的。这并不意味着它不能做,但你应该认真考虑使用一个更好的设计语言。(或者,更准确地说,是一种设计的语言,而不是随机积累的语言。)对于最大长度为4的
,如果不是“%var:~4%”==“goto ask_”
@HarryJohnston:我真的希望看到一种用任何“设计更好的语言”编写的解决方案,它比纯批处理文件解决方案简单得多。请注意:此类其他语言解决方案通常与纯批处理文件解决方案一样复杂,如中所示。@Aacini:我不确定您为什么将其变得如此复杂(请参阅Mofi的答案进行比较),但如果我们将您的方法转换为C,例如,我相信它会大大简化。将调用子shell的
for/F
命令与用于生成回车符的空设备与四个字符的C等价物
'\r'
进行比较。即使在财政部的例子中,也要将
%UserInput:~4%“==”
strlen(UserInput)>4进行比较。(尽管如此,我并没有真正考虑简单性,而是可靠性和可维护性,比如结构化循环和goto。)
@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