1401Views8Replies

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...

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
Cubit-GamesY
Cubit-GamesY

5 years ago

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

)

0
orksecurity
orksecurity

11 years ago

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.

0
TheBatchies
TheBatchies

Answer 11 years ago

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."

0
Psychic Master
Psychic Master

Answer 11 years ago

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

0
fwjs28
fwjs28

Answer 11 years ago

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

0
g-one
g-one

11 years ago

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

0
lemonie
lemonie

11 years ago

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

0
AndyGadget
AndyGadget

11 years ago

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).