VB Script help for file modification

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

VB Script help for file modification

Post by kpassaur » Tue Jul 07, 2009 5:49 pm

I need some help converting this to MS - it is a script that should return the last modified time of a file.

strFile = "C:\myfile.dat"

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.GetFile(strFile)

wscript.echo "File Modified: " & CDate( objFile.DateLastModified)

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 07, 2009 6:01 pm

I'd turn it into a function:

Code: Select all

VBSTART
Function GetFileModifiedDate(strFile)
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  set objFile = objFSO.GetFile(strFile) 
  GetFileModifiedDate = CDate( objFile.DateLastModified)
End Function
VBEND

VBEval>GetFileModifiedDate("c:\myfile.dat"),modDate
MessageModal>File Modified: %modDate%
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 07, 2009 6:04 pm

But why not use FileDate and/or FileTime:

FileDate>c:\myfile.dat,fDate
FileTime>c:\myfile.dat,fTime
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Why not use FileDate and FileTime

Post by kpassaur » Tue Jul 07, 2009 6:19 pm

FileDate and FileTime return the creation time not the modified time. So, if I create a file and copy it the next day, the creation time is the next day (when the file was created). I need to be able to reset it to the Origional creation time which is the modified time.

I know it does not make sense but that seems to be what is happening. For instance I have a file created, modified, and accessed on June 5, when I copy it the new created is today, the modifed is June 5 and the accessed is Today.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 07, 2009 6:32 pm

FileDate and FileTime return the modified time. I just ran them on a file, then modified the file then called them again and got the new time.

But, no matter, you can use the VBScript too.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

modified time

Post by kpassaur » Tue Jul 07, 2009 6:44 pm

Yes, as that is the new time, I want the old time that is the issue. As I mentioned it makes no sense to me but when I think about it on some levels it does.

When you copy a file it is actually created (so the present time which is the created time) , when you modify it is the modified time. Now, when you copy a file and you need to reset it to the origional time it would be the last modified time.

I have been pulling my hair out trying to find out why. The issue for me is that each day files are scanned, and then copied into a folder, perhaps on the same day perhaps later. Once they are processed they need to have the origional scan date.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 07, 2009 6:46 pm

Well. I get the same results with the VBScript you posted as I do with FileDate/FileTime.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

modified date

Post by kpassaur » Tue Jul 07, 2009 6:56 pm

It has to be me as I am getting the same results now, the modifed time.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Move vs copy?

Post by gdyvig » Tue Jul 07, 2009 7:54 pm

Hi kpassaur ,

Originally you were getting the time the file was copied, but now you are getting the last modified time before the file was copied?

Perhaps the difference is how the files were copied. If you drag a file from one location to another on the same drive you have moved it and the creation and modified times will remain unchanged. But if you drag a file to another drive you are copying it and the creation time will be the copy time. I have not checked it out, but perhaps copy/moving files within a script does the same thing.





Gale

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

That would explain it

Post by kpassaur » Tue Jul 07, 2009 8:05 pm

I think you have hit the nail on the head, when testing sometimes I dragged them into a folder and others copied and pasted. All I know is that for two days sometimes it worked and sometimes not. Now that you brought this up it would explain it.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jul 07, 2009 8:58 pm

Hi All,

The MS FileDate> command supplies the Last Modified date so if you needed to find the Creation or Last Accessed dates of a file, I believe VBScript is necessary.

As a take-off on the function Marcus posted, see below for functions for all three file dates should anyone need them:

Code: Select all

VBSTART
Function GetFileDateCreated(strFile)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  set objFile = objFSO.GetFile(strFile)
  GetFileDateCreated = CDate(objFile.DateCreated)
End Function

Function GetFileDateLastModified(strFile)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  set objFile = objFSO.GetFile(strFile)
  GetFileDateLastModified = CDate(objFile.DateLastModified)
End Function

Function GetFileDateLastAccessed(strFile)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  set objFile = objFSO.GetFile(strFile)
  GetFileDateLastAccessed = CDate(objFile.DateLastAccessed)
End Function
VBEND

Let>FileName=c:\test.txt

VBEval>GetFileDateCreated("%FileName%"),DateCreated
VBEval>GetFileDateLastModified("%FileName%"),DateLastModified
VBEval>GetFileDateLastAccessed("%FileName%"),DateLastAccessed

MessageModal>File: %FileName%%CRLF%%CRLF%Created: %DateCreated%%CRLF%Last Modified: %DateLastModified%%CRLF%Last Accessed: %DateLastAccessed%
Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 07, 2009 10:14 pm

One more possible variable. According to Microsoft the file system will influence file times.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts