Advertisement
T3RRYT3RR0R

Advanced batch input saving and selection

May 21st, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.05 KB | None | 0 0
  1. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.     @Echo off & CD "%~dp0" & TITLE Extractor %~n0 & Setlocal & REM Extractor by T3RRY                     :::
  3. :::                                                                                                           :::  
  4. ::: - Program demonstrating the following principles:                                                         :::
  5. :::  - Multiline display via Caret with LF variable and Delayed Expansion                                     :::
  6. :::  - Same Line Mulitocolor Display using Ascii Escape Code                                                  :::
  7. :::  - Safe input with Validation using Set /P, Doublequoting, Delayed Expansion and Conditional Checks       :::
  8. :::  - Loading of contents of text files into array for user selection and use by a Choice 'Scroll' Function. :::
  9. :::                                                                                                           :::
  10. ::: - The techniques in this demo have practical application for scripts which require user input to be       :::
  11. :::   stored and selectively retrieved in future executions of the script.                                    :::
  12. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  13.  
  14.  
  15.     (Set LF=^
  16.  
  17.  
  18.     %= NewLine variable to split set /p input line =%)
  19.  
  20. ::: / Creates variable /AE = Ascii-27 escape code.
  21. ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
  22. ::: - https://stackoverflow.com/a/34923514/12343998
  23. :::
  24. ::: - /AE can be used  with and without DelayedExpansion.
  25.     Setlocal
  26.     For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  27.         Endlocal
  28.         Set "/AE=%%a"
  29.     )
  30. ::: \
  31.  
  32. ::: - create blank batch file to store and Selectively retieve variables with
  33. :Menu
  34.     CLS
  35.     If not Exist "inputURLlog.log" (Break>inputURLlog.log & Goto :main)
  36.     For %%A in ( "%/AE%[36m[%/AE%[35mN%/AE%[36m]ew" , "[%/AE%[35mL%/AE%[36m]oad%/AE%[0m")Do Echo.%%~A
  37.     Choice /N /C nl >nul
  38.     If "%errorlevel%" == "2" (Goto :load)  
  39. Goto :Main
  40.  
  41. :Input <VarName>
  42.     Set "variable=%~1"
  43.     Setlocal EnableDelayedExpansion
  44.  
  45. :Validate
  46. ::: - allow input of variable, test input, store for reuse.
  47.  
  48.     Echo(%/AE%[35m
  49.     Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
  50.     If "!input!"=="" (
  51.         Echo.%/AE%[31m!variable! required.!LF!%/AE%[33m{%/AE%[37m e exit q quit c close %/AE%[33m} To Exit!LF!%/AE%[33m{%/AE%[37m m menu b back r return %/AE%[33m} For previous Menu!LF!%/AE%[33m{%/AE%[37m l load o open s start %/AE%[33m} To select and start an url
  52.     Pause > Nul & Goto :Validate)
  53.     For %%I in (e exit q quit c close) do IF /I "!input!"=="%%I" (EXIT)
  54.     For %%I in (m menu b back r return) do IF /I "!input!"=="%%I" (Goto :menu)
  55.     For %%I in (l load o open s start) do IF /I "!input!"=="%%I" (Goto :load)
  56.     IF /I "!input!"=="load" (Goto :load)
  57. ::: - Url Validation - Optional
  58.     (ping !input!) > Nul || (Echo Url not valid & Endlocal & Goto :main)
  59.     ECHO(!input!>> "inputURLlog.log"
  60.     Endlocal
  61.     Exit /B
  62.    
  63. :main
  64.     Call :Input Url
  65. Goto :main
  66.  
  67. :load
  68.     Setlocal EnableDelayedExpansion
  69.  
  70.     Set lines=0
  71.     For /f "Tokens=* Delims=" %%A in (inputURLlog.log) do (
  72.         Set /A lines+=1
  73.         Set "line[!lines!]=%%A"
  74.     )
  75. REM Set "#=%lines%" & REM start from newest entry
  76.     Set "#=1" & REM start from oldest entry
  77. :extract
  78.  
  79.     For %%A in (!#!) do (
  80.     Cls
  81.     Title Option %%A of !lines!
  82.     Echo(%/AE%[32mUrl: %/AE%[33m!line[%%A]!
  83.     For %%B in ("%/AE%[36m[%/AE%[35mS%/AE%[36m]tart" , "[%/AE%[35mN%/AE%[36m]ext" "[%/AE%[35mL%/AE%[36m]ast" "[%/AE%[35mM%/AE%[36m]enu%/AE%[0m")Do Echo.%%~B
  84.     Choice /N /C LNSM
  85.     If "!errorlevel!"=="4" (Endlocal & Goto :Menu)
  86.     If "!errorlevel!"=="3" (Set "Url=!line[%%A]!" & Goto :Selected)
  87.     If "!errorlevel!"=="2" (IF not !#! GEQ !lines! (Set /A #+=1) Else (Set "#=1"))
  88.     If "!errorlevel!"=="1" (IF not !#! LEQ 1 (Set /A #-=1) Else (Set "#=%lines%"))
  89.     )
  90.     Goto :extract
  91.    
  92. ::: - Demonstrate that variable has been reloaded
  93. :Selected
  94.     start "" !Url!
  95.     Pause >nul
  96.     Endlocal
  97. GOTO :load
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement