Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Load file but only if it has been modified....

What I am trying to do is this....

On program start, Load a .txt file from network drive.
Check if the .txt file has changed (maybe by checking the last modified date/time of the files properties..)????
If the .txt file has changed, load the file again.
If the .txt file hasn't changed do nothing.

Any pointers anyone, adnd is this the best way to do this?

Many thanks,

Dave.
0
wildarmsdave
Asked:
wildarmsdave
  • 4
  • 3
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not sure I understand your description...

You load a .txt file at program start...got it.  

Are we supposed to store the modified date/time now?  (after displaying the text file on a form)

Now when should we check to see if it has changed?  Do you want to use a Timer and check to see if the file has changed every 30 seconds or so?  If it has, then load the file again and update the file contents on your form?
0
 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
You can use this to check when the find was last modified

Private Sub Form_Load()
Dim FSO As Scripting.FileSystemObject 'Set reference to Microsoft Scripting Runtime
Set FSO = New Scripting.FileSystemObject
Dim File As Scripting.File

If FSO.FileExists("C:\EOW-3.txt") Then
Set File = FSO.GetFile("C:\EOW-3.txt")


MsgBox (File.DateLastModified)

Then you can use something like this

if DateDiff("h", Date, File.DateLastModified) > 24 then

// Do whatever you need to do

Where Date is the current date.

so with this you can check if the file was last modified in a certain amount of time.


Hope this helps ,

Leo
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can do it natively in VB6 with FileDateTime():

    Dim modified As Date
    modified = FileDateTime("c:\somefile.txt")
    MsgBox modified
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wildarmsdaveAuthor Commented:
I think leikelman has hit the nail on the head although I don't have time to try his code yet. I'll have a look later...

Idle Mind, you are correct with what I am wanting to do..

Currently the program opens the file every 60 seconds and if the data has changed, displays it on screen. This is fine as all the data contains is a few lines of text. However, the data will now contain images and tables which will increase the file size. I would sooner check if the file has changed BEFORE I load it from the network. The program will be running on 60+ PC's so don't want to overload our network..

Dave.
0
 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Glad the code helps : )

Leo
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So something like this...

Option Explicit

Private fileName As String
Private lastModified As Date

Private Sub Form_Load()
    fileName = "c:\someFile.txt"
    lastModified = FileDateTime(fileName)
    LoadFile fileName, Text1
End Sub

Private Sub LoadFile(ByVal file As String, ByVal tb As TextBox)
    If Dir(file) <> "" Then
        Open file For Binary Access Read As #1
        tb.Text = Input(LOF(1), 1)
        Close #1
    End If
End Sub

Private Sub Timer1_Timer()
    Dim curModified As Date
    curModified = FileDateTime(fileName)
    If curModified <> lastModified Then
        lastModified = curModified
        LoadFile fileName, Text1
    End If
End Sub
0
 
wildarmsdaveAuthor Commented:
Sorry for the late reply!!

Both solutions are fine but I have a bit of a dilema regarding to award the points to (and I don't want to upset anyone!!)

leikelman's code got me up and running but then Idle_Mind  posted his code which was better than mine (not difficult!) so I decided to use that instead. What do you guys think??
0
 
Leo EikelmanDirector, IT and Business DevelopmentCommented:
split points
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Split 'em...points are no big deal...    =)
0
 
wildarmsdaveAuthor Commented:
Ok lads. I just wanted to make sure you were both happy with that before I split them. Thank you both once again for your quick responses and class answers!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now