T3RRYT3RR0R

Input Label Navigation and Validation Batch / VBS

Dec 25th, 2019
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.37 KB | None | 0 0
  1. @echo off
  2.  
  3. Set This_File=%~0
  4.  
  5. :decide
  6. cls
  7. ECHO Enter Option1 or Option2
  8.  
  9. Call :Get_Input label YourTitle YourMessage
  10.  
  11. Call :TestLabel decide
  12.  
  13. ECHO disallowed,banned,restricted>"%temp%\labelnametest.txt"
  14.  
  15. Call :RestrictLabel decide
  16. GOTO decide
  17.  
  18. :option1
  19. ECHO Option 1
  20. pause
  21. goto decide
  22.  
  23. :option2
  24. ECHO Option 2
  25. pause
  26. goto decide
  27.  
  28. :banned
  29. ECHO Shouldn't Be Here
  30. pause
  31. goto decide
  32.  
  33. :disallowed
  34. ECHO Shouldn't Be Here
  35. pause
  36. goto decide
  37.  
  38. :restricted
  39. ECHO Shouldn't Be Here
  40. pause
  41. goto decide
  42.  
  43. :exit
  44. IF EXIST "%temp%\labelnametest.txt" (
  45. DEL /Q ""%temp%\labelnametest.txt""
  46. )
  47. exit
  48.  
  49. :TestLabel
  50. IF EXIST "%temp%\labelnametest.txt" (
  51. DEL /Q "%temp%\labelnametest.txt"
  52. )
  53.  
  54. FIND /I ":%label%" "%This_File%" >nul
  55. IF ERRORLEVEL ==1 ECHO Invalid Option! && Timeout 1 >nul && GOTO %1
  56.  
  57. GOTO :EOF
  58.  
  59. :RestrictLabel
  60. FIND /I "%label%" "%temp%\labelnametest.txt" >nul
  61. IF ERRORLEVEL ==1 (
  62.     goto %label% 2>nul
  63.     ) else (
  64.     ECHO Disallowed! && Timeout 1 >nul && GOTO %1
  65. )
  66.  
  67. GOTO :EOF
  68.  
  69. :Get_Input
  70. ::: Using this Function:
  71. ::: CALL :Get_Input YourVariable YourTitle YourMessage
  72.  
  73. ::: Assign the variable (Passed by call Argument) to be set and tested
  74.  
  75. SET "Fetch_Variable=%~1"
  76. SET "My_Title=%~2"
  77. SET "My_Message=%~3"
  78.  
  79. ::: Ensure Undefined Value for Input
  80.  
  81. SET input=
  82.  
  83. :main
  84.  
  85. ::: Exit Filter once Acceptable Variable is Set
  86.  
  87. IF DEFINED input GOTO return
  88.  
  89. ::: Create and start VBS script to Launch an Input Box and Store the input to text file for retrieval from this Batch program.
  90.  
  91. (
  92. ECHO Dim objFSO 'File System Object
  93. ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
  94. ECHO Dim objTS 'Text Stream Object
  95. ECHO Const ForWriting = 2
  96. ECHO Set objTS = objFSO.OpenTextFile("%temp%\inputTest.txt", ForWriting, True^)
  97. ECHO objTS.Write(InputBox("%My_Message%","%My_Title%","Enter %Fetch_Variable%"^)^)
  98. ECHO objTS.Close(^)
  99. ECHO Set bjFSO = Nothing 'Destroy the object.
  100. ECHO Set objTS = Nothing 'Destroy the object.
  101. ) >"%temp%\GetInput.vbs"
  102.  
  103. START /wait %temp%\GetInput.vbs
  104.  
  105. ::: Start /wait required to hold the batch until input is returned
  106.  
  107. :loadinput
  108.  
  109. ::: Insist on user Input prior to attempting to load input.
  110.  
  111. IF NOT EXIST "%temp%\inputTest.txt" GOTO invInput
  112.  
  113. <%temp%\inputTest.txt (
  114. SET /P input=
  115. )
  116.  
  117. ::: Test to ensure Variable defined. Needed here to prevent environment variable not defined message ahead of next test Should the variable not be defined.
  118.  
  119. IF NOT DEFINED input GOTO invInput
  120.  
  121. ::: Test for Doublequotes and reset variable if present
  122.  
  123. SET Input | FIND """" >NUL
  124. IF NOT ERRORLEVEL 1 SET Input=
  125.  
  126. IF NOT DEFINED input GOTO invInput
  127.  
  128. SET Input | FIND "~" >NUL
  129. IF NOT ERRORLEVEL 1 SET Input=
  130.  
  131. IF NOT DEFINED input GOTO invInput
  132.  
  133. ::: Test for Spaces (REM out if not required)
  134.  
  135. IF NOT "%input%"=="%input: =%" GOTO invInput
  136.  
  137. ::: Test for all other standard Symbols. (REM out any you wish to permit)
  138.  
  139. IF NOT "%input%"=="%input:&=%" GOTO invInput
  140. IF NOT "%input%"=="%input:(=%" GOTO invInput
  141. IF NOT "%input%"=="%input:)=%" GOTO invInput
  142. IF NOT "%input%"=="%input:<=%" GOTO invInput
  143. IF NOT "%input%"=="%input:>=%" GOTO invInput
  144. IF NOT "%input%"=="%input:{=%" GOTO invInput
  145. IF NOT "%input%"=="%input:}=%" GOTO invInput
  146. IF NOT "%input%"=="%input:]=%" GOTO invInput
  147. IF NOT "%input%"=="%input:[=%" GOTO invInput
  148. IF NOT "%input%"=="%input:#=%" GOTO invInput
  149. IF NOT "%input%"=="%input:^=%" GOTO invInput
  150. IF NOT "%input%"=="%input:+=%" GOTO invInput
  151. IF NOT "%input%"=="%input:-=%" GOTO invInput
  152. IF NOT "%input%"=="%input:/=%" GOTO invInput
  153. IF NOT "%input%"=="%input:\=%" GOTO invInput
  154. IF NOT "%input%"=="%!!|%" GOTO invInput
  155. IF NOT "%input%"=="%input:$=%" GOTO invInput
  156. IF NOT "%input%"=="%input:!=%" GOTO invInput
  157. IF NOT "%input%"=="%input:?=%" GOTO invInput
  158. IF NOT "%input%"=="%input:@=%" GOTO invInput
  159. IF NOT "%input%"=="%input:'=%" GOTO invInput
  160. IF NOT "%input%"=="%input:,=%" GOTO invInput
  161. IF NOT "%input%"=="%input:.=%" GOTO invInput
  162. IF NOT "%input%"=="%!!%" GOTO invInput
  163. IF NOT "%input%"=="%input:`=%" GOTO invInput
  164.  
  165. GOTO main
  166.  
  167. :invInput
  168.  
  169. SET input=
  170.  
  171. IF EXIST "%temp%\GetInput.vbs" (
  172. DEL /Q "%temp%\GetInput.vbs"
  173. )
  174.  
  175. GOTO main
  176.  
  177. :return
  178.  
  179. ::: assigns the input value to the variable name being validated.
  180.  
  181. SET %Fetch_Variable%=%input%
  182.  
  183. SET input=
  184.  
  185. IF EXIST "%temp%\inputTest.txt" (
  186. DEL /Q "%temp%\inputTest.txt"
  187. )
  188.  
  189. IF EXIST "%temp%\GetInput.vbs" (
  190. DEL /Q "%temp%\GetInput.vbs"
  191. )
  192.  
  193. GOTO :EOF
Advertisement
Add Comment
Please, Sign In to add comment