Question

Change file date and time stamp

Asked by: MICS

I have a vb 6.0 program that i need to change the date and time stamp on a file i created. Can anyone give me the code to do this.


thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2002-05-27 at 09:06:36ID20305077
Tags

change

Topic

Visual Basic Programming

Participating Experts
4
Points
75
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Date Stamp
    In VB I have a script that opens a file, adds a date stamp, emails the file (as an attachment), and then moves the file to an archive folder. This script runs daily. As long as it is emailed the same day it is created it work fine but if it is sent the following day it doesn'...
  2. Change a file's date/time stamp in VB
    Hello, I know it's possible to read a file's date/time stamp in VB, but is it possible to change it? Thanks for the help, John

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: acperkinsPosted on 2002-05-27 at 09:17:35ID: 7037425

Please maintain these open questions:
Printing in Javascript Date: 03/20/2002 08:43AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20279360
Printing Date: 01/31/2002 05:58AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20261587

Thanks,
Anthony

 

by: Richie_SimonettiPosted on 2002-05-27 at 13:14:07ID: 7037708

'From www.allapi.net

'This project needs a Common Dialog box, named CDBox.
'  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
'   and select Microsoft Common Dialog control)
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
    Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'KPDTeam@Allapi.net
    Dim m_Date As Date, lngHandle As Long
    Dim udtFileTime As FILETIME
    Dim udtLocalTime As FILETIME
    Dim udtSystemTime As SYSTEMTIME
    m_Date = Format(Now, "DD-MM-YY")

    'Set the dialog's title
    CDBox.DialogTitle = "Choose a file ..."
    'Set the dialog's filter
    CDBox.Filter = "All Files (*.*)|*.*"
    'Show the 'Open File'-dialog
    CDBox.ShowOpen

    udtSystemTime.wYear = Year(m_Date)
    udtSystemTime.wMonth = Month(m_Date)
    udtSystemTime.wDay = Day(m_Date)
    udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
    udtSystemTime.wHour = Hour(m_Date)
    udtSystemTime.wSecond = Second(m_Date)
    udtSystemTime.wMilliseconds = 0

    ' convert system time to local time
    SystemTimeToFileTime udtSystemTime, udtLocalTime
    ' convert local time to GMT
    LocalFileTimeToFileTime udtLocalTime, udtFileTime
    ' open the file to get the filehandle
    lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    ' change date/time property of the file
    SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
    ' close the handle
    CloseHandle lngHandle
    MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub

 

by: MICSPosted on 2002-05-27 at 13:34:11ID: 7037730

umm... it must be easier than that.  I want to change a file that is hard coded. lets say "C:\MyFile.txt" and i want its created date to be Jan 9, 1996...

 

by: Richie_SimonettiPosted on 2002-05-27 at 13:46:33ID: 7037744

Well, maybe there is another "simple" way but you did ask for the code, not the easiest one.
With Windows, the most simple thing could be harder to implement.
Cheers

 

by: inthedarkPosted on 2002-05-27 at 21:30:58ID: 7038375

Date and time handling is much more complicated than most people realise.  In fact it is so complicated that even Microsoft screwed it up.  In Windows NT if you create a file in January and then view the time of create of the same file in April, you will see that the time of creation changes.

The problem is that file times are meant to be stored in GMT and then translated into local time when they are viewed but somehow it all went wrong.  Recently I was working on a band new Windows 2000 server from Dell it was configured with US Date formats, I loaded a Novell driver which changed the way that filetime worked.  We changed the date format of the server to UK date format and my VB project forms (ABOUT 100 OF THEN) got redated back to the fifties and sixties.

In summary Richie_Simonetti, don't waste much time looking for a simpler way, I don't think there is one.....

 

by: inthedarkPosted on 2002-05-27 at 22:19:12ID: 7038405

The example posted had a bug which I have now fixed. I turned the example into an easy-to-use class.

Example of usage:

Private Sub Command1_Click()

Dim GF As New zGF         ' General Functions

Dim dt As New zFileDates
Dim d$
Dim FilePath$
Dim newdate As Date

' get the path to re-date
FilePath = "D:\CustData\Abacus\Utilities\PrintOrders\"
GF.CheckSlash FilePath ' make sure has a slash at the end


newdate = Now

' Read files from the path
d$ = Dir$(FilePath + "*.*")
Do While Len(d$) > 0
    ' change the file date/time
    dt.UpdateFileTime FilePath + d$, newdate
    d$ = Dir
Loop
MsgBox "Done"


End Sub

===============end of example

Here is the

1)Create a new class module
2)paste the following code
3)change the class name to zFileDates

====Code for zFileDates class=========

Option Explicit

