Home » Blog
date 10.Jul.2016

■ Shift file modification dates back/forward with scripting


Backup programs and operations depend on modification dates. Most of the time these maintain themselves as you work with files but there are occasions when manually setting dates is required, e.g. during daylight saving time (DST) changes. xplorer² has commands to set file dates (Actions > Change attributes menu), aka touch dates to a fixed value, and to synchronize dates in a pair of matching folders (Sync-touch menu command). The latter is very handy to correct DST phantom date differences of 1 hour introduced in folders stored on FAT partitions — but it only works for a single folder at a time.

Instead of adding a new command in xplorer² for deep folder touching, I present here a solution that can shift modification dates by a fixed amount backward or forward. It is a windows scripting host VBS script, whose essense minus error checking is:

REM create shell object for file
path = WScript.Arguments(0)
i = InStrRev(path, "\")
name = mid(path, i+1)
path = left(path, i)
Set objShell = CreateObject ("Shell.Application") 
Set objFolder = objShell.Namespace (path) 
set objFile = objFolder.ParseName(name)

szDate = objFile.ModifyDate
i = cint(WScript.Arguments(1)) ' take sign into account
objFile.ModifyDate = DateAdd("h", i, szDate)
Click to download CHMDATE.VBS date changing script.
USAGE: chmdate.vbs c:\path\to\file +-XX (where XX is number of hours to shift, negative for backward time)

funny that DOS console doesn't have any command for file dates manipulation

This script operates on one file at a time. It uses the ModifyDate property to read and change the modification date, plus DateAdd for date arithmetic. You must pass the filename and the amount of hours to shift as 2 command line arguments, e.g.
cscript chmdate.vbs c:\test.txt 2
would change the date of TEST.TXT by 2 hours (added to whatever it was before). You can run this command from xplorer² addressbar while browsing the folder where you extracted the CHMDATE.VBS script file. Try it with a couple of test files to get a feeling of it.

we are using the console server CSCRIPT to simplify passing arguments to the script

How to change dates of many files en masse
There are many ways to apply this script in xplorer² e.g. composing a batch script with <Ctrl+B>, but let's do it via a user command instead.

  1. note the full path where you placed the script (e.g. copy it with <Alt+C> command). Let's say it is C:\CHMDATE.VBS
  2. Use Customize > User commands > Add new menu and type in this command
    >> cscript c:\chmdate.vbs "$F" +1
    The special symbol $F stands for the full path of the selected file and we are shifting +1 hour ahead.
  3. optionally add a keyboard shortcut to the newly added command or/and add it as a toolbar button.
  4. select the files you need changed and execute the user command on them. To work with subfolders use e.g. File > Browse flat menu command to gather all the files in one window.

Note at step 2 we use a double >> prompt to repeat the command for all selected files. Otherwise a single > would execute the command once passing all the selected files as a collection argument

define user command for touch

I understand we are in the middle of the summer so this script isn't going to be required for some months, but at least you have ample time to understand the ins and outs of its workings till you actually need it next fall <g>

xplorer² version 4.2 (and later) has a dedicated Actions > Change dates menu command that can shift the modification and creation dates forward and backward much more easily.

Post a comment on this topic »

Share |

©2002-2016 ZABKAT LTD, all rights reserved | Privacy policy | Sitemap