How do you write to (and access) a text file in VBscript? Additionally, is there an append command?
Writing to file?
Posted by Stoned00d on Tue 30 Jul 2002 05:22 PM — 7 posts, 29,015 views.
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:
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
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
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.
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. :)
Indeed, I will store the active set as you mentioned, in the state file... but only the currenly active set. :)
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.
Quote:
Additionally, is there an append command?
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.