Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ VBscript ➜ Amending a .txt file

Amending a .txt file

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kelson   (5 posts)  Bio
Date Sun 29 Sep 2002 05:54 AM (UTC)
Message
Okay, I've been working for the past 30 or so hours on creating a plugin to make a directory full of .txt files of items. A few things to begin with. First, I am so far using the FileSystemObject code to create the .txt files.

Currently, I can take the data and create the <eq_name>.txt file with all of it except the things that repeat. Here is an example of the data the triggers use.

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Name : A circlet of ivy
Object: circlet ivy febriwyn
Level : 53
Type : armor Weight: 2
Flags : glow magic bless antievil antineutral burnproof
Value : 0 gold, 0 silver
Armor class: 9 pierce
9 bash
9 slash
4 vs. magic.
Affects: damroll by 5.
Affects: hitroll by 5.
Affects: wisdom by 1.
Affects: constitution by 1.
Affects: intelligence by 1.
Affects: ac by -4.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Now, I can get everything to work right except the affects, so I have set a seperate function to write each one as they are triggered then let the next one do the same. That, of course, requires they append to <eq_name>.txt instead of rewriting it.

Two side things. First, could someone tell me why this works: StatePath = World.GetInfo(60) & "state\" & World.GetPluginName & "\" & "jewelry\"
--> This one tosses in C:\...\jewelry\ directory

And this works: StatePath = World.GetInfo(60) & "state\" & World.GetPluginName & "\" & World.GetVariable("EQ_Type")
--> This one adds var EQ_Type to txt name

But this does not: StatePath = World.GetInfo(60) & "state\" & World.GetPluginName & "\" & World.GetVariable("EQ_Type") & "\"
--> This one SHOULD toss in C:\...\jewelry\ directory if EQ_Type = jewelry

I would really prefer to use a capture mode instead of the triggered method. What I mean is the user types idspell, this is aliases to run idStart and c ident *. idStart activates capture mode to get all the output from c ident * or it could capture all until idStop was run. Not sure how exactly I'd like to do it. For the affects I was wondering if there was any way for a multiline capture possibly. (ie, capture all information inside *-*-* lines)

Thanks in advance. I am brand spankin-new to VBscript but I have programmed in a variety of languages. The code overwrites var Affects that the trigger captures the value of Affects: into and the result is the code tells me I have only one Affect instead of x as I really do.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #1 on Sun 29 Sep 2002 08:38 PM (UTC)
Message
There are some minor similarities in what you are doing, and what I have done in my "AOD_EQ" script. (You can download my plugin from the URL in my sig.)

In my script, if there will be a variable number of lines to store, then I first read\write a number in the file, indicating how many lines to read. Specifically, the user can program any number of "extra" items to have in their inventory. If they have 10 items, then part way through my file, there will just be a line that reads "10", so that the script knows to read the next 10 lines as "extra" items.

Later in my script, I have an "eqfix" alias. It fixes all of my equipment, using a mud command called "eqcond", which tells you the state of your items. It looks like this:

eqcond

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[HEAD   ] a small basinet                      [      GOOD]
[NECK   ] a leather amulet (glowing)           [ QUITE BAD]
[ARMS   ] Vambraces of Ghaal                   [  MEDIOCRE]
[HANDS  ] Orange gloves (shimmering)           [      GOOD]
[RRING  ] Ring of Barbarian                    [   PERFECT]
[LRING  ] a small, plain ring                  [   PERFECT]
[LBODY  ] a red silk cloak decorated with a... [  MEDIOCRE]
[TORSO  ] A byrnie mail shirt                  [QUITE GOOD]
[BELT   ] Blue giant belt                      [QUITE GOOD]
[LEGS   ] servant's leggings                   [      GOOD]
[FEET   ] Boots of Great Power                 [  MEDIOCRE]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[WPN-R  ] Death Blade                          [   PERFECT]
[WPN-L  ] Bonestripper (Spinning)              [   TRASHED]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

In my case, I add a trigger (via script) to scan for the first column, which is the position of the item. Each time the trigger fires it calls a routine which stores the location in a variable. I seperate multiple item locations by using a "," in the variable.

I also start a timer before issuing the "eqcond" via script. The timer waits 3 seconds to allow the mud to present it's output, and for the step above. When the timer fires 3 seconds later, it calls a script which then acts on the items grabbed, and repairs them.

In your case, you could do a similar thing. Start a timer before you examine your item. Allow enough time for the mud command to execute. Use triggers to gather all the information, and then later when the timer executes, disable your temporary trigger(s), and save all the data. You could put a number in the file to indicate how many of the following lines relate to "Affects", or any other data where there is not a standard amount of lines to read/write.

