how to write date modified into text file ?
Author |
Message |
Pete #1 / 14
|
 how to write date modified into text file ?
I have a file on a share \\server\share\file.exe I would like to create a script which will look at the modification date of this file, and then output the modification date into a text file on the user's hard drive, c:\log.txt The script should constantly compare the modification date of \\server\share\file.exe with the date inserted into the log.txt file, and if the server's file modification date becomes more recent, then the script should update the log file on the user's hard drive with the new modification date. Does my request make sense and if so, can someone show me how?
|
Tue, 05 Oct 2004 00:05:30 GMT |
|
 |
Harvey Colwel #2 / 14
|
 how to write date modified into text file ?
Since you're not doing anything if the date is different, why not just write the date to the file each time the script is ran. If you do want to do something, such as pop up a message, let me know and I'll show you how to do the compare. ---Cut Here-------------------------------------------------------- Option Explicit Dim oFS Set oFS = WScript.CreateObject("Scripting.FileSystemObject") Const sFile = "\\server\share\file.exe" Const sLog = "C:\Log.Txt" LogDate sFile, sLog oWS.Run "notepad.exe " & sLog Set oFS = Nothing WScript.Quit '--------------------- Sub LogDate(sFile, sLog) Dim oLog Set oLog = oFS.OpenTextFile(sLog,2,True) oLog.WriteLine GetModDate(sFile) oLog.Close Set oLog = Nothing End Sub '--------------------- Function GetModDate(sFile) GetModDate = oFS.GetFile(sFile).DateLastModified End Function ---Cut Here--------------------------------------------------------
Quote: > I have a file on a share \\server\share\file.exe > I would like to create a script which will look at the modification date of > this file, and then output the modification date into a text file on the > user's hard drive, c:\log.txt
|
Tue, 05 Oct 2004 06:03:13 GMT |
|
 |
MVP #3 / 14
|
 how to write date modified into text file ?
Quote: > I have a file on a share \\server\share\file.exe > I would like to create a script which will look at the modification > date of this file, and then output the modification date into a text > file on the user's hard drive, c:\log.txt > The script should constantly compare the modification date of > \\server\share\file.exe with the date inserted into the log.txt file, > and if the server's file modification date becomes more recent, then > the script should update the log file on the user's hard drive with > the new modification date. > Does my request make sense and if so, can someone show me how?
This example script runs forever and checks the file's last modified date every 15 seconds and appends any changes to a log file... set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() do dtFile = fso.getfile(sFile).datelastmodified if datedif("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,8,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Tue, 05 Oct 2004 08:49:23 GMT |
|
 |
Pete #4 / 14
|
 how to write date modified into text file ?
Thanks Michael, The thing is, I don't want the script to append the date to end of the log file such that the log file keeps a history of all these dates... I just want the log file to have one line in it, that being the date-modified of the file we are checking on the server. If the server's file changes, then I want the script to update the log file with the new date-modified of this file.
Quote: > I have a file on a share \\server\share\file.exe > I would like to create a script which will look at the modification > date of this file, and then output the modification date into a text > file on the user's hard drive, c:\log.txt > The script should constantly compare the modification date of > \\server\share\file.exe with the date inserted into the log.txt file, > and if the server's file modification date becomes more recent, then > the script should update the log file on the user's hard drive with > the new modification date. > Does my request make sense and if so, can someone show me how?
This example script runs forever and checks the file's last modified date every 15 seconds and appends any changes to a log file... set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() do dtFile = fso.getfile(sFile).datelastmodified if datedif("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,8,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Tue, 05 Oct 2004 21:43:39 GMT |
|
 |
Alex K. Angelopoulo #5 / 14
|
 how to write date modified into text file ?
