We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

wildarmsdave
wildarmsdave asked
on
Medium Priority
232 Views
Last Modified: 2010-04-07
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.
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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?
Leo EikelmanDirector, IT and Business Development
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You can do it natively in VB6 with FileDateTime():

    Dim modified As Date
    modified = FileDateTime("c:\somefile.txt")
    MsgBox modified

Author

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.
Leo EikelmanDirector, IT and Business Development

Commented:
Glad the code helps : )

Leo
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Author

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??
Leo EikelmanDirector, IT and Business Development

Commented:
split points
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Split 'em...points are no big deal...    =)

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.