' converted to a class form the code supplied here:
' the code from this location had a date/time bug
' which was fixed
' please report any errors to:
' nyoung@nyoung@vipintersoft.com

   'KPD-Team 1998
   'URL: http://www.allapi.net/
   'KPDTeam@Allapi.net

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
   wYear As Integer
   wMonth As Integer
   wDayOfWeek As Integer
   wDay As Integer
   wHour As Integer
   wMinute As Integer
   wSecond As Integer
   wMilliseconds As Integer
End Type

Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
         (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
   ByVal dwShareMode As Long, _
   ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, _
   ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, _
   lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" _
    (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime _
    Lib "kernel32" (lpLocalFileTime As FILETIME, _
    lpFileTime As FILETIME) As Long

Public Sub UpdateFileTime(FileName As String, NewDateTime As Date)
 
   Dim lngHandle As Long
   Dim udtFileTime As FILETIME
   Dim udtLocalTime As FILETIME
   Dim udtSystemTime As SYSTEMTIME

   udtSystemTime.wYear = Year(NewDateTime)
   udtSystemTime.wMonth = Month(NewDateTime)
   udtSystemTime.wDay = Day(NewDateTime)
   udtSystemTime.wDayOfWeek = Weekday(NewDateTime) - 1
   udtSystemTime.wHour = Hour(NewDateTime)
   udtSystemTime.wMinute = Minute(NewDateTime) ' this was missing
   udtSystemTime.wSecond = Second(NewDateTime)
   udtSystemTime.wMilliseconds = 0

   ' convert system time to local time
   SystemTimeToFileTime udtSystemTime, udtLocalTime
   
   ' convert local time to GMT
   LocalFileTimeToFileTime udtLocalTime, udtFileTime
   
   ' open the file to get the filehandle
   lngHandle = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
   
   ' change date/time property of the file
   SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
   
   ' close the handle
   CloseHandle lngHandle
   
End Sub





 

by: acperkinsPosted on 2002-05-27 at 23:44:07ID: 7038531

MICS,

Richie has given you the best solution.  The only other shorter solution that I know of is to use the File System Object.  Note that there are in fact three file dates involved, as you can see by going to the Properties window in Explorer.

In order to use the following code you need to make a reference to the Microsoft Scripting Runtime.  And uncomment the date(s) you want changed.

Sub ChangeFileDate(ByVal FileName As String, ByVal FileDate As Date)
Dim fs As Scripting.FileSystemObject
Dim fil As Scripting.File

Set fs = New Scripting.FileSystemObject
Set fil = fs.GetFile(FileName)
'fil.DateCreated = FileDate
'fil.DateLastModified = FileDate
fil.DateLastModified = FileDate
Set fil = Nothing
Set fs = Nothing

End Sub

 

by: acperkinsPosted on 2002-05-27 at 23:46:04ID: 7038535

Sorry about the typo:
Sub ChangeFileDate(ByVal FileName As String, ByVal FileDate As Date)
Dim fs As Scripting.FileSystemObject
Dim fil As Scripting.File

Set fs = New Scripting.FileSystemObject
Set fil = fs.GetFile(FileName)
'fil.DateCreated = FileDate
'fil.DateLastAccessed = FileDate
fil.DateLastModified = FileDate

Set fil = Nothing
Set fs = Nothing

End Sub

 

by: MICSPosted on 2002-05-28 at 05:46:44ID: 7039100

acperkins,

the method that you posted will not work,

fil.DateCreated = FileDate
fil.DateLastAccessed = FileDate
fil.DateLastModified = FileDate

you cannot use these to assign values, they are read only.

 

by: Richie_SimonettiPosted on 2002-05-28 at 06:43:10ID: 7039249

All of this is nice but creating a class for this? Why load such object in memory and waste resources?
OOP is good 8 and i like it) but not apply it to everything always.

 

by: acperkinsPosted on 2002-05-28 at 06:51:48ID: 7039272

>>you cannot use these to assign values, they are read only<<
I stand corrected, my mistake.

Anthony

 

by: MICSPosted on 2002-05-28 at 08:02:02ID: 7039447

there were a few bugs that were fixed by inthedark.

inthedark, i have posted another question with 50points for you, for fixing the bugs.

thanks guys

 

by: inthedarkPosted on 2002-05-28 at 16:23:18ID: 7040601

Thanks Richie my app got redated nicely thanks....one day I'll post a question "form code vs class code" but right now I'm taking a bath with too many crocodiles.

 

by: dsackerPosted on 2003-02-04 at 17:17:26ID: 7879317

Does anyone have a solution for changing the file dates via VB Script or WSH scripts?  I'm administering some NT scheduled tasks across multiple platforms, and have already used the FileSystemObject to successfully timestamp files for archival sake.  The API solutions above do not work in VBScript.  Any suggestions (other than writing a VB executable program *lol*)?

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...