BATCH programming help wanted

General software, Operating Systems, and Programming discussion.
Everything from software questions, OSes, simple HTML to scripting languages, Perl, PHP, Python, MySQL, VB, C++ etc.
Post Reply
Sparky•The•Elf
New Member
Posts: 12
Joined: Thu Jun 03, 2004 5:46 pm

BATCH programming help wanted

Post by Sparky•The•Elf »

I'm making a text adventure game using BATCH, and in order to make it work, I need to know how to get batch to add a line to an outside text document without bringing the document up, and I need to know to get it to take it away from that document, and finally I need to know how to make the program only perform an action if that line does or does not exist. I have windows xp. Thanks for any help
User avatar
Norm
SG VIP
Posts: 14195
Joined: Tue Mar 27, 2001 12:00 pm

Post by Norm »

Start>Run
%systemroot%\hh.exe mk:@MSITStore: %systemroot%\help\ms-its:ntcmds.chm::/ntcmds.htm

The above command brings up the WinXP CommandLine-Reference (in case you didn't know)

1."I need to know how to get batch to add a line to an outside text document without bringing the document up"

Look into using the "type' command.
You can have it redirect the contents of a file to another file.
eg:
If you create a file named test.txt and put a line of text into it you can have that text added to another file using the redirector >> to "append" or add the text to a newfile.

type test.txt>>newfile.txt
(copies the text in 'test.txt' to 'newfile.txt')

For answers to the other questions look into the 'find' command. You can locate 'strings' in files etc.
From there depending on your knowledge of Dos, and/or the commandline, you can figure out the rest.
I would detail it for you, but it's something I have never done, never had a reason to.

Good luck.
2speedy

Post by 2speedy »

Hi,

As the first reply says you can:

type someline >> firstfile

But if you want to get it out, things get more comples. Maybe this:

copy firstfile firstfile.sav
type someline >> firstfile

then if you want the original back:

copy firstfile.sav firstfile

As to checking to see if the added line is in the file, you can use find:

find "the string I added" firstfile
if errorlevel 1 [it's not there so do whatever]
goto end
[it is there so do something else]

**
Hope this helps
Sparky•The•Elf
New Member
Posts: 12
Joined: Thu Jun 03, 2004 5:46 pm

Thanks

Post by Sparky•The•Elf »

Sparky•The•Elf
New Member
Posts: 12
Joined: Thu Jun 03, 2004 5:46 pm

:)

Post by Sparky•The•Elf »

:) :) :) :) :)
this is an update to my last question, for anyone who cares, (probably nobody). To save anything to a file, just use echo combined with redirector commands!!


@echo off
:start
echo Enter a string to be saved to "savedstring.txt".
set /p string=
echo You entered %string%.
echo Now going to save %string% to a file.
pause
echo %string%>>savedstring.txt
goto start

the above code will create the folder called savedstring.txt, and will add the string you entered to it, and then allows you to add another string, and so on

the following 2 programs, run separately, would create and load a loadable text

program 1:

@echo off
:start
echo Enter a string to be saved.
set /p var=
echo You entered %var%.
echo Now going to save %var% to file.
pause
echo set var=%var%>>savednumber.dat

after running that program, load this one:


@echo off
:start
echo This program will allow you to load the text you saved.
echo Have you loaded it already? (y/n)
set /p choice=
set choice=%choice:~0,1%
if '%choice%'=='y' goto yes
if '%choice%'=='n' goto no
echo "%choice%" is not a valid option.
goto start
:no
pause
type savednumber.dat>>loadstring.bat
echo call loadnumber.cmd>>loadstring.bat
pause
call loadstring.bat
:yes
pause
echo The string you saved was: %var%.
pause


*pats himself on back*
btiessen
New Member
Posts: 1
Joined: Thu Apr 29, 2010 7:34 pm

Post by btiessen »

Thats awesome sparky!

But no matter what site i look at i cannot find anything that tells you advanced commands like

set choice=%choice:~0,1% ??? Where did you learn that? And what does it mean?

I am pretty decent at batch now, but im just limited by the information i can find on the web.

Thanks :)
Post Reply