1001Views8Replies

Author Options:

how can a batch file be written to remember text from the previous time it was run? Answered

such as if i were to input:"hello ow are you" could the batch file remember this line the next time it was run even if it was a year later...

8 Replies

user
Cubit-GamesY (author)2015-06-22

I use .txt files, for example, here is a program called Name.bat that when ran it will ask you for your name, then the next time its ran it will tell you your name as you previously entered in its past run:

@echo off

if exist name.txt (

set /p name<name.txt

echo Your Name Is %name%!

pause

)

if not exist name.txt (

echo Hi! Whats Your Name?

set /p name=Name:

@echo %name%>name.txt

exit

)

Select as Best AnswerUndo Best Answer

user
orksecurity (author)2009-07-16

Short term, environment variables. Longer term, use the variable value to build a second batch file which sets an environment variable, and have this batchfile call the other. Better answer: Switch from batchfiles to a more serious language.

Select as Best AnswerUndo Best Answer

user
TheBatchies (author)orksecurity2010-02-23

Hey man, he asked how to do this in batch. Please answer in batch, otherwise don't answer, because I get that a lot, the "Batch is seriously not a language."

Select as Best AnswerUndo Best Answer

user

could you give a more serious language that is easy to learn?

Select as Best AnswerUndo Best Answer

user
fwjs28 (author)orksecurity2009-07-16

batch isn't even a language...although i do get you're point, Batch is a highly useful tool...

Select as Best AnswerUndo Best Answer

user
g-one (author)2009-07-16

Try this batch code. It will write inputs in the own file. Note that the number behind the option "skip" is the count of lines of the actual batch code.

@echo off &setlocal
for /f "skip=7 tokens=*" %%i in ('type "%~0"') do set "txt=%%i"
if defined txt echo Your last input was "%txt%".
echo.
set /p "x=Your new Input: "
if defined x echo %x%>>"%~0"
goto :eof

Select as Best AnswerUndo Best Answer

user
lemonie (author)2009-07-16

You're stretching things a bit here (as other comments). A batch file is just a list of commands, it's not a programming language. L

Select as Best AnswerUndo Best Answer

user
AndyGadget (author)2009-07-16

You need to use redirection which is telling a command to take its input or send its output to a device (or file) other than the screen or keyboard.

Simplest form ;-

echo Hello how are you > hello.txt
type hello.txt

Using > overwrites an existing file.
Using >> appends (sticks it on the end) an existing file (creates it if necessary).

Select as Best AnswerUndo Best Answer