darjab
darjab

Reputation: 129

Downloading content from txt file to a variable

I made a counter that works when you press the button as the action in VBScript.

my code:

  Licznik_ID = Licznik_ID + 1

    Dim Stuff, myFSO, WriteStuff, dateStamp

    Stuff = "Whatever you want written"

    Set myFSO = CreateObject("Scripting.FileSystemObject")
    Set WriteStuff = myFSO.OpenTextFile("c:\tmp\yourtextfile.txt", 2, True)
    WriteStuff.WriteLine(Licznik_ID)
    WriteStuff.Close
    SET WriteStuff = NOTHING
    SET myFSO = NOTHING

the counter variable named "Licznik_ID" indicated by the arrow.

enter image description here

It is written to the file "c:\tmp\yourtextfile.txt" And it works well. For each time the number increases by one and is replaced and stored in the txt file.

The file contains the number 1 and the increase in the txt file appears the number 2 and so on ...

How now download the data stored with the file "c: \tmp\yourtextfile.txt" back to the variable to use it in such a way that when you start NiceForm or button has been loaded with the contents of txt file into a variable?

Upvotes: 1

Views: 91

Answers (1)

langstrom
langstrom

Reputation: 1702

Set myFSO = CreateObject("Scripting.FileSystemObject")
Licznik_ID = myFSO.OpenTextFile("C:\tmp\yourtextfile.txt").ReadAll
Licznik_ID = Licznik_ID + 1
myFSO.OpenTextFile("C:\tmp\yourtextfile.txt",2,True).Write(Licznik_ID)

FSO is kinda weird the way it does its file modes sometimes.

edit: If you want to allow this to create the file without errors if it doesn't exist, replace line 2 with this:

If myFSO.FileExists("C:\tmp\yourtextfile.txt") Then 
    Licznik_ID = myFSO.OpenTextFile("C:\tmp\yourtextfile.txt").ReadAll
End If 

If the file doesn't exist, Licznik_ID will be Empty. Empty + 1 = 1 in vbscript.

Upvotes: 1

Related Questions