在批处理文件中搜索输入中存在的字符

时间:2013-06-10 18:00:18

标签: batch-file

我有一个数字变量(在o​​racle DB中存储为VARCHAR2用于某些业务需求)。批处理文件接受用户必须输入数字的用户的此变量。

我希望批处理文件检查用户是否没有输入字符,如果是,则重新指导控制以开始输入,用户必须输入数字变量。

如果没有输入,则在调用存储过程时分配此编号的默认值。

到目前为止,我有以下代码: -

}:enter_input
set nullinp=
set input=
echo.
set /P input="Enter the input (default size is 50): "
echo.
if "%input%"=="%nullinp%" (
set /A input= -1)

:validate_input
echo select to_char('%input%') from dual; > %dir%\validate_input.txt
echo exit >> validate_input.txt

echo "%dbpwd%" | %connStr% @%dir%\validate_input.txt > %log%\validate_input.log

findstr "ORA-" %log%\validate_input.log
if %ERRORLEVEL% NEQ 0 goto message1
if %ERRORLEVEL% EQU 0 goto catch_error

:catch_error
   cls
   echo.  
  echo "ERROR ==> Check no entered"
  echo 
  echo.
  echo.
  goto enter_input

我想在上面添加条件来检查输入的输入是否包含任何字符值,然后控件必须返回到:enter_input。

感谢。

3 个答案:

答案 0 :(得分:2)

试试这个:

echo(%input%|findstr "^[0-9-][0-9]*$" >nul && echo only numbers || goto:enter_input

答案 1 :(得分:0)

PowerShell示例:

do {
  write-host -nonewline "Enter the input (default size is 50): "
  $response = read-host
  if ( $response -eq "" ) {
    $size = 50
  } else {
    $size = $response -as [Int]
  }
  if ( -not $size ) {
    write-host "Invalid size"
  }
} until ( $size -gt 0 )
"You entered: $size"

比尔

答案 2 :(得分:-1)

如果您可以使用外部实用程序,我建议您编写一个名为EditV32.exe的小程序(或64位Windows的EditV64.exe)。它可以将用户输入读入环境变量,并且还具有 -n 选项以仅强制数字输入。你可以从这里下载:

http://www.westmesatech.com/editv.html

例如:

set INPUT=
editv32 -p "Enter the input (default size is 50): " -n
if %ERRORLEVEL% EQU 2 set INPUT=50

退出代码2表示用户在没有输入任何内容的情况下按下了ENTER,因此在这种情况下,您将指定值50。

另请注意,如果您使用的是64位版本的Windows,则应使用EditV64.exe而不是EditV32.exe。

比尔