Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off & CD "%~dp0"
- TITLE %~n0 & Setlocal
- %= Remark =%
- %= System to safely take user input through set /p, save all variables and reload on next use of program =%
- (Set LF=^
- %= NewLine variable to split set /p input line =%)
- %= create blank batch file to store and retieve variables in for automatic reload =%
- If not Exist "inputlog.bat" (Echo(@Echo Off>inputlog.bat & Goto :main)
- For %%A in ("[36m[[34mN[36m]ew","[[34mL[36m]oad[0m")Do Echo.%%~A
- Choice /N /C nl >nul
- If errorlevel 2 (Goto :load)
- Goto :Main
- :Input <VarName> <Optional - define valid values with each additional paramater>
- Set "variable=%~1"
- Setlocal EnableDelayedExpansion
- IF not "%~2"=="" For %%A in (%*) Do (If not "%%~A"=="%1" (Set "valid=%%~A !valid!"))
- :Force
- %= allow input of variable, test input, store for reloading. =%
- Echo([35m
- Set /P "input=!variable!!LF![36m{> [33m"
- IF "!input!"=="" (
- Echo([31m!variable! required.
- Goto :Force
- )
- IF "%~2"=="" (
- ECHO(Set "!variable!=!input!">>inputlog.bat
- Endlocal & Set "%variable%=%input%"
- Exit /B
- )
- For %%A in (!valid!) Do (
- If /I "!input!"=="%%~A" (
- ECHO(Set "!variable!=!input!">>inputlog.bat
- Endlocal & Set "%variable%=%input%"
- Exit /B
- )
- )
- Echo([31m!variable! required.
- Echo([36mSelect from:
- For %%A in (!valid!) Do (Echo([33m%%~A[0m)
- Goto :Force
- :main
- %= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
- Call :Input language english spanish
- %= Define multiple variables at once =%
- For %%A in ("name" "age" "location" "job" "hobbies") Do (Call :Input %%A)
- %= undefine local variables =%
- endlocal
- %= Display that variable definitions have been removed =%
- For %%A in ("language" "name" "age" "location" "job" "hobbies") Do Set %%A
- :load
- %= Reload the variables from the input Log and display. =%
- Call inputlog.bat
- %= Demonstrate that variables have been reloaded =%
- Setlocal EnableDelayedExpansion
- For %%A in ("language" "name" "age" "location" "job" "hobbies") Do (Echo([37m%%~A:!LF![36m[33m!%%~A![32m[0m)
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement