hexagon logo

Writing/Reading a .txt file

This is very much a VB question not related to PC-DMIS. There is so much information out there for VB that I find it very hard to locate a good example of what I want to do.

I have a script at the beginning and end of my part program. I would like to get the total runtime of the program. I have a way to get systemtime in each and put it into seconds and such, thanks to this message board.

I do not want to write the beginning time to a variable and have it go through the part program. What I wanted was a temporary .txt file or something similar that I could write the begintime value (in seconds) to. The second script would grab this value and use it. The .txt file either needs to be cleared or overwritten by the new begintime number every execution. How do I code this?

My goal is to take these values and use them to calculate CMM run time for the day in Excel. Can I export the Total Runtime number I get in the second script to Excel? It will probably be easier to google information for this; I will update this post if I find what I am looking for.

Thanks!
Parents
  • A quickie I wrote in VB for capturing the time. You will need to write this into two seperate files.

    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub Main()[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteNow As DateTime = DateAndTime.Now[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteSecond As Integer = dteNow.Second[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strTime As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strTime = Str(dteHour) + ":" + Str(dteMinute) + ":" + Str(dteSecond)[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.WriteAllText("C:\temp\xtime.txt", strTime, False)[/COLOR][/SIZE]
     
    [SIZE=2][COLOR=#0000ff]       MsgBox("Wait for it...", MsgBoxStyle.OkOnly)   [COLOR=red]<----Seperate here[/COLOR][/COLOR][/SIZE]
    [COLOR=#ff0000][/COLOR][COLOR=#0000ff] 
    [SIZE=2]       dteNow = DateAndTime.Now[/SIZE]
    [/COLOR][/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim strStartTime As String[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartSecond As Integer[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strStartTime = My.Computer.FileSystem.ReadAllText("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff] 
    [SIZE=2][COLOR=#0000ff]       dteStartHour = Int(Left(strStartTime, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartMinute = Int(Mid(strStartTime, 5, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartSecond = Int(Mid(strStartTime, 9, 3))[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim dteEndHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndSecond As Integer = dteNow.Second[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim intElapsedHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedSecond As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strElapsed As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]       If dteStartSecond > dteEndSecond Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndSecond = dteEndSecond + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       If dteStartMinute > dteEndMinute Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndHour = dteEndHour - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       intElapsedHour = dteEndHour - dteStartHour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedMinute = dteEndMinute - dteStartMinute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedSecond = dteEndSecond - dteStartSecond[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strElapsed = Str(intElapsedHour) + ":" + Str(intElapsedMinute) + ":" + Str(intElapsedSecond)[/COLOR][/SIZE][/COLOR][/SIZE]
    [COLOR=#0000ff][/COLOR] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       MsgBox("Elapsed time: " + strElapsed, MsgBoxStyle.OkOnly)[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.DeleteFile("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]   End Sub[/COLOR][/SIZE][/COLOR][/SIZE]


Reply
  • A quickie I wrote in VB for capturing the time. You will need to write this into two seperate files.

    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub Main()[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteNow As DateTime = DateAndTime.Now[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteSecond As Integer = dteNow.Second[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strTime As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strTime = Str(dteHour) + ":" + Str(dteMinute) + ":" + Str(dteSecond)[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.WriteAllText("C:\temp\xtime.txt", strTime, False)[/COLOR][/SIZE]
     
    [SIZE=2][COLOR=#0000ff]       MsgBox("Wait for it...", MsgBoxStyle.OkOnly)   [COLOR=red]<----Seperate here[/COLOR][/COLOR][/SIZE]
    [COLOR=#ff0000][/COLOR][COLOR=#0000ff] 
    [SIZE=2]       dteNow = DateAndTime.Now[/SIZE]
    [/COLOR][/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim strStartTime As String[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteStartSecond As Integer[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strStartTime = My.Computer.FileSystem.ReadAllText("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff] 
    [SIZE=2][COLOR=#0000ff]       dteStartHour = Int(Left(strStartTime, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartMinute = Int(Mid(strStartTime, 5, 3))[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       dteStartSecond = Int(Mid(strStartTime, 9, 3))[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim dteEndHour As Integer = dteNow.Hour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndMinute As Integer = dteNow.Minute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim dteEndSecond As Integer = dteNow.Second[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       Dim intElapsedHour As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedMinute As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim intElapsedSecond As Integer[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       Dim strElapsed As String[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]       If dteStartSecond > dteEndSecond Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndSecond = dteEndSecond + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       If dteStartMinute > dteEndMinute Then[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndHour = dteEndHour - 1[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]           dteEndMinute = dteEndMinute + 60[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       End If[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       intElapsedHour = dteEndHour - dteStartHour[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedMinute = dteEndMinute - dteStartMinute[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]       intElapsedSecond = dteEndSecond - dteStartSecond[/COLOR][/SIZE]
    [/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       strElapsed = Str(intElapsedHour) + ":" + Str(intElapsedMinute) + ":" + Str(intElapsedSecond)[/COLOR][/SIZE][/COLOR][/SIZE]
    [COLOR=#0000ff][/COLOR] 
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       MsgBox("Elapsed time: " + strElapsed, MsgBoxStyle.OkOnly)[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]       My.Computer.FileSystem.DeleteFile("C:\temp\xtime.txt")[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]
    [SIZE=2][COLOR=#0000ff]   End Sub[/COLOR][/SIZE][/COLOR][/SIZE]


Children
No Data