Indeed, much of what you mentioned looks so much like my script, it looks like you've already borrowed from it. :) (You even use the same variable names!) I do a similar thing in regards to where I save the file (off the "state" path).

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #2 on Sun 29 Sep 2002 08:56 PM (UTC)

Amended on Sun 29 Sep 2002 09:02 PM (UTC) by Magnum

Message
Here's a little more sampling from my script, one of my files:

98d4f9b29e888f89004a29ee-6a6edfa2c71dae3c97f1c82c-Solo.txt

7
none|||none
bonestripper|||red
deathblade|||none
basinet|||blue
leather amulet|||blue
vambraces of ghaal|||blue
orange gloves|||blue
silk cloak|||blue
mail shirt|||blue
belt|||blue
servant leggings|||blue
boots of great power|||blue
plain ring|||blue
ring of barbarian|||none
none|||none
none|||none
none|||none
4
gems|||red
vial of blade poison|||red
vial of blade poison|||red
vial of blade poison|||red

The first line is the file name. I use the Player ID as well as the World ID because in my case, the data stored is specific to the player as well as the World name. (They are stored in a subdirectory which is the World name as well).

The first number, "7" is not a line count. It's an indicator of what level to repair items, based on a scale of the different damage states.

The next 17 lines are predictable, in that I always know there will be 17 lines, so I know exactly how many to write and read. I use a tripple bar ("|||") to seperate multiple elements on the same line. If there in an ANSI code to indicate 'end of field', I don't know what it is, or I probably would have used that instead.

After those 17 lines, comes the 'extra' items. This could be a variable number of items, so the following line in the file indicates how many of the following lines pertain to them.

This is just one way to do this, and I'm not claiming it's the most efficient... but it works for me. Good luck! :)

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #3 on Sun 29 Sep 2002 09:18 PM (UTC)
Message
Quote:

But this does not: StatePath = World.GetInfo(60) & "state\" & World.GetPluginName & "\" & World.GetVariable("EQ_Type") & "\"
--> This one SHOULD toss in C:\...\jewelry\ directory if EQ_Type = jewelry