Then change fso.opentextfile(sLog,8,true).writeline dtFile to fso.opentextfile(sLog,2,true).writeline dtFile The parameters are in the docs. Quote:
> Thanks Michael, > The thing is, I don't want the script to append the date to end of the log > file such that the log file keeps a history of all these dates... I just > want the log file to have one line in it, that being the date-modified of > the file we are checking on the server. If the server's file changes, then > I want the script to update the log file with the new date-modified of this > file.
> > I have a file on a share \\server\share\file.exe > > I would like to create a script which will look at the modification > > date of this file, and then output the modification date into a text > > file on the user's hard drive, c:\log.txt > > The script should constantly compare the modification date of > > \\server\share\file.exe with the date inserted into the log.txt file, > > and if the server's file modification date becomes more recent, then > > the script should update the log file on the user's hard drive with > > the new modification date. > > Does my request make sense and if so, can someone show me how? > This example script runs forever and checks the file's last modified date > every 15 seconds and appends any changes to a log file... > set fso = createobject("scripting.filesystemobject") > sLog = "c:\mylog.txt" > sFile = "\\server\share\file.exe" > dtPrev = now() > do > dtFile = fso.getfile(sFile).datelastmodified > if datedif("s",dtPrev,dtFile) <> 0 then > dtPrev = dtFile > fso.opentextfile(sLog,8,true).writeline dtFile > end if > wscript.sleep 15 * 1000 > loop while true '***this is an infinite loop*** > -- > Michael Harris > Microsoft.MVP.Scripting > Seattle WA US > --
|
Tue, 05 Oct 2004 22:13:10 GMT |
|
 |
Pete #6 / 14
|
 how to write date modified into text file ?
doesn't the script that michael posted check to see if the date-modified of \\server\share\file.exe has changed in comparison to the last time the script ran? As opposed to what I need, which is this: The file "c:\mylog.txt" has one entry in it: 04/18/2002 8:01:01 AM The script will check this entry and compare it to the date modified of \\server\share\file.exe If the date-modified of \\server\share\file.exe is more recent than the entry in c:\mylog.txt, then overwrite the entry in c:\mylog.txt with the date-modified of \\server\share\file.exe --------------
Quote: > Then change > fso.opentextfile(sLog,8,true).writeline dtFile > to > fso.opentextfile(sLog,2,true).writeline dtFile > The parameters are in the docs.
Quote: > > Thanks Michael, > > The thing is, I don't want the script to append the date to end of the log > > file such that the log file keeps a history of all these dates... I just > > want the log file to have one line in it, that being the date-modified of > > the file we are checking on the server. If the server's file changes, then > > I want the script to update the log file with the new date-modified of this > > file.
> > > I have a file on a share \\server\share\file.exe > > > I would like to create a script which will look at the modification > > > date of this file, and then output the modification date into a text > > > file on the user's hard drive, c:\log.txt > > > The script should constantly compare the modification date of > > > \\server\share\file.exe with the date inserted into the log.txt file, > > > and if the server's file modification date becomes more recent, then > > > the script should update the log file on the user's hard drive with > > > the new modification date. > > > Does my request make sense and if so, can someone show me how? > > This example script runs forever and checks the file's last modified date > > every 15 seconds and appends any changes to a log file... > > set fso = createobject("scripting.filesystemobject") > > sLog = "c:\mylog.txt" > > sFile = "\\server\share\file.exe" > > dtPrev = now() > > do > > dtFile = fso.getfile(sFile).datelastmodified > > if datedif("s",dtPrev,dtFile) <> 0 then > > dtPrev = dtFile > > fso.opentextfile(sLog,8,true).writeline dtFile > > end if > > wscript.sleep 15 * 1000 > > loop while true '***this is an infinite loop*** > > -- > > Michael Harris > > Microsoft.MVP.Scripting > > Seattle WA US > > --
|
Tue, 05 Oct 2004 22:38:26 GMT |
|
 |
MVP #7 / 14
|
 how to write date modified into text file ?
set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() if fso.fileexists(sLog) then if fso.getfile(sLog).size > 0 then sDate = fso.opentextfile(sLog,1).readline if isdate(sDate) then dtPrev = cdate(sDate) end if end if end if do dtFile = fso.getfile(sFile).datelastmodified if datediff("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,2,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 01:52:40 GMT |
|
 |
#8 / 14
|
 how to write date modified into text file ?
|
Fri, 19 Jun 1992 00:00:00 GMT |
|
 |
Pete #9 / 14
|
 how to write date modified into text file ?
That is perfect. One question: is it possible to create the "c:\mylog.txt" file as a hidden file?
set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() if fso.fileexists(sLog) then if fso.getfile(sLog).size > 0 then sDate = fso.opentextfile(sLog,1).readline if isdate(sDate) then dtPrev = cdate(sDate) end if end if end if do dtFile = fso.getfile(sFile).datelastmodified if datediff("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,2,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 03:08:50 GMT |
|
 |
MVP #10 / 14
|
 how to write date modified into text file ?
Quote: > One question: is it possible to create the "c:\mylog.txt" file as a hidden > file?
This is your last 'freebie' ;-)... WSH 5.6 documentation download http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/00... set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() if fso.fileexists(sLog) then attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs if fso.getfile(sLog).size > 0 then sDate = fso.opentextfile(sLog,1).readline if isdate(sDate) then dtPrev = cdate(sDate) end if end if else fso.createtextfile sLog attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs end if do dtFile = fso.getfile(sFile).datelastmodified if datediff("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,2,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 03:30:41 GMT |
|
 |
Pete #11 / 14
|
 how to write date modified into text file ?
The script creates the "c:\mylog.txt" file but errors with a "permission denied" when it tries to write to the file, so the file ends up empty..
Quote: > One question: is it possible to create the "c:\mylog.txt" file as a hidden > file?
This is your last 'freebie' ;-)... WSH 5.6 documentation download http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/00... sdncompositedoc.xml set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() if fso.fileexists(sLog) then attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs if fso.getfile(sLog).size > 0 then sDate = fso.opentextfile(sLog,1).readline if isdate(sDate) then dtPrev = cdate(sDate) end if end if else fso.createtextfile sLog attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs end if do dtFile = fso.getfile(sFile).datelastmodified if datediff("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,2,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 03:50:37 GMT |
|
 |
Pete #12 / 14
|
 how to write date modified into text file ?
but it does successfully create it as hidden..
Quote: > One question: is it possible to create the "c:\mylog.txt" file as a hidden > file?
This is your last 'freebie' ;-)... WSH 5.6 documentation download http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/00... sdncompositedoc.xml set fso = createobject("scripting.filesystemobject") sLog = "c:\mylog.txt" sFile = "\\server\share\file.exe" dtPrev = now() if fso.fileexists(sLog) then attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs if fso.getfile(sLog).size > 0 then sDate = fso.opentextfile(sLog,1).readline if isdate(sDate) then dtPrev = cdate(sDate) end if end if else fso.createtextfile sLog attribs = fso.getfile(sLog).attributes Or 2 'hidden fso.getfile(sLog).attributes = attribs end if do dtFile = fso.getfile(sFile).datelastmodified if datediff("s",dtPrev,dtFile) <> 0 then dtPrev = dtFile fso.opentextfile(sLog,2,true).writeline dtFile end if wscript.sleep 15 * 1000 loop while true '***this is an infinite loop*** -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 04:16:49 GMT |
|
 |
MVP #13 / 14
|
 how to write date modified into text file ?
Quote:
> The script creates the "c:\mylog.txt" file but errors with a "permission > denied" when it tries to write to the file, so the file ends up empty..
Well, it works perfectly for me exactly as written and posted (except for giving sFile the name of a real file)... Are you testing with my example or something you wrote using the example? If the latter is the case, then you'll have to post exactly the code you are using... If you have sufficient NT permissions to create the log file and then set the hidden attribute then you clearly have enough NT sufficient permissions to write to the file. The only reason you might not is if another process has it locked and/or opened for update precisely when *you* want to open/write to it... -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 04:47:14 GMT |
|
 |
Pete #14 / 14
|
 how to write date modified into text file ?
okay fair enough, thanks for help Michael.
Quote:
> The script creates the "c:\mylog.txt" file but errors with a "permission > denied" when it tries to write to the file, so the file ends up empty..
Well, it works perfectly for me exactly as written and posted (except for giving sFile the name of a real file)... Are you testing with my example or something you wrote using the example? If the latter is the case, then you'll have to post exactly the code you are using... If you have sufficient NT permissions to create the log file and then set the hidden attribute then you clearly have enough NT sufficient permissions to write to the file. The only reason you might not is if another process has it locked and/or opened for update precisely when *you* want to open/write to it... -- Michael Harris Microsoft.MVP.Scripting Seattle WA US --
|
Wed, 06 Oct 2004 09:18:47 GMT |
|
|
|