Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Write text to a simple file (VB Scripting)

    Hello all,
    I have a need to write a time date stamp and a serial number to a simple text file after a certain part of code runs. Basically, I need to continually overwrite what is in this file. Has anyone ever done something like this before?

    Thanks,
    Mark

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    Something like this?

    <pre>Option Explicit


    Public Sub LogDTS()
    Dim iFileNO As Integer
    iFileNO = FreeFile
    Open "C:WorkMyLog.txt" For Output As #iFileNO
    Print #iFileNO, Format(Date, "dd mmmm yyyy"); Spc(5); Format(Time(), "hh:mm:ss"); Spc(5); "123456"
    Close #iFileNO
    End Sub
    </pre>

    Legare Coleman

  3. #3
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    I will give it a try.

    Thanks!

  4. #4
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    OK that works!

    Now for the next part. I want to open that file first and compare the date with the current date. If the date is different, then reset the serial number to 0, otherwise write the current serial number and TD stamp.

    Thanks,
    Mark

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Write text to a simple file (VB Scripting)

    What is your host and language? Are you using VBA in an Office application, VBScript with the Windows Script Host, VBScript with Internet Explorer or ASP, VB, etc. Different things are available in each situation.

    In VBA, I would suggest using the methods that work with INI files, You create a section that looks like this:

    [My Settings]
    SerialNo=123445
    Date=10/17/2004

    However, I don't think that is available in VBScript. Instead, you could use the FileSystemObject's various methods (too complicated to describe off the top of my head).

    Another option is to use XML. Code to read and write XML is ugly, but it is the INI file of the future. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> I posted an example somewhere that writes and then reads back Outlook toolbar position information.

  6. #6
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    Hi,
    It is working in another windows application that uses VBscript for some operations. The system in question needs to do the following. It needs to open this text file and check the date within the file. After verifying it, it should write the next serial number or reset the serial number to zero again. I have been provided code earlier in this post about how to write to the file and that appears to work fine. Now I need to tell it to inspect the file first. Got any ideas?

    Thanks,
    Mark

  7. #7
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    This works. How do I make a 5 digit number display the leading zeros? In other words, I have this 5 digit SN like 00011. I want to write the leading zeros into the text file mentioned earlier.

    THanks,
    Mark

  8. #8
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    Does this do what you want?

    <pre>Public Sub LogDTS()
    Dim iFileNO As Integer, strLogDate As String, lSerial As Long
    Dim strLine As String
    iFileNO = FreeFile
    Open "C:WorkMyLog.txt" For Input As #iFileNO
    Line Input #iFileNO, strLine
    lSerial = Trim(Right(strLine, 6))
    strLogDate = Trim(Left(strLine, Len(strLine) - 19))
    Close #iFileNO
    If strLogDate <> Format(Date, "dd mmmm yyyy") Then
    lSerial = 0
    End If
    Open "C:WorkMyLog.txt" For Output As #iFileNO
    lSerial = lSerial + 1
    Print #iFileNO, Format(Date, "dd mmmm yyyy"); Spc(5); Format(Time(), "hh:mm:ss"); _
    Spc(5); Format(lSerial, "00000")
    Close #iFileNO
    End Sub
    </pre>

    Legare Coleman

  9. #9
    New Lounger
    Join Date
    Sep 2004
    Location
    York, Virginia, USA
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Write text to a simple file (VB Scripting)

    <P ID="edit" class=small>(Edited by paulbonney on 28-Oct-04 07:26. Just a quick update to use code standard naming practice and also to use the free file option)</P>Mark

    Here is a modified version of what I did for a old project

    Sub Key()
    Dim dDate As Date
    Dim sIndex As String
    Dim iFileNO As Integer
    iFileNO = FreeFile

    On Error GoTo NoIni
    ' Read Data from ini file
    Open "C:TempKey.ini" For Input As #iFileNO
    On Error GoTo BadIni
    ' Read data into all variables
    Input #1, dDate, sIndex
    ' Close before reopening in another mode.
    Close #1
    GoTo Ini

    ' Initialize a new INI file
    NoIni:
    dDate = Date
    sIndex = 0
    Close #1
    GoTo Ini

    ' Found but had errors
    BadIni:
    dDate = Date
    sIndex = 1
    Close #1
    GoTo Ini

    Ini:
    On Error Resume Next
    ' Check to see if new Date
    If dDate <> Date Then
    dDate = Date
    sIndex = 0
    End If

    'Increments counter
    sIndex = sIndex + 1
    ' formats counter to 5 digits
    sIndex = Right("00000" + sIndex, 5)
    dDate = Date
    ' Save Data to ini file
    Open "C:TempKey.ini" For Output As #iFileNO
    Write #1, dDate, sIndex
    Close #1 ' Close file.
    End Sub

    Good luck and I hope

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •