Refresh Txt File (1 Viewer)

ajetrumpet

Banned
Local time
Today, 05:49
Joined
Jun 22, 2007
Messages
5,638
all,

this is a continuation of this thread of mine: http://www.access-programmers.co.uk/forums/showthread.php?t=182364

i am looking for a way to refresh a txt file after a DOS redirection of text into my text file. here is my function that I run:
Code:
Function t()

            Call GetCurrentDirectory("-r")

Dim fNum As Long

fNum = FreeFile()

Open "c:\test.txt" For Input As #fNum
Close #fNum

            Call PopulateTable

End Function
in the above code, I am trying to refresh it by opening the streamed file, then closing it right away. the GetCurrentDirectory function populates the text file with the information from DOS, but the PopulateTable function gets the text file info I had before I ran this function. If there was no file existing, it gives me a empty string. Here is the function that doesn't work:
Code:
Function PopulateTable()

On Error Resume Next

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM FileContents"

Dim fNum As Long
Dim ReadFile() As String
Dim i As Integer
Dim FileString As String
Dim DoubleSpaceLoc As Double
Dim LengthOfFileSize As Double
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("FileContents", dbOpenDynaset)

fNum = FreeFile()

[COLOR="Red"][B]'THIS OPENS THE FILE, BUT GRABS INFORMATION THAT IS NOT CURRENTLY IN THERE![/B][/COLOR]
Open "c:\test.txt" For Input As #fNum
ReadFile = Split(Input$(LOF(fNum), fNum), vbCrLf)
Close #fNum

  For i = LBound(ReadFile) To UBound(ReadFile)
Debug.Print ReadFile(i)
    rs.AddNew
    rs!filenamefield = Right(ReadFile(i), Len(ReadFile(i)) - InStrRev(ReadFile(i), " "))
    
      'GET THE EXTRACTION VARIABLES IN THIS SECTION
      FileString = ReadFile(i)
      DoubleSpaceLoc = InStrRev(ReadFile(i), "  ")
      LengthOfFileSize = (InStr((InStrRev(ReadFile(i), "  ") + 2), ReadFile(i), " ")) - _
                         (InStrRev(ReadFile(i), "  ") + 2)
      
      [COLOR="Red"][B]'THIS SECTION POPULATES THE TABLE WITH THE WRONG INFORMATION[/B][/COLOR]
      If InStr(ReadFile(i), "<DIR>") = 0 Then
         rs!filesizefield = Mid(FileString, (DoubleSpaceLoc + 2), LengthOfFileSize)
      Else
         rs!filesizefield = "n/a"
      End If
         If InStr(ReadFile(i), "<DIR>") > 0 Then
            rs!filenamefield = Mid(FileString, (DoubleSpaceLoc + 2), (Len(FileString)) - (DoubleSpaceLoc + 1))
         Else
            rs!filenamefield = Mid(FileString, _
                               (DoubleSpaceLoc + 2) + (LengthOfFileSize + 1), _
                               (Len(FileString)) - ((DoubleSpaceLoc + 1) + (LengthOfFileSize - 1)))
         End If
    
    rs!full = ReadFile(i)
    rs.Update
    
  Next i

DoCmd.SetWarnings True

End Function
One thing that may be helpful for people looking at this is that when I simply CLICK on the file name in windows explorer, the size of the file changes, which is the change that I'm trying to register in VBA. I can't figure this out! is there any help out there?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:49
Joined
Jan 20, 2009
Messages
12,854

MarkK

bit cruncher
Local time
Today, 03:49
Joined
Mar 17, 2004
Messages
8,186
Why doesn't GetCurrentDirectory just write it's results to table--and to file if necessary--and you solve this read problem by simply not performing it.
 

petehilljnr

Registered User.
Local time
Today, 03:49
Joined
Feb 13, 2007
Messages
192
So - if I'm reading correctly, it sounds like the text file hasn't finishing writing? If that is the case, then what about something to test to see if the length of the file keeps increasing and pausing until it stops increasing (i.e. it has finished writing)?

Something along the lines of (I haven't tested):

Code:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
 
Sub WaitForFile(strFile As String)
 
Dim prev_len As Long
 
prev_len = 0
 
Sleep (500) ' wait 1/2 second to see if file updating
 
Do While FileLen(strFile) <> prev_len
 
    prev_len = FileLen(strFile)
    Sleep (500) ' wait 1/2 second to see if file updating
    
Loop
 
End Sub

Just an idea.

Regards,
Pete
 

ajetrumpet

Banned
Local time
Today, 05:49
Joined
Jun 22, 2007
Messages
5,638
thank you all. this is still a problem, and i'm gonig to try out the suggestions posted here. will post back when i have results. thanks!
 

Users who are viewing this thread

Top Bottom