Features, discussions, tips, tricks, questions, problems and feedback

Write data to text file

I’m using ScriptAgent to get values into a text file.

I can get an Adroit.GetTag.
I can get a date and time.

But I want to convert the date and time to a different output.
Now the date is in the format dd/mm/yyyy hh:mm:ss.

I want to have a date stamp that goes like yyyy/mm/dd hh:mm:ss.

Does someone know how I can make this happen?
This is my code:

This is the result:

Hopefully, someone understands me and can help me.

'Based on the abovementioned script where "datum" = now
' Split the input string to extract date and time components
dateTimeParts = Split(datum, " ")
dateParts = Split(dateTimeParts(0), "/")
timePart = dateTimeParts(1)

' Assemble the date in yyyy/mm/dd format
formattedDate = dateParts(2) & "/" & dateParts(1) & "/" & dateParts(0)

' Assemble the formatted date and time
formattedDateTime = formattedDate & " " & timePart

The abovementioned formattedDateTime will contain the date as yyyy/mm/dd in a string format

Frits,

Thank you for te help, this works perfect.

Frits,

Do you know how to write a line at the end instead of the top of a txt file?

To append to a text file (to add lines at the end of the file) use OpenTextFile as follows:

objFSO.OpenTextFile("c:\temp\lijn9.txt",8)

When I change the readinge 1 to appending i got it stops working.

If I remove : strContents = objFile.ReadAll , then I get a error.

Can you tell me what I’m doing wrong.

Sub Main()

'--------------------------------------------------------
'voor de juiste datum zorgen.
'datum = Now zorgt voor de datum van het systeem alleen dan wel in een ferkeerd format
'deze moet hier om om gezet worden naar het gewenste format.
'Om te zorgen dat de datum en tijd nu kloppen

if Adroit.GetTag(“TEST_BIT_ON_OF.value”)= True Then

datum = Now
’ Split the input string to extract date and time components
dateTimeParts = Split(datum, " ")
dateParts = Split(dateTimeParts(0), “/”)
timePart = dateTimeParts(1)

’ Assemble the date in yyyy/mm/dd format
formattedDate = dateParts(2) & “/” & dateParts(1) & “/” & dateParts(0)

’ Assemble the formatted date and time
formattedDateTime = formattedDate & " " & timePart

'----------------------------------------------
'lijst met variabelen
myvalue = Adroit.GetTag(“CAPPER_L3_PINDA_POT_PM.value”)

'---------------------------------------------------------------------
'Hier worden de gegevens naar het txt filse gestuurd.
Const ForReading = 8
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“C:\temp\lijn9.txt”,ForReading)
strContents = objFile.ReadAll
objFile.Close
strFirstLine = formattedDateTime & ", " & myvalue
strNewContents = strFirstLine & vbCrLf & strContents
Set objFile = objFSO.OpenTextFile(“C:\temp\lijn9.txt”, ForWriting)
objFile.WriteLine strNewContents

objFile.Close

End if

End Sub

No, appending should be used in place of writing, not reading. In other words, where you OpenTextFile for writing(2), it should be replaced with appending (8) instead. Reading should remain as 1.

If you put it like that. Than it makes perfect sense.

Thank you again for the help.

A post was split to a new topic: Combining statements with “And” in VBScript

A post was split to a new topic: Script Agent Scheduling