{"id":982,"date":"2012-12-12T11:45:17","date_gmt":"2012-12-12T10:45:17","guid":{"rendered":"http:\/\/www.blue-bears.com\/blog\/?p=982"},"modified":"2012-12-12T11:46:37","modified_gmt":"2012-12-12T10:46:37","slug":"timeshift-changement-last-date-modif-de-tous-les-fichiersrep-dun-repertoire-a-la-date-du-jour","status":"publish","type":"post","link":"http:\/\/www.blue-bears.com\/blog\/?p=982","title":{"rendered":"TimeShift : Changement Last Date Modif de tous les fichiers\/rep d&rsquo;un r\u00e9pertoire \u00e0 la date du jour."},"content":{"rendered":"<p>&lsquo;VBA Visual Basic EXCEL<\/p>\n<p>&lsquo;Changing a files created, modified and accessed times.<br \/>\n&lsquo;The following code demonstrations how to alter the three file date stamps (creation, last accessed and last modified).<\/p>\n<p><!--more--><\/p>\n<p>Option Explicit<\/p>\n<p>Private oCollec As Collection<\/p>\n<p>Private Type FILETIME<br \/>\nLowDateTime As Long<br \/>\nHighDateTime As Long<br \/>\nEnd Type<\/p>\n<p>Private Type SYSTEMTIME<br \/>\nYear As Integer<br \/>\nMonth As Integer<br \/>\nDayOfWeek As Integer<br \/>\nDay As Integer<br \/>\nHour As Integer<br \/>\nMinute As Integer<br \/>\nSecond As Integer<br \/>\nMilliseconds As Integer<br \/>\nEnd Type<\/p>\n<p>Private Declare Function CreateFile Lib \u00ab\u00a0kernel32\u00a0\u00bb Alias \u00ab\u00a0CreateFileA\u00a0\u00bb (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<br \/>\nPrivate Declare Function SetFileTime Lib \u00ab\u00a0kernel32\u00a0\u00bb (ByVal hFile As Long, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As Long<br \/>\nPrivate Declare Function SystemTimeToFileTime Lib \u00ab\u00a0kernel32\u00a0\u00bb (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long<br \/>\nPrivate Declare Function CloseHandle Lib \u00ab\u00a0kernel32\u00a0\u00bb (ByVal hObject As Long) As Long<br \/>\nPrivate Declare Function LocalFileTimeToFileTime Lib \u00ab\u00a0kernel32\u00a0\u00bb (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long<\/p>\n<p>&lsquo;Purpose : To modify file creation, last accessed and last modified time for a file<br \/>\n&lsquo;Inputs : sFileName The path and name of the file to alter.<br \/>\n&lsquo; dFileDate The new file date.<br \/>\n&lsquo; [bSetCreationTime] If True alters the file creation time.<br \/>\n&lsquo; [bSetLastAccessedTime] If True alters the file last modified\/accessed time.<br \/>\n&lsquo; [bLastWriteTime] If True alters the file last write time.<br \/>\n&lsquo;Outputs : Returns True if succeeded in altering the file time.<br \/>\n&lsquo;Notes :<br \/>\n&lsquo;Revisions :<\/p>\n<p>Function FileSetDate(ByVal sFileName As String, ByVal dFileDate As Date, Optional bSetCreationTime As Boolean = False, Optional bSetLastAccessedTime As Boolean = False, Optional bSetLastModified As Boolean = False) As Boolean<br \/>\nConst GENERIC_WRITE = &amp;H40000000, OPEN_EXISTING = 3<br \/>\nConst FILE_SHARE_READ = &amp;H1, FILE_SHARE_WRITE = &amp;H2<\/p>\n<p>Dim lhwndFile As Long<br \/>\nDim tSystemTime As SYSTEMTIME<br \/>\nDim tLocalTime As FILETIME, tFileTime As FILETIME<\/p>\n<p>tSystemTime.Year = Year(dFileDate)<br \/>\ntSystemTime.Month = Month(dFileDate)<br \/>\ntSystemTime.Day = Day(dFileDate)<br \/>\ntSystemTime.DayOfWeek = Weekday(dFileDate) &#8211; 1<br \/>\ntSystemTime.Hour = Hour(dFileDate)<br \/>\ntSystemTime.Minute = Minute(dFileDate)<br \/>\ntSystemTime.Second = Second(dFileDate)<br \/>\ntSystemTime.Milliseconds = 0<\/p>\n<p>&lsquo;Open the file to get the filehandle<br \/>\nlhwndFile = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&amp;, OPEN_EXISTING, 0, 0)<br \/>\nIf lhwndFile Then<br \/>\n&lsquo;File opened<br \/>\n&lsquo;Convert system time to local time<br \/>\nSystemTimeToFileTime tSystemTime, tLocalTime<br \/>\n&lsquo;Convert local time to GMT<br \/>\nLocalFileTimeToFileTime tLocalTime, tFileTime<br \/>\n&lsquo;&#8212;&#8212;-Change date\/time property of the file<br \/>\nFileSetDate = True<br \/>\nIf bSetCreationTime Then<br \/>\nFileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, tFileTime, 0&amp;, 0&amp;))<br \/>\nEnd If<br \/>\nIf bSetLastAccessedTime Then<br \/>\nFileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, 0&amp;, tFileTime, 0&amp;))<br \/>\nEnd If<br \/>\nIf bSetLastModified Then<br \/>\nFileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, 0&amp;, 0&amp;, tFileTime))<br \/>\nEnd If<br \/>\n&lsquo;Close the file handle<br \/>\nCall CloseHandle(lhwndFile)<br \/>\nEnd If<br \/>\nEnd Function<\/p>\n<p>&lsquo;Demonstration routine<br \/>\nSub Test()<br \/>\n&lsquo;Set the creation time<br \/>\nFileSetDate \u00ab\u00a0C:\\temp\\Posters.html\u00a0\u00bb, Now, True<br \/>\n&lsquo;Set the last accessed time<br \/>\nFileSetDate \u00ab\u00a0C:\\temp\\Posters.html\u00a0\u00bb, Now, , True<br \/>\n&lsquo;Set the last write time<br \/>\nFileSetDate \u00ab\u00a0C:\\temp\\Posters.html\u00a0\u00bb, Now, , , True<br \/>\nEnd Sub<\/p>\n<p>Public Sub Macro1()<br \/>\nDim chemin As String<\/p>\n<p>chemin = InputBox(\u00ab\u00a0Entrez le chemin du r\u00e9pertoire\u00a0\u00bb, \u00ab\u00a0R\u00e9pertoire\u00a0\u00bb)<br \/>\n&lsquo;chemin = \u00ab\u00a0N:\\GestionSousMandat\\PTF_SOLDES\\\u00a0\u00bb<\/p>\n<p>Set oCollec = New Collection<br \/>\nSearchAllFilesInFolders (chemin)<br \/>\nAfficheListe<br \/>\nSet oCollec = Nothing<\/p>\n<p>End Sub<\/p>\n<p>Private Sub SearchAllFilesInFolders(ByVal chemin As String)<\/p>\n<p>Dim fso As FileSystemObject<br \/>\nDim dossier As Folder<\/p>\n<p>Set fso = New FileSystemObject<br \/>\nSet dossier = fso.GetFolder(chemin)<br \/>\nCall scanFolder(dossier)<\/p>\n<p>End Sub<\/p>\n<p>Private Sub scanFolder(ByVal dossier As Folder)<br \/>\nDim sousdossier As Folder<br \/>\nDim fichier As File<\/p>\n<p>For Each fichier In dossier.Files<br \/>\noCollec.Add fichier<br \/>\nNext<\/p>\n<p>For Each sousdossier In dossier.SubFolders<br \/>\nCall scanFolder(sousdossier)<br \/>\nNext<\/p>\n<p>End Sub<\/p>\n<p>Private Sub AfficheListe()<br \/>\nDim i As Long<br \/>\nDim lig As Long<br \/>\nDim ws As Worksheet<\/p>\n<p>Set ws = ThisWorkbook.Worksheets(1)<br \/>\nlig = 2<br \/>\nws.Columns(1).Clear<\/p>\n<p>With ws<br \/>\nFor i = 1 To oCollec.Count<br \/>\n.Range(\u00ab\u00a0A\u00a0\u00bb &amp; lig).Value = oCollec(i)<br \/>\nFileSetDate oCollec(i), Now, , , True<\/p>\n<p>lig = lig + 1<br \/>\nNext i<br \/>\nEnd With<br \/>\nEnd Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lsquo;VBA Visual Basic EXCEL &lsquo;Changing a files created, modified and accessed times. &lsquo;The following code demonstrations how to alter the three file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-informatique"],"_links":{"self":[{"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=982"}],"version-history":[{"count":2,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":984,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions\/984"}],"wp:attachment":[{"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blue-bears.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}