VBS Check Date Modified

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2948 Views - Last Post: 18 November 2010 - 09:32 AM Rate Topic: -----

#1 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

VBS Check Date Modified

Posted 08 November 2010 - 07:35 AM

Hi,

I don't know if I should use this forum to ask a VB script question. But I thought I would try.

The goal is to check a txt file; first to see if it's empty or not (and return status of that in a file) then proceed to check if the file is actually modified (was it modified 5mins ago for example).

Here my current status of the script

on error resume next

const InputTextFile = "C:\ExportDataToFile_DGF\ConsoleProgramErrFile.txt"
const OutputTextFile = "C:\mon_script\filestatus.txt"

ReadLogTextFile()

sub ReadLogTextFile()
	Dim TextFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(InputTextFile, 1, false)
	if NOT objFile.AtEndOfStream then
		TextFile = objFile.Read(300)
	end if
	objFile.Close
	if len(TextFile) = 0  then
		CreateMonitorTextFile("Ok")
	else
		CreateMonitorTextFile("Error")
	end if
end sub

sub CheckFileDate ()
	Dim currentDate
	Dim dateStatus
	currentDate = Date()
	dateStatus = 0

	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.GetFile(InputTextFile)
	if objFile.DateLastModified = currenDate then
		Set objTextFile = objFSO.OpenTextFile(OutputTextFile, 2)
		set dateStatus = 1
		objTextFile.WriteLine ("ConsoleProgramErrFile=" & dateStatus)
	else
		Set objFile = objFSO.CreateTextFile(OutputTextFile)
		set dateStatus = 2
		objFile.WriteLine ("ConsoleProgramErrFile=" & dateStatus)
		objFile.Close
		
end sub

sub CreateMonitorTextFile(status)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FileExists(OutputTextFile) then
		Set objTextFile = objFSO.OpenTextFile(OutputTextFile, 2)
		objTextFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objTextFile.Close
	else
		Set objFile = objFSO.CreateTextFile(OutputTextFile)
		objFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objFile.Close
	end if
end sub



I would appreciate any input

Is This A Good Question/Topic? 0
  • +

Replies To: VBS Check Date Modified

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: VBS Check Date Modified

Posted 16 November 2010 - 04:48 PM

What's wrong with your code?
Was This Post Helpful? 0
  • +
  • -

#3 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 17 November 2010 - 08:51 AM

View PostBobRodes, on 16 November 2010 - 11:48 PM, said:

What's wrong with your code?


It doesn't work.

ReadLogTextFile()
works fine


CheckFileDate ()
doesn't work.
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: VBS Check Date Modified

Posted 17 November 2010 - 10:36 AM

View Postzukker, on 17 November 2010 - 07:51 AM, said:

View PostBobRodes, on 16 November 2010 - 11:48 PM, said:

What's wrong with your code?


It doesn't work.

ReadLogTextFile()
works fine


CheckFileDate ()
doesn't work.



Bob asked a valid question, so you could've been a little more helpful with your reply.

Have you tried debugging the routine and if so, at which point does it stop working?

Try to help yourself, but giving a bit more information please.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: VBS Check Date Modified

Posted 17 November 2010 - 12:10 PM

Sounds like you're irritated that you need to go to the trouble of explaining your problem before we can fix it for you. If I were as helpful as you appear to be willing to be, I would have responded thusly:

Quote

I don't know if I should use this forum to ask a VB script question. But I thought I would try.
Well, when you succeed in actually asking a question, let me know and I'll see if I can answer it. Until then, keep trying.

Quote

I would appreciate any input
Ok, here's some input: Nice looking code. Has 54 lines. Uses the FileSystemObject class.

Since it's in my nature to be much more helpful than that, and I am willing to assume that you might not be as irritated as you sound but may simply lack experience in troubleshooting problems, perhaps you will explain exactly what sort of error you encounter when your line of code "doesn't work."
Was This Post Helpful? 0
  • +
  • -

