Advertisement
T3RRYT3RR0R

Batch user input Save Reload system.

Apr 3rd, 2020
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.10 KB | None | 0 0
  1. @Echo off & CD "%~dp0"
  2. TITLE %~n0 & Setlocal
  3.  
  4. %= Remark =%
  5. %= System to safely take user input through set /p, save all variables and reload on next use of program =%
  6.  
  7.     (Set LF=^
  8.  
  9.  
  10. %= NewLine variable to split set /p input line =%)
  11.  
  12. %= create blank batch file to store and retieve variables in for automatic reload =%
  13.  
  14.     If not Exist "inputlog.bat" (Echo(@Echo Off>inputlog.bat & Goto :main)
  15.     For %%A in ("[N]ew","[L]oad")Do Echo.%%~A
  16.     Choice /N /C nl >nul
  17.     If errorlevel 2 (Goto :load)   
  18.     Goto :Main
  19.  
  20. :Input <VarName> <Optional - define valid values with each additional paramater>
  21.     Set "variable=%~1"
  22.     Setlocal EnableDelayedExpansion
  23.     IF not "%~2"=="" For %%A in (%*) Do (If not "%%~A"=="%1" (Set "valid=%%~A !valid!"))
  24.  
  25. :Force
  26. %= allow input of variable, test input, store for reloading. =%
  27.  
  28.     Echo(
  29.     Set /P "input=!variable!!LF!{> "
  30.     IF "!input!"=="" (
  31.         Echo(!variable! required.
  32.         Goto :Force
  33.     )
  34.     IF "%~2"=="" (
  35.         ECHO(Set "!variable!=!input!">>inputlog.bat
  36.         Endlocal & Set "%variable%=%input%"
  37.         Exit /B
  38.     )
  39.  
  40.     For %%A in (!valid!) Do (
  41.         If /I "!input!"=="%%~A" (
  42.                 ECHO(Set "!variable!=!input!">>inputlog.bat
  43.                 Endlocal & Set "%variable%=%input%"
  44.                 Exit /B
  45.         )
  46.     )
  47.     Echo(!variable! required.
  48.     Echo(Select from:
  49.     For %%A in (!valid!) Do (Echo(%%~A)
  50.     Goto :Force
  51.  
  52. :main
  53. %= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
  54.     Call :Input language english spanish
  55.  
  56. %= Define multiple variables at once =%
  57.     For %%A in ("name" "age" "location" "job" "hobbies") Do (Call :Input %%A)
  58.  
  59. %= undefine local variables =%
  60.     endlocal
  61. %= Display that variable definitions have been removed =%
  62.     For %%A in ("language" "name" "age" "location" "job" "hobbies") Do Set %%A
  63.  
  64. :load
  65. %= Reload the variables from the input Log and display. =%
  66.     Call inputlog.bat
  67. %= Demonstrate that variables have been reloaded =%
  68.     Setlocal EnableDelayedExpansion
  69.     For %%A in ("language" "name" "age" "location" "job" "hobbies") Do (Echo(%%~A:!LF!!%%~A!)
  70.     pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement