Way to save a /p to a new .txt file?
Moderator: DosItHelp
Way to save a /p to a new .txt file?
Is there a way to save /p to a .txt file that is made by the script, I'm making kind of an late 1980s OS thing that has a notepad feature, and I want to make it so what the user writes is saved to a file.
-
- Posts: 11
- Joined: 16 Nov 2016 12:03
Re: Way to save a /p to a new .txt file?
Something like this?
Code: Select all
@echo off
echo user response
set /p "a="
echo %a% > newtxt.txt
Re: Way to save a /p to a new .txt file?
For the purpose described, which is something thats easily reusable for all user input, I believe something like this subroutine is more what you seek. It forces a value to be Set to the variable, allows restriction of responses, and autosaves all input to a .bat file that can be called for easy loading.
Instead of scripting set /P varname=prompt, and the follow up verification /write actions each time you take user input, just expand the macro for the call to the subroutine that performs all the above mentioned actions with %$Set%
Instead of scripting set /P varname=prompt, and the follow up verification /write actions each time you take user input, just expand the macro for the call to the subroutine that performs all the above mentioned actions with %$Set%
Code: Select all
@Echo off & CD "%~dp0"
TITLE %~n0
::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: - https://stackoverflow.com/a/34923514/12343998
:::
::: - /AE can be used with and without DelayedExpansion.
Setlocal
For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
Endlocal
Set "/AE=%%a"
)
::: \
Set "$Set=Call :Input"
Setlocal
%= Remark =%
(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" (Goto :main)
Echo.%/AE%[36m[%/AE%[34mN%/AE%[36m]ew [%/AE%[34mL%/AE%[36m]oad%/AE%[0m
Choice /N /C nl >nul
If errorlevel 2 (Goto :load)
Del /Q "inputlog.bat"
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!"))
:Confirm
%= allow input of variable, test input, store for reloading. =%
Echo(%/AE%[35m
Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
IF "!input!"=="" (
Echo(%/AE%[31m!variable! required.
Goto :Confirm
)
IF "%~2"=="" (
If not Exist "inputlog.bat" >inputlog.bat Echo.@Echo Off
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
For %%A in (!valid!) Do (
If /I "!input!"=="%%~A" (
If not Exist "inputlog.bat" >inputlog.bat Echo.@Echo Off
ECHO(Set "!variable!=!input!">>inputlog.bat
Endlocal & Set "%variable%=%input%"
Exit /B
)
)
Echo(%/AE%[31m!variable! required.
Echo(%/AE%[36mSelect from:
For %%A in (!valid!) Do (Echo(%/AE%[33m%%~A%/AE%[0m)
Goto :Confirm
:main
%= define variable (parameter 1) and restrict definition of variables to specified values (additional parameters) =%
%$Set% language english spanish
%= Define multiple variables at once =%
For %%A in ("name" "age" "location" "job" "hobbies") Do (%$Set% %%A)
%= undefine local variables and show variable definitions have been removed =%
endlocal
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(%/AE%[37m%%~A:!LF!%/AE%[36m%/AE%[33m!%%~A!%/AE%[32m%/AE%[0m)
pause
Re: Way to save a /p to a new .txt file?
Depending on what is meant with "making kind of an late 1980s OS thing" you might want to use the copy command:
You simpyl type in anything you want (that includes <return>), except a lonesome <SUB> character, which you can type in by pressing <CTRL>+<Z>.
penpen
Code: Select all
copy con "sample.txt"
penpen