#6 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 17 November 2010 - 01:46 PM

View PostBobRodes, on 17 November 2010 - 11:10 AM, said:

Sounds like you're irritated that you need to go to the trouble of explaining your problem before we can fix it for you. If I were as helpful as you appear to be willing to be, I would have responded thusly:

Quote

I don't know if I should use this forum to ask a VB script question. But I thought I would try.
Well, when you succeed in actually asking a question, let me know and I'll see if I can answer it. Until then, keep trying.

Quote

I would appreciate any input
Ok, here's some input: Nice looking code. Has 54 lines. Uses the FileSystemObject class.

Since it's in my nature to be much more helpful than that, and I am willing to assume that you might not be as irritated as you sound but may simply lack experience in troubleshooting problems, perhaps you will explain exactly what sort of error you encounter when your line of code "doesn't work."



The problem is that I don't actually know where the problem exists, and my knowledge of vbs exceptions is null.
I would assume that my comparison of the datelastmodified is completely off (hence the topic), right now I try to compare it to the currentdate to get the script to run and then proceed to make changes that suits my needs which is more complicated. It should check the datelastmodified to see if it was modified this month and this check should be done every month the 25th ( but that can problably be solved by using scheduled tasks)
Was This Post Helpful? 0
  • +
  • -

#7 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: VBS Check Date Modified

Posted 17 November 2010 - 02:02 PM

If you think it's in that area, then maybe you could have a look at using the DateDiff function (assuming that's available in VBS).

This will allow to check the difference between the two dates using different intervals (hours, days, etc.).

Give that a lookup and let us know how you get on.

This post has been edited by maj3091: 17 November 2010 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: VBS Check Date Modified

Posted 17 November 2010 - 09:03 PM

All right, now we're getting somewhere. Do you get an error message, or does the Sub just not work as you intend it to?
Was This Post Helpful? 0
  • +
  • -

#9 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 18 November 2010 - 12:56 AM

I do not get any error messages when i run the script (as if everything runs as its supposed to).
But the output file isn't complete, it seems that it never runs the checkfiledate() at all.

I've been thinking about what maj3091 (thanks for the tip btw) said and redid some of my code.
The problem now is that I don't get any output to my filestatus file.

on error resume next

const InputTextFile = "C:\ExportDataToFile_DGF\ConsoleProgramErrFile.txt"
const OutputTextFile = "C:\mon_script\filestatus.txt"

ReadLogTextFile()

sub ReadLogTextFile()
	Dim TextFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(InputTextFile, 1, false)
	if NOT objFile.AtEndOfStream then
		TextFile = objFile.Read(300)
	end if
	objFile.Close
	if len(TextFile) = 0  then
		CheckFileDate()
	else
		CreateMonitorTextFile("Error")
	end if
end sub

sub CheckFileDate ()
	Dim currentDate
	Dim fileDate
	currentDate = Date()
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.GetFile(InputTextFile)
	fileDate = objFile.DateLastModified											
	
	if DateDiff(m,currentDate,fileDate) = 0 then
		
		CreateMonitorTextFile("Ok")										
	else
		CreateMonitorTextFile("Error")
	end if		
end sub

sub CreateMonitorTextFile(status)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FileExists(OutputTextFile) then
		Set objTextFile = objFSO.OpenTextFile(OutputTextFile, 2)
		objTextFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objTextFile.Close
	else
		Set objFile = objFSO.CreateTextFile(OutputTextFile)
		objFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objFile.Close
	end if
end sub


Was This Post Helpful? 0
  • +
  • -

#10 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: VBS Check Date Modified

Posted 18 November 2010 - 01:19 AM

OK, I'm not sure whether you can debug VBS the same as VB6, so I'll make the assumption that you can't.

As a test, try replacing your function calls to CreateMonitorFile with msgbox, just so you can determine which path it's taking. When you're happy with that, do the same in the CreateMonitorFile function (replace the writeline with a msgbox.....sounds trivial, but at least it will identify the exact path through your code and help narrow it down.

You might want to consider declaring local variables for your filesystem object as opposed what appear to be global, just to isolate the code in that particular routine, otherwise you run the risk of changing the variable (objFSO for example) when you call another function.

Looking at your CreateMonitorFile code, it's difficult to see anything wrong otherwise.

This post has been edited by maj3091: 18 November 2010 - 01:22 AM

Was This Post Helpful? 1
  • +
  • -

#11 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 18 November 2010 - 01:55 AM

I seem to have isolated the issue.

If i put a msgbox before line 32 (if DateDiff(m,currentDate,fileDate) = 0 then) i get the box.
But if i put one after i get nothing (i put one in 'else' as well).

It seems that my DateDiff isn't working correctly, the Date() and DateLastModified returns date formatted in different ways.

EDIT:I changed my currentDate to now() instead of date() but i still get the same problem.
I've tried to run the datediff by itself and this function seems to be the issue

EDIT2: DateDiff(m,currentDate,fileDate) needs to be DateDiff("m",currentDate,fileDate), however i still get no output.

This post has been edited by zukker: 18 November 2010 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#12 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: VBS Check Date Modified

Posted 18 November 2010 - 02:52 AM

Can you post two examples of the dates you are working with and I'll have a look.

I've used the datediff functions many times in VB6 with no issues at all.
Was This Post Helpful? 0
  • +
  • -

#13 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 18 November 2010 - 02:59 AM

View Postmaj3091, on 18 November 2010 - 09:52 AM, said:

Can you post two examples of the dates you are working with and I'll have a look.

I've used the datediff functions many times in VB6 with no issues at all.


I've seemed to solved the problems I've been having :) With a lot of help with msgbox.

Thank you for your assistance.
Was This Post Helpful? 0
  • +
  • -

#14 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: VBS Check Date Modified

Posted 18 November 2010 - 03:07 AM

No probs, happy to have helped. Feel free to acknowledge the assistance by clicking the + button! :bigsmile: (EDIT: You already did whilst I was typing this! ;))

Don't forget to post your updated code for others to see for future reference.

This post has been edited by maj3091: 18 November 2010 - 03:08 AM

Was This Post Helpful? 0
  • +
  • -

#15 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 22
  • Joined: 10-July 08

Re: VBS Check Date Modified

Posted 18 November 2010 - 03:14 AM

Here's the code that seems to work
on error resume next

const InputTextFile = "C:\ExportDataToFile_DGF\ConsoleProgramErrFile.txt"
const OutputTextFile = "C:\mon_script\filestatus.txt"

ReadLogTextFile()

sub ReadLogTextFile()
	Dim TextFile
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(InputTextFile, 1, false)
	if NOT objFile.AtEndOfStream then
		TextFile = objFile.Read(300)
	end if
	objFile.Close
	if len(TextFile) > 0  then
		CheckFileDate()
		
	else
		CreateMonitorTextFile("Error")
	end if
end sub

sub CheckFileDate ()
	
	Dim currentDate
	Dim fileDate
	currentDate = Now()
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.GetFile(InputTextFile)
	fileDate = objFile.DateLastModified
	if DateDiff("yyyy",currentDate,fileDate) = 0 & DateDiff("m",currentdate,fileDate) = 0 then
		CreateMonitorTextFile("Ok")
														
	else
		CreateMonitorTextFile("Error")
		
		
	end if		
end sub

sub CreateMonitorTextFile(status)
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	if objFSO.FileExists(OutputTextFile) then
		Set objTextFile = objFSO.OpenTextFile(OutputTextFile, 2)
		objTextFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objTextFile.Close
	else
		Set objFile = objFSO.CreateTextFile(OutputTextFile)
		objFile.WriteLine ("ConsoleProgramErrFile=" & status)
		objFile.Close
	end if
end sub


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2