Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

TouchDirs: update "modified" timestamps of a direc


  • Please log in to reply
3 replies to this topic
andreas
  • Guests
  • Last active:
  • Joined: --
my 1st AHK script, hoping it may be useful for somebody else as well ...

;=======================================================================
; AutoHotKey script: TouchDirs.ahk (V1.0 08.08.06)
;
; set "modified" timestamp of a directory
; to the one of the newstest file OR directory in it
; propagate timestamps up the dir tree
; (empty dirs keep their timestamp)
;
; (possible extension: set "creation" timestamp to oldest file/dir)
;
; use script or parts of it as you like but at your own risk
;
; andreas lang, conTEXT AG, Zurich, Switzerland
;=======================================================================

#SingleInstance ignore               ; don't start script more than once

                                                               ; globals
mod = 0                 ; flag to check if any timestamp update happened
touched = 0                       ; updated timestamp for that many dirs

if 0 <> 1                                      ; exactly 1 parm required
{
  msgBox usage: TouchDirs pathspec
  exit
}

dir = %1%

IfNotExist, %dir%
{
  msgBox can't find "%dir%"
  exit
}

StringRight, lastDirChar, dir, 1                ; fetch last char of dir
If lastDirChar = \
{
  StringTrimRight, dir, dir, 1                 ; chop trailing backslash
}

                 ; round 1: set dir date to the one of newest FILE in it
TouchDirFromFiles(dir)

             ; round 2-n: set dir date to the one of newest SUBDIR in it
passes = 1
Loop
{                          ; do as long as any timestamp update happened
  If mod = 0
    break
  Else
  {
    mod = 0                                          ; assume we're done
    TouchDirFromDirs(dir)
    ++passes
  }
}

msgBox %touched% dir(s) touched in %passes% pass(es).

exit

;-----------------------------------------------------------------------
; functions
;-----------------------------------------------------------------------
TouchDirFromFiles(dir)
{
  global mod, touched
  actDir = %dir%
  FileGetTime, dirDate, %dir%, M

  Loop, %dir%\*.*, 1, 0      ; loop over files AND folders, no recursion
  {
    FileGetAttrib, attr, %A_LoopFileLongPath%

    IfNotInString, attr, D                               ; file: process
    {
      If A_LoopFileTimeModified > %dirDate%        ; file newer than dir
      {
        FileSetTime, %A_LoopFileTimeModified%, %actDir%, M, 2, 0
        dirDate = %A_LoopFileTimeModified%       ; remember new dir date
        mod = 1                                      ; timestamp updated
        ++touched
      }
    }
    Else                                                          ; dirs
    {
      TouchDirFromFiles(A_LoopFileLongPath)                 ; drill down
    }
  }
}
;-----------------------------------------------------------------------
TouchDirFromDirs(dir)
{
  global mod, touched
  actDir = %dir%
  FileGetTime, dirDate, %dir%, M

  Loop, %dir%\*.*, 2, 0           ; loop over folders only, no recursion
  {
    If A_LoopFileTimeModified > %dirDate%            ; newer than parent
    {
      StringRight, lastDirChar, dir, 1          ; fetch last char of dir
      If lastDirChar <> :                          ; if not drive itself
      {
        FileSetTime, %A_LoopFileTimeModified%, %actDir%, M, 2, 0
        dirDate = %A_LoopFileTimeModified%       ; remember new dir date
        mod = 1                                      ; timestamp updated
        ++touched
      }
    }

    TouchDirFromDirs(A_LoopFileLongPath)                    ; drill down
  }
}
;=======================================================================


andreas
  • Guests
  • Last active:
  • Joined: --
line 6 should obviously start with a ; instead of :

sorry, andreas

(note from moderator: it has been fixed in the post above)

Harmor
  • Members
  • 182 posts
  • Last active: Sep 20 2007 05:09 AM
  • Joined: 06 Nov 2005
Hehe...I noticed that too.

I remember writing a script like this, but I called it "violate.exe" instead of touch... :wink:
//TODO: Create kewl sig...

andreas
  • Guests
  • Last active:
  • Joined: --
GUIed version of TouchDirs
same functionality as in the command line version above

;=======================================================================
; AutoHotKey script: TouchFolders.ahk (V1.0 14.08.06)
;
; set "modified" timestamp of a directory
; to the one of the newstest file OR directory in it
; propagate timestamps up the dir tree
; (empty dirs keep their timestamp)
;
; (possible extension: set "creation" timestamp to oldest file/dir)
;
; use script or parts of it as you like but at your own risk
;
; andreas lang, conTEXT AG, Zurich, Switzerland
;=======================================================================

#SingleInstance ignore               ; don't start script more than once

                                                               ; globals
mod     = 0             ; flag to check if any timestamp update happened
touched = 0                       ; updated timestamp for that many dirs


FileSelectFolder, dir, *C:\, 0           ; don't allow making new folder
if ErrorLevel or dir =
{
  msgBox no folder selected. exit!
  exit
}

StringRight, lastDirChar, dir, 1                ; fetch last char of dir
If lastDirChar = \
{
  StringTrimRight, dir, dir, 1                 ; chop trailing backslash
}

                 ; round 1: set dir date to the one of newest FILE in it
TouchDirFromFiles(dir)

             ; round 2-n: set dir date to the one of newest SUBDIR in it
passes = 1
Loop
{                          ; do as long as any timestamp update happened
  If mod = 0
    break
  Else
  {
    mod = 0                                          ; assume we're done
    TouchDirFromDirs(dir)
    ++passes
  }
}

msgBox %touched% dir(s) touched in %passes% pass(es).

exit

;-----------------------------------------------------------------------
; functions
;-----------------------------------------------------------------------
TouchDirFromFiles(dir)
{
  global mod, touched
  actDir = %dir%
  FileGetTime, dirDate, %dir%, M

  Loop, %dir%\*.*, 1, 0      ; loop over files AND folders, no recursion
  {
    FileGetAttrib, attr, %A_LoopFileLongPath%

    IfNotInString, attr, D                               ; file: process
    {
      If A_LoopFileTimeModified > %dirDate%        ; file newer than dir
      {
        FileSetTime, %A_LoopFileTimeModified%, %actDir%, M, 2, 0
        dirDate = %A_LoopFileTimeModified%       ; remember new dir date
        mod = 1                                      ; timestamp updated
        ++touched
      }
    }
    Else                                                          ; dirs
    {
      TouchDirFromFiles(A_LoopFileLongPath)                 ; drill down
    }
  }
}
;-----------------------------------------------------------------------
TouchDirFromDirs(dir)
{
  global mod, touched
  actDir = %dir%
  FileGetTime, dirDate, %dir%, M

  Loop, %dir%\*.*, 2, 0           ; loop over folders only, no recursion
  {
    If A_LoopFileTimeModified > %dirDate%            ; newer than parent
    {
      StringRight, lastDirChar, dir, 1          ; fetch last char of dir
      If lastDirChar <> :                          ; if not drive itself
      {
        FileSetTime, %A_LoopFileTimeModified%, %actDir%, M, 2, 0
        dirDate = %A_LoopFileTimeModified%       ; remember new dir date
        mod = 1                                      ; timestamp updated
        ++touched
      }
    }

    TouchDirFromDirs(A_LoopFileLongPath)                    ; drill down
  }
}
;=======================================================================