Make sure you have all the proper path delimiters ("\") that you should.

Try doing a World.Note of the path and file name just before you attempt to open it, to verify it is correct, and help track where things may be going wrong. (I use 'World.Note' all over the place when trying to track bugs, then remove them afterwards).

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Kelson   (5 posts)  Bio
Date Reply #4 on Mon 30 Sep 2002 06:27 AM (UTC)
Message
Actually, when searching for how to make a database of any kind with vbscript in the manner I intended your's was the only useful code I could find! I took it, read it a dozen times, wrote some...refined...went back...pasted a bunch :)...editted some more, etc (btw, I already had you in the desc for being the originator of the code I was using...always try to give credit where credit is due, your code was a great help).

Right now I'm using the default (to set the base variables and spit out some code so I know the plugin initiated right), loadfile, and savefile sections of the code (the later two being heavily modified). The prefix is "" now instead of the long name you had as well. Now, the problem isn't in getting it to store the majority of the information, that works fine. It lies in capturing the multiple affects lines that appear (I have a second sub func working with it now that spits forth the world.notes (I have been using them quite often since I started this project to check values on variables) but can't append to .txt, it overwrites it).

For EQ_Type I know that it is using the right value, not sure why it isn't turning it into a path, if you could expand on that a bit. You have the World.GetPluginName before a "\" so I just copied that with the World.GetVariable("EQ_Type") and a "\"...that is what is confusing me as to why it doesn't work. I've even redefined it within the sub that writes onto the .txt file the value of EQ_Type, showing it is valid.

Thank you for your help
Top

Posted by Kelson   (5 posts)  Bio
Date Reply #5 on Mon 30 Sep 2002 06:31 AM (UTC)
Message
This is a copy of what my .txt file currently contains after checking an item.

C:\...\jewelry\A circlet of ivy.txt

Name : A circlet of ivy
Object: circlet ivy febriwyn
Level : 53
Type : armor
Flags : closeable
Armor class: 9 pierce
9 bash
9 slash
4 vs. magic.

Which is exactly what I want. If the affects do an open file (from your load sub) then attempt to write it overwrites it all instead of adding to it.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #6 on Mon 30 Sep 2002 05:31 PM (UTC)

Amended on Mon 30 Sep 2002 05:37 PM (UTC) by Magnum

Message
Ah, ok... In my original version, I do the full writing and reading in one fell swoop. I also open the file in such a way that it will overwrite an existing version. In my case, this is done on purpose, because the file is intended to be updated often.

If your script opens the file a second time to append to it, then you need to change the manner by which you open the file. This is easily done. From the VBS Documentation file:

CreateTextFile Method
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.

object.CreateTextFile(filename[, overwrite[, unicode]])

Arguments

object

Required. Always the name of a FileSystemObject or Folder object.

filename

Required. String expression that identifies the file to create.

overwrite

Optional. Boolean value that indicates if an existing file can be overwritten.
The value is True if the file can be overwritten;
False if it can't be overwritten.
If omitted, existing files are not overwritten.

unicode

Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file.
The value is True if the file is created as a Unicode file;
False if it's created as an ASCII file. If omitted, an ASCII file is assumed.

Remarks

The following code illustrates how to use the CreateTextFile method to
create and open a text file: 

Sub CreateAfile
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
   MyFile.WriteLine("This is a test.")
   MyFile.Close
End Sub

If the overwrite argument is False, or is not provided,
for a filename that already exists, an error occurs. 

So, basically, from these lines in my plugin:

EqFileName = StatePath & StatePrefix & World.GetVariable("EQType") & ".txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.CreateTextFile(EQFileName, True, False)

...you just need to change the "True" value to "False" instead, the second time you open the file to append to it.

Now, if you do the reverse when you read the file back, (opening the file twice), then you may have to find a way in the code to skip down to the point in the file where you want to start the second batch of reading. I'm sure that's possible though...

The help I quoted above came from an absolutely essential file for anyone who dabbles in Visual Basic Script. Being so profoundly useful to VBS programmers, it is of course impossible to find. I've spent hours looking for a link to the file at Microsoft.com. Maybe it's included with some bigger package, and that's why I can't find it individually.

For the time being, you can grab it here:

http://www.MagnumsWorld.com/ftp/VBSDocs.exe

I don't know how long I will maintain that link. It would be very nice if Nick could host that file. :)

As for your pathname problems, it's hard to offer assistance without seeing the code. If you could post the relative bits, I'll try and offer help. :)

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #7 on Mon 30 Sep 2002 05:45 PM (UTC)
Message
Heh... As I read over my posting, I realize I might be wrong about opening the file to append it it. (Because of the last line of the help file on this matter).

Download the file, look for the answer there. It's the same thing I would do if I had more time right now. :)

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #8 on Mon 30 Sep 2002 10:32 PM (UTC)
Message
Quote:

Right now I'm using the default (to set the base variables and spit out some code so I know the plugin initiated right), loadfile, and savefile sections of the code (the later two being heavily modified).


Once you start writing/appending to dozens of files, you might be better off using a database. There were some recent posts about this - search for the words 'database', 'access', 'create', 'table'.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Kelson   (5 posts)  Bio
Date Reply #9 on Tue 01 Oct 2002 12:43 AM (UTC)
Message
Until I find a nice database, I'd like to keep the .txt files I was making. First, it allows me to individually enter files for certain items I like or are limited, for example, to add in where or how to obtain it (for instance, the Robe of Escape [very, very, very rare in the mud] can only be obtained by performing a specific service for a mob in a certain area and only works once per reboot. The item is very good, however, so I would enter the "armor" directory, open the "Robe of Escape.txt" file and add in directions as to obtaining it and using it. Secondly it allows for easy sorting later on (click and drag to new folder...I'm so spoiled).

If someone could show me some nice looking database visuals (just a link or email would do nicely) since I've never had much luck trying to use them in the past.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #10 on Fri 04 Oct 2002 01:06 AM (UTC)
Message
Databases are funny things, and the better ones tend to separate out data handling from the visuals.

eg. MySQL is simply an SQL server, and you need to develop your own GUI (although I believe they have a project for that). However when it comes to handling large volumes of data it is very effective and reliable.

I wrote a PHP interface to MySQL (it is available from this site in the forum section) but you need to have PHP (and MySQL) going to use it (and for PHP you need a web server). However, if you have all that, then it uses web access to display your database in your web browser, and it is easy to add/change/delete records. I use it for the forum, and forum adminstration, as well as lots of other things inhouse.

What I am tempted to do is an interface to a database from MUSHclient (ie. make a more general set of database interfaces) which would be easy enough if anyone was interested.

eg. it would work a bit like this:

sql SELECT * FROM mytable ORDER BY item_type


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


27,043 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.