SHARE
TWEET

Ref: http://bit.ly/1PCowls ReadFormattedLine

relay12 Feb 4th, 2016 (edited) 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2.  
  3. rem ReadFormattedLine.bat: Read a line from keyboard with specific format
  4. rem Antonio Perez Ayala
  5.  
  6. cls
  7. echo Read a line from keyboard with specific format
  8. echo/
  9. echo call :ReadFormattedLine var="mask"  [/M "message"]  [/P]  [/A^|/W^|/F]
  10. echo/
  11. echo The mask specify valid input characters per position via the following chars:
  12. echo/
  13. echo    #  -  Any digit
  14. echo    _  -  Any letter
  15. echo    +  -  A letter that is converted to uppercase
  16. echo    ?  -  Any letter or digit
  17. echo    @  -  Letter or digit, convert letter to uppercase
  18. echo/
  19. echo The following characters are just displayed/inserted at their positions:
  20. echo    $  /  \  (  )  [  ]  :  !!  ,  .  -  space  letters  digits
  21. echo/
  22. echo Any character in the mask different than previous ones cause an error.
  23. echo/
  24. echo If /P (password) switch is given, input characters are displayed as asterisks.
  25. echo/
  26. echo Normally the input is completed when Enter key is pressed after read at least
  27. echo one character, but the following switches changes this behavior.
  28. echo/
  29. echo    /A (auto):   Input is auto-completed after the last character; Enter key
  30. echo                 is ignored.
  31. echo    /W (whole):  Enter key is accepted at first or last input positions only,
  32. echo                 that is, when input is empty or whole.
  33. echo    /F (fields): Enter key fills the field with spaces and move the cursor to
  34. echo                 the next input field in the line.
  35. echo/
  36. echo To input a whole value terminated by Enter, use /W switch and insert any
  37. echo character at the first position in the mask.
  38. echo/
  39. echo/
  40. echo Some examples:
  41. echo/
  42.  
  43. setlocal
  44. rem Define the following variable before call the subroutine
  45. set "thisFile=%~F0"
  46. call :ReadFormattedLine test="(_-#-+-#-_)" /M "Enter (_-#-+-#-_): " /W
  47. echo Read: "%test%"
  48. echo/
  49. call :ReadFormattedLine number="#####" /M "Enter a number of up to 5 digits: "
  50. echo Number: "%number%"
  51. echo/
  52. call :ReadFormattedLine pass="########" /M "Enter your password (8 digits): " /P /A
  53. echo Password: "%pass%"
  54. echo/
  55. call :ReadFormattedLine phone="(###)###-####" /M "Enter the telephone with area code: " /W
  56. echo Telephone: "%phone%"
  57. echo/
  58. call :ReadFormattedLine RFC="[++++-######-@@@]" /M "Enter your RFC [4letters-6digits-3alpha]: " /W
  59. echo RFC: "%RFC%"
  60. echo/
  61. call :ReadFormattedLine timeStamp="####/##/##  HH:MM: ##:##" /M "Enter a time-stamp.  YYYY/MM/DD: " /W
  62. echo Time-stamp: "%timeStamp%"
  63. echo/
  64. call :ReadFormattedLine name="+_______________  Last name: +_______________" /M "First name: " /F
  65. echo Name: "%name%"
  66. echo/
  67. echo Enter the list of IP addresses
  68. echo (press Enter in an empty line to end the list)
  69. set i=0
  70. :nextIP
  71.    set /A i+=1
  72.    call :ReadFormattedLine IP[%i%]="###.###.###.###" /M "%i%-  " /W
  73. if defined IP[%i%] goto nextIP
  74. set /A n=i-1
  75. echo/
  76. echo IP addresses read: %n%
  77. set IP[
  78. echo/
  79. echo/
  80. echo End of examples
  81. pause
  82. goto :EOF
  83.  
  84.  
  85. :ReadFormattedLine var="mask" [/M "message"] [/P] [/F /W /A]
  86.  
  87. if "%~2" equ "" echo ERROR: Missing parameters & exit /B 1
  88. setlocal EnableDelayedExpansion
  89.  
  90. set "var=%~1"
  91. set "mask=%~2"
  92. shift & shift
  93. set "message="
  94. if /I "%1" equ "/M" set "message=%~2" & shift & shift
  95. set "password="
  96. if /I "%1" equ "/P" set "password=1" & shift
  97. set "switch=%~1"
  98.  
  99. set quote="
  100. set "digit= 0 1 2 3 4 5 6 7 8 9 "
  101. set "letter= A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
  102. set "alphaNum=%digit%%letter%"
  103. set "fmt=#_+?@"
  104. set "show=$/\()[]:;,.- %digit: =%%letter: =%"
  105. for /F %%a in ('copy /Z "%thisFile%" NUL') do set "CR=%%a"
  106. for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a" & set "SP=.%%a "
  107.  
  108. < NUL (
  109.    set /P "=%message%"
  110.    for /F "eol=| delims=" %%a in ('cmd /U /C echo !mask!^| find /V ""') do (
  111.       if "!fmt:%%a=!" neq "%fmt%" (
  112.          set /P "=Û"
  113.       ) else if "%%a" neq " " (
  114.          set /P "=%%a"
  115.       ) else (
  116.          set /P "=!SP!"
  117.       )
  118.    )
  119.    set /P "=!SP!!CR!%message%"
  120. )
  121. set "input="
  122. set /A i=0, key=0
  123. goto checkFormat
  124.  
  125. :nextKey
  126.    set "key="
  127.    for /F "delims=" %%a in ('xcopy /W "%thisFile%" "%thisFile%" 2^>NUL') do if not defined key set "key=%%a"
  128.    if "!key:~-1!" neq "!CR!" goto endif
  129.       if /I "%switch%" equ "/A" goto nextKey
  130.       if /I "%switch%" neq "/F" goto check/W
  131.          :nextField
  132.          set "format=!mask:~%i%,1!"
  133.          if "%format%" equ "" goto endRead
  134.          if "!fmt:%format%=!" equ "%fmt%" goto checkFormat
  135.          set /P "=Û" < NUL
  136.          set "input=%input% "
  137.          set /A i+=1
  138.          goto nextField
  139.       :check/W
  140.       if /I "%switch%" neq "/W" goto checkEmpty
  141.          if %i% equ 0 goto endRead
  142.          if "%format%" equ "" goto endRead
  143.          goto nextKey
  144.       :checkEmpty
  145.       if %i% gtr 0 goto endRead
  146.       goto nextKey
  147.    :endif
  148.    set "key=!key:~-1!"
  149.    if "!key!" equ "!BS!" (
  150.       if %i% gtr 0 (
  151.          if "%format%" equ "" (
  152.             set /P "=!SP!!BS!!BS!Û!BS!" < NUL
  153.          ) else (
  154.             set /P "=Û!BS!!BS!Û!BS!" < NUL
  155.          )
  156.          set "input=%input:~0,-1%"
  157.          set /A i-=1
  158.          if !i! equ 0 set key=0
  159.       )
  160.       goto checkFormat
  161.    )
  162.    if "%format%" equ "" goto nextKey
  163.    if "!key!" equ "=" goto nextKey
  164.    if "!key!" equ "!quote!" goto nextKey
  165.    if "%format%" equ "#" ( rem Any digit
  166.       if "!digit: %key% =!" equ "%digit%" goto nextKey
  167.    ) else if "%format%" equ "_" ( rem Any letter
  168.       if "!letter: %key% =!" equ "%letter%" goto nextKey
  169.    ) else if "%format%" equ "+" ( rem Any letter, convert it to uppercase
  170.       if "!letter: %key% =!" equ "%letter%" goto nextKey
  171.       for %%a in (%letter%) do if /I "!key!" equ "%%a" set "key=%%a"
  172.    ) else (
  173.      rem Rest of formats are alphanumeric: ? @
  174.       if "!alphaNum: %key% =!" equ "%alphaNum%" goto nextKey
  175.       if "%format%" equ "@" ( rem Convert letters to uppercase
  176.          for %%a in (%letter%) do if /I "!key!" equ "%%a" set "key=%%a"
  177.       ) else if "%format%" neq "?" echo ERROR: Invalid format in mask: "%format%" & exit /B 2
  178.       )
  179.    )
  180.    if defined password (
  181.       set /P "=*" < NUL
  182.    ) else (
  183.       set /P "=%key%" < NUL
  184.    )
  185.    set "input=%input%%key%"
  186.  
  187.    :nextFormat
  188.    set /A i+=1
  189.    :checkFormat
  190.    set "format=!mask:~%i%,1!"
  191.    if "%format%" equ "" (
  192.       if /I "%switch%" equ "/A" goto endRead
  193.       if /I "%switch%" equ "/M" goto endRead
  194.       goto nextKey
  195.    )
  196.    if "!show:%format%=!" neq "%show%" (
  197.       if "!key!" equ "!BS!" (
  198.          if "%format%" neq " " (
  199.             set /P "=%format%!BS!!BS!Û!BS!" < NUL
  200.          ) else (
  201.             set /P "=!SP!!BS!!BS!Û!BS!" < NUL
  202.          )
  203.          set "input=%input:~0,-1%"
  204.          set /A i-=1
  205.          if !i! equ 0 set key=0
  206.          goto checkFormat
  207.       ) else (
  208.          if "%format%" neq " " (
  209.             set /P "=%format%" < NUL
  210.          ) else (
  211.             set /P "=!SP!" < NUL
  212.          )
  213.          set "input=%input%%format%"
  214.          goto nextFormat
  215.       )
  216.    )
  217.    if "%input:~-1%!key!" equ " !BS!" (
  218.       set /P "=Û!BS!!BS!" < NUL
  219.       set "input=%input:~0,-1%"
  220.       set /A i-=1
  221.       goto checkFormat
  222.    )
  223.  
  224. goto nextKey
  225. :endRead
  226. echo/
  227. endlocal & set "%var%=%input%"
  228. exit /B
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top