# WEBfactory 2010

#### Writing a simple text file with WEBfactory VChannel

Writing a text file with VChannels can be useful when a user needs to store information about the ongoing process.

In order to manipulate text files with VChannel, we need to use the Windows FileSystemObject. In the following tutorial, we will demonstrate how to use VChannels in conjunction with FileSystemObject to create, write and read a text file. For this, we will need to have a trigger signal in WEBfactoryStudio and to create another VChannel enabled signal.

The Setpoint 2 trigger signal and the WriteToFile VChannel enabled signal in WEBfactoryStudio

As it can be seen in the image above, we have defined a signal (named WriteToFile) and enabled VChannel for it. In the VChannel options, we have selected the Setpoint 2 signal as our trigger.

Now it's time to write the VB script that will allow us to create and write to a file:

• Declare the filesys and filetxt methods. Declare the constants needed for appending in case the file already exists.

dim filesys, filetxt 'This methods are set below
Const ForAppending = 8 'This constant is necessary for appending the text in case the file already exists. 8 means ForAppending.
Set filesys = CreateObject("Scripting.FileSystemObject") 'The CreateObject method allows us to create the text file
Set filetxt = filesys.CreateTextFile("c:\TextFile\SignalValue.txt", ForAppending, True) 'The CreateTextFile method uses CreateObject
to create the txt file
• Let's give the script something to write. Declare a date variable and a value variable:

mydate = Now() 'Gets the current date and time
actualvalue = X(1) 'Gets the value of the 1 exponent  - the Setpoint 2 trigger signal has the exponent 1.
Alternatively, the signal name can be used.
• Now we are going to use the filetxt method set above to write a combination of strings and variables to the signal:

filetxt.WriteLine(mydate & " Signal value is" & actualvalue) 'Writing the mydate variable, the string "Signal value is"
and the actualvalue variable to the text file.
filetxt.Close


The complete code should look like this:

Sub GetResult()

mydate = Now()
actualvalue = X(1)
dim filesys, filetxt, path
Const ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\TextFile\SignalValue.txt", ForAppending, True)
filetxt.WriteLine(mydate & " Signal value is " & actualvalue)
filetxt.Close

End Sub

The complete script

To test the script, enter 1 in the Signal Value field. Make sure that the Test Value of the trigger signal is 1. Any other value can be used. Click Execute to run the script with the trigger signal value of 1.

Enter the signal value and execute the script

In the TextFile folder on the C: drive, the SignalValue.txt file should have been created. The text file should contain the date, time, string and variable we defined in the script.

The result of the script

If the text file already exists, the script will append the line to it.

Appending text to the file