1135Views8Replies

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

Discussions

0
None
Cubit-GamesY

3 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
None
orksecurity

9 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
None
TheBatchiesorksecurity

Answer 8 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
None
fwjs28orksecurity

Answer 9 years ago

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

0
None
g-one

9 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
None
lemonie

9 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
None
AndyGadget

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