【问题标题】:Batch file input phone number批处理文件输入电话号码
【发布时间】:2014-09-06 08:21:18
【问题描述】:

我编写了一个批处理文件来向电话号码%number% 发送短信。 (如果您的古玩,它使用 curl 和 textbelt.com 系统)。我使用了一种过于复杂的方式让用户输入电话号码。我将字符限制为只有数字,自动十个字符长,并自动添加破折号。

这是我的代码:

:start
cls
echo Enter phone number:
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x1=%x% - 1
cls
echo Enter phone number:
echo %x1%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x2=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x3=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x4=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x5=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x6=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x7=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x8=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%%x8%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x9=%x% - 1
cls
echo Enter phone number:
echo %x1%%x2%%x3%-%x4%%x5%%x6%-%x7%%x8%%x9%
choice /c 0123456789 >nul
set x=%errorlevel%
set /a x10=%x% - 1
set number=%x1%%x2%%x3%%x4%%x5%%x6%%x7%%x8%%x9%%x10%
cls
echo is this the correct number?
echo. %number%
choice /c yn 
set x=%errorlevel%
if %x%==2 goto start

如您所见,它确实又长又复杂。它有效,但它就是您所说的“意大利面条代码”。我敢打赌,有一种更简单的方法可以实现我的目标。

【问题讨论】:

    标签: batch-file phone-number


    【解决方案1】:

    如果您可以使用外部程序,您可以使用 editv32.exe(32 位)或 editv64.exe(64 位)程序来获取环境变量的输入。示例 shell 脚本(批处理文件):

    @echo off
    setlocal enableextensions
    set PHONE=
    editv32 -l 10 -n -p "Enter phone number: " PHONE
    echo You entered: %PHONE%
    

    -l 10 将输入长度限制为 10 个字符,-n 只允许输入数字。您可以从这里下载editv32.exe/editv64.exe:http://www.westmesatech.com/editv.html(版权所有免费软件)

    【讨论】:

      【解决方案2】:

      一个想法:

      @echo off
      setlocal enabledelayedexpansion
          cls
      :start
      set number=
      for /l %%a in (1,1,9) do (
      cls
      if defined number echo !number!
      echo Enter phone number:
      choice /c 0123456789 >nul
      set /a x=!errorlevel!-1
      set  number=!number!!x!
      )
      
      echo is this the correct number?
      echo. %number:~0,3%-%number:~3,3%-%number:~6,3%
      choice /c yn 
      set x=%errorlevel%
      if %x%==2 goto start
      

      【讨论】:

        【解决方案3】:

        您可以以非常简单的方式使用 ReadFormattedLine 子程序;例如:

        :start
        cls
        call :ReadFormattedLine number="###-###-####" /M "Enter phone number: "
        echo Is this the correct number?
        choice /c yn
        if %errorlevel% equ 2 goto start
        echo The correct number is: %number%
        

        这个子程序是用纯Batch编写的,不需要任何额外的程序,它允许多种格式化输入操作,如读取密码、转换为大写字母等。您可以从:Read a line with specific format下载。

        【讨论】:

          【解决方案4】:

          我的开源 editenv 工具取代了我的旧 editv32/editv64 实用程序:

          https://github.com/Bill-Stewart/editenv

          示例用法:

          editenv -n 10 -a 0123456789 -p "Enter phone number: " PHONE
          

          此命令将显示Enter phone number: 提示并等待输入。输入长度限制为 10 个字符 (-n),允许的字符限制为数字 (-a)。

          在这里下载:

          https://github.com/Bill-Stewart/editenv/releases

          【讨论】:

            猜你喜欢
            • 2021-10-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-30
            • 1970-01-01
            • 2017-08-25
            • 1970-01-01
            相关资源
            最近更新 更多