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 ➜ Writing to file?

Writing to file?

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


Posted by Stoned00d   (14 posts)  Bio
Date Tue 30 Jul 2002 05:22 PM (UTC)
Message
How do you write to (and access) a text file in VBscript? Additionally, is there an append command?
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #1 on Tue 30 Jul 2002 08:49 PM (UTC)
Message
Search the forums here for a link, or search the internet, and you should be able to find a VBScript Documentation file, which can be very useful for trying to learn things on your own. I'm sorry, I don't know the link myself.

In the meantime, here's a subroutine from one of my scripts:

Sub EQ_Save_File (thename, theoutput, arrWildcards)
	Dim arrItems
	Dim EqItem, EqStore
	Dim FSO, EqFileName
	Dim F
	Dim x
	EqFileName = ScriptPath & "AOD_EQ_" & World.GetVariable("EQType") & ".txt"
	Set FSO = CreateObject("Scripting.FileSystemObject")
	Set F = FSO.CreateTextFile(EQFileName, True, False)
	F.WriteLine World.GetVariable("EqFixLevel")
	arrItems = Split(EQItemList, ", ")
	For x = LBound(arrItems) to UBound(arrItems)
		EqItem = "EQ_" & arrItems(x)
		EqStore = "EQstr_" & arrItems(x)
		F.WriteLine World.GetVariable(EqItem) & "|||" & World.GetVariable(EqStore)
	Next
	F.WriteLine World.GetVariable("EQTotalExtras")
	If CInt(World.GetVariable("EQTotalExtras")) > 0 Then
		For x = 1 to CInt(World.GetVariable("EQTotalExtras"))
			EqItem = "EQ_x_" & CStr(x)
			EqStore = "EQstr_x_" & CStr(x)
			F.WriteLine World.GetVariable(EqItem) & "|||" & World.GetVariable(EqStore)
		Next
	End If
	F.Close
	Set F = Nothing
	Set FSO = Nothing
End Sub

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 Tue 30 Jul 2002 08:52 PM (UTC)
Message
Oh, here's the other half, the read subroutine... although it does more than that. If the file is not found, it initializes all the data it handles.

Sub EQ_Load_File (thename, theoutput, arrWildcards)
	Dim arrItems
	Dim EqItem, EqStore
	Dim FSO, EqFileName
	Dim arrFileContents
	Dim F
	Dim x
	If CInt(World.GetVariable("EQTotalExtras")) > 0 Then
		For x = 1 to CInt(World.GetVariable("EQTotalExtras"))
			World.DeleteVariable "EQ_x_" & CStr(x)
			World.DeleteVariable "EQstr_x_" & CStr(x)
		Next
	End If
	EqFileName = ScriptPath & "AOD_EQ_" & World.GetVariable("EQType") & ".txt"
	arrItems = Split(EQItemList, ", ")
	Set FSO = CreateObject("Scripting.FileSystemObject")
	If (FSO.FileExists(EqFileName)) Then
		Set F = FSO.OpenTextFile(EqFileName, 1)
		World.SetVariable "EqFixLevel", F.ReadLine
		World.SetVariable "EqFixLevelStatus", "[" & World.GetVariable("EqFixLevel") & "] " _
			& EQ_MapDamageLevel(World.GetVariable("EqFixLevel"))
		For x = LBound(arrItems) to UBound(arrItems)
			EqItem = "EQ_" & arrItems(x)
			EqStore = "EQstr_" & arrItems(x)
			arrFileContents = Split(F.ReadLine, "|||")
			World.SetVariable EqItem, arrFilecontents(0)
			World.SetVariable EqStore, arrFilecontents(1)
		Next
		World.SetVariable "EQTotalExtras", F.ReadLine
		If CInt(World.GetVariable("EQTotalExtras")) > 0 Then
			For x = 1 to CInt(World.GetVariable("EQTotalExtras"))
				EqItem = "EQ_x_" & CStr(x)
				EqStore = "EQstr_x_" & CStr(x)
				arrFileContents = Split(F.ReadLine, "|||")
				World.SetVariable EqItem, arrFilecontents(0)
				World.SetVariable EqStore, arrFilecontents(1)
			Next
		End If
		F.Close
		Set F = Nothing
		Set FSO = Nothing
	Else
		For x = LBound(arrItems) to UBound(arrItems)
			EqItem = "EQ_" & arrItems(x)
			EqStore = "EQstr_" & arrItems(x)
			World.SetVariable EqItem, "none"
			World.SetVariable EqStore, "none"
		Next
		World.SetVariable "EqFixLevel", 7
		World.SetVariable "EqFixLevelStatus", "[" & World.GetVariable("EqFixLevel") & "] " _
			& EQ_MapDamageLevel(World.GetVariable("EqFixLevel"))
		World.SetVariable "EQTotalExtras", 0
		EQ_Save_File "EQ_Load_File", theoutput, arrWildcards
	End If
End Sub

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,173 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 30 Jul 2002 09:40 PM (UTC)
Message
In version 3.23 onwards you could use a plugin and rely on its automatic serialization of internal variables. You can force a save of those variables if you are worried about losing the lot.

- Nick Gammon

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

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #4 on Wed 31 Jul 2002 05:34 AM (UTC)
Message
I'll just point out for myself, Nick, that I understand what you said, but it wouldn't apply in this case. Each file is essentially a collection of game equipment. A user could quite possibly have many 'sets' of equipment (as I do), and thus, I would not want all of those variables (2 for each piece of equipment) stored in memory at the same time.... Therefor, I store each set in a file, and only keep the currenly active set in memory.

Indeed, I will store the active set as you mentioned, in the state file... but only the currenly active set. :)

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,173 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 31 Jul 2002 08:08 AM (UTC)
Message
I thought I was on risky grounds with my comment, but thought if the purpose was simply to "keep state" the plugin would do it, however there would be lots of cases where reading/writing text files would be handy.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #6 on Thu 01 Aug 2002 01:20 AM (UTC)
Message
Quote:

Additionally, is there an append command?


The text file interface will support appending, read up on Scripting.FileSystemObject to see exactly how to do it.

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