cfisher440
Well-known member
- Joined
- Oct 11, 2005
- Messages
- 73
- Programming Experience
- 1-3
I want to make a script that will get me a specific file's last modified date and subtract it from the current date giving me a number of days.
Example. Today is 8/22/06. The file test.log was last modified on 8/18/06.
So the program would basically state the file was last modified three
days ago.
I did this easily using vb.net, but it needs to be in a script format because I can't have the .NET framework running on this PC.
To make it a little complicated, if the file modification date is 30 days or more of a difference from the current date, then it does "something"
This is what I did in VB.net. I just need it to be in VBScript
Any questions / comments / ideas / solutions would be appreciated.
Example. Today is 8/22/06. The file test.log was last modified on 8/18/06.
So the program would basically state the file was last modified three
days ago.
I did this easily using vb.net, but it needs to be in a script format because I can't have the .NET framework running on this PC.
To make it a little complicated, if the file modification date is 30 days or more of a difference from the current date, then it does "something"
This is what I did in VB.net. I just need it to be in VBScript
VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.IO[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Windows.Forms[/SIZE]
[SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE][SIZE=2] LastModified[/SIZE]
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Main()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fileName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "C:\test\test.log"[/SIZE]
[SIZE=2][COLOR=#008000]' determine whether filename is a file[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(fileName) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' get file's modification date and time and display it through a messagebox[/COLOR][/SIZE]
[SIZE=2]MessageBox.Show(GetInformation(fileName))[/SIZE]
[SIZE=2][COLOR=#008000]' display file contents through StreamReader[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'obtain reader and file contents[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] stream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamReader[/SIZE]
[SIZE=2]stream = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamReader(fileName)[/SIZE]
[SIZE=2][COLOR=#008000]' handle exception if stream reader is unavailable[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exceptionCatch [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IOException[/SIZE]
[SIZE=2][COLOR=#008000]'display error[/COLOR][/SIZE]
[SIZE=2]MessageBox.Show("FILE ERROR", "FILE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'notify user that neither file nor directory exists[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Directory.Exists(fileName) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' notify user that neither file nor directory exists[/COLOR][/SIZE]
[SIZE=2]MessageBox.Show(fileName & " does not exist", _[/SIZE]
[SIZE=2]"FILE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetInformation([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] fileName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] information [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ts [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan[/SIZE]
[SIZE=2]information = fileName & " exists" & vbCrLf & vbCrLf[/SIZE]
[SIZE=2]information &= "Current date: " & _[/SIZE]
[SIZE=2]DateTime.Now() & vbCrLf & "Last modified: " & _[/SIZE]
[SIZE=2]File.GetLastWriteTime(fileName)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] DateTime.Now() = File.GetLastWriteTime(fileName) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]information &= "Last time file modified - Today!"[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]ts = DateTime.Now().Subtract(File.GetLastWriteTime(fileName))[/SIZE]
[SIZE=2]information &= vbCrLf & "File was last modified: " & ts.Days.ToString() & " days ago."[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] information[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE]
Any questions / comments / ideas / solutions would be appreciated.