VB Script help for file modification
Moderators: JRL, Dorian (MJT support)
VB Script help for file modification
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)
strFile = "C:\myfile.dat"
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.GetFile(strFile)
wscript.echo "File Modified: " & CDate( objFile.DateLastModified)
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
But why not use FileDate and/or FileTime:
FileDate>c:\myfile.dat,fDate
FileTime>c:\myfile.dat,fTime
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Why not use FileDate and FileTime
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.
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.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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.
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
modified time
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.
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.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
modified date
It has to be me as I am getting the same results now, the modifed time.
Move vs copy?
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
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
That would explain it
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.
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:
Take care
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%
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 -
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 -
