Quite simply, how would I find out if a specific file in a folder is older than X hours? The file name and location will always be the same.
Thanks
Quite simply, how would I find out if a specific file in a folder is older than X hours? The file name and location will always be the same.
Thanks
The solution to this will depend much on the expected time-range, since AFAIK you don't get time as one integer in a batch file without external programs. This means you have to parse the time value in your script, and the amount of parsing and the number of calculations needed, depend on the range you require it to work for. Here is one simple solution. It assumes that the file can NOT be older than 24hours.
On comparing file times with batch code following must be taken into account:
1. Date and time format settings
There are the environment variables DATE and TIME which on access (and not on start of batch execution) return current date and time in a format depending on Windows Region and Language settings. The country defines the date and time format.
It is even possible there to define for example for German short date format with
DD.MM.YYYY
(day and month with leading zero) and time format withHH:mm:ss
(24 hour format with leading zero for hour, minute and second). But this does not mean that value of DATE and TIME are really using this format. For example TIME is in formatH:mm:ss,ms
when having selected a German country even withHH:mm:ss
defined which means no leading zero on hour, but on minute and second, and additionally milliseconds after a comma in time string. The date string can be also with or without weekday depending on region settings.And there is the pattern
%~t
to get last modification date and time of a directory or file. Run in a command prompt windowcall /?
andfor /?
and read all help pages displayed for those two commands for details. The format of the date and time string returned by%~t
as well as displayed on output of command DIR depends also on Windows region settings, but is usually not equal the format of variables DATE and TIME. The time of last modification time of a file or directory is usually returned without second value in date/time string.Best is to find out with following batch file best executed before 10:00 AM what are the formats on current machine for current user.
2. File system of storage media
On Windows file systems two storage formats are used for the file times:
On storage media using NTFS the NTFS precision time is used which is a 64-bit number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
On storage media using FAT, FAT16, FAT32 or exFAT the file times are stored using DOS Date Time format with two 16-bit values with a resolution of 2 seconds and using current local time.
This means an odd second is not possible for a file on FAT media, but on NTFS media. Copying a file on an NTFS drive with an odd second in last modification time to a FAT32 drive results in having identical files with 1 second difference in last modification time.
The file times on NTFS media are stored in UTC. So what is returned by
%~t
or command DIR as well as what is displayed for example in Windows Explorer depends onChanging any of those 4 parameters results in an immediate change of ALL displayed file times of files and directories on NTFS storage media.
File times on FAT media are more or less constant as using local time on creation, modification or last access, but read further for details.
3. Time zone, daylight saving time
As file times on NTFS media are stored in UTC, but displayed in local time, the current time zone and the daylight saving time for this time zone are important for file time comparisons, especially when done for files on NTFS and FAT32 drives.
But also for files on a FAT media, the time zone and daylight saving time settings could be important depending on version of Windows. Read below for details.
4. Automatic adjustment for daylight saving time
There is the setting for automatic adjustment for daylight saving time which becomes important when being enabled as by default and currently set time zone defines a daylight saving time adjustment.
For example daylight saving time is active currently in Europe. Disabling this option in clock settings of Windows results in an immediate change of the displayed file times on NTFS media by -1 hour, and also many files on FAT media depending on version of Windows.
This 1 hour time difference caused by the daylight saving time adjustment must be taken into account on comparing file times on NTFS and FAT media.
5. Version of Windows
NTFS is supported by all Windows NT versions. File times behavior is equal for files and directories on all Windows versions supporting NTFS.
But how file times on FAT media are interpreted depends on version of Windows. Prior Windows Vista the file times on FAT media are independent on change of time zone, daylight saving time setting and automatic adjustment of daylight saving time. Last modification date/time of a file is constant on Windows 2000 and Windows XP.
But on Windows Vista and later the displayed file times on FAT media depend on daylight saving time and automatic adjustment for daylight saving time. The time zone does not directly matter. Selecting another time zone as currently used does not change the displayed file times of files on FAT media as it does for files on NTFS media. But if current time is within daylight saving time period of the active time zone and the automatic adjustment for daylight saving time is enabled and the local time of the file or directory is within the daylight saving time period, +1 hour is added by Windows Vista and later. The files last modified on FAT drives within standard time are constant over the year; just the files last modified within DST period are displayed with or without +1 hour depending on current time with enabled automatic DST adjustment.
That different FAT file time management can be extremely confusing when having for example a public share of a folder on a FAT32 drive on a Windows 7 machine and being connected to this public folder with a Windows XP machine. A file last modified on 2015-09-19 17:39:08 stored on FAT32 drive on Windows 7 PC is displayed today by Windows 7 with German time zone with file time 19.09.2015 17:39:08, but in 2 months although not modified with 19.09.2015 16:39:08, and Windows XP displays same file on connected Windows 7 PC today and in future constant with file time 19.09.2015 17:39:08.
Comparing file times of files stored in archives (ZIP, RAR, ...) with files on NTFS or FAT media can be really a nightmare.
6. Comparing file time with current time
For this task comparing last modification time of a file with current time, it should be enough to just take date and time format settings into account.
The batch code below uses code explained in detail in my answer on Batch file to delete files older than N days. This explanation should be read before using the code below.
Depending on date/time format of the variables DATE and TIME and date/time string returned by
%~tI
for the file on local Windows PC, it might be necessary to make small modifications to code. Appropriate hints are given in the comment lines of batch code.rojo added information on how date and time format settings, file system and Windows version can be ignored by using wmic - the Windows Management Instrumentation command-line utility.
Using wmic has the advantage of fixed format for the date/time strings and therefore the batch code works on all Windows computers without adaptations to local date/time format.
Also version of Windows does not matter on using wmic because of internal usage of GetFileTime function (most likely).
The file system becomes only important on evaluating also minutes offset of local time / file time to UTC. Command wmic returns on FAT drives just
+***
for the minutes offset to UTC while the minutes offset is correct for the last modification file time of a file on an NTFS drive.Example for same file on NTFS and FAT32 drive:
+120 is sum of +60 minutes for CET (Central European Time) and +60 minutes for active daylight saving time, or in other words +120 minutes for CEST (Central European Summer Time).
Therefore the batch code below does not evaluate the minutes offset to UTC which is not needed on comparing last modification file time of a file with current local time.
Further the file name must be specified always with full path in wmic command line. Just specifying the file name for a file in current directory or a file name with relative path does not work. And each backslash must be escaped with one more backslash in file name with path.
The main disadvantage on using wmic is the speed. The batch code above needs on my computer about 50 ms to finish according to Process Monitor log (average of several runs). The batch code below calling wmic twice needs for the same task about 1500 ms to finish (also an average value). Therefore using wmic on a large number of files is definitely not a good idea if it can be avoided because local date/time format is known.
Alternatively you could create a simple program using C# for example like that:
That program could then be used to find all files that are older than 2 hours within a batch file like that:
An improved and self-compiling version
As mentioned by @Paul one could even build a hacky hybrid batch / c# file that will compile and run itself:
And can be used like this:
Pure batch is horrible at date math. What happens if your script runs shortly after midnight? What if the time check spans a Daylight Savings Time change?
I propose invoking Windows Script Host and borrowing from a language that better handles date / time math. Here's a hybrid batch / JScript script that'll compute the age of a file or directory pretty easily. Save it with a .bat extension and give it a shot.
For more information on batch / JScript hybrid scripts, see this GitHub page. The style used above is similar to Hybrid.bat on that page. This answer is based on another. For what it's worth, PowerShell is also good at handling date math; but WSH executes much more quickly.