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
Alternatively you could create a simple program using C# for example like that:
// compile using c:\Windows\Microsoft.NET\Framework\v3.5\csc FileByAge.cs
using System;
using System.IO;
public static class Program
{
public static void Main(string[] args)
{
double age = double.Parse(args[0]);
string fileName;
while ((fileName = Console.ReadLine()) != null)
{
if(age >= (DateTime.Now - new FileInfo(fileName).CreationTime).TotalHours)
{
continue;
}
Console.WriteLine(fileName);
}
}
}
That program could then be used to find all files that are older than 2 hours within a batch file like that:
for /F "delims=" %f in ('dir /B *.txt^|FileByAge.exe 2') do echo %f
As mentioned by @Paul one could even build a hacky hybrid batch / c# file that will compile and run itself:
@echo off
setlocal
set CsFile=%TEMP%\%~n0.cs
set ExeFile=%TEMP%\%~n0.exe
pushd %SystemRoot%\Microsoft.NET\Framework
for /F %%f in ('dir /B /O:-N v*') do cd %%f & goto :BreakLoop;
:BreakLoop
(echo /* & type "%~f0") > "%CsFile%"
csc.exe /nologo /out:"%ExeFile%" "%CsFile%"
del "%CsFile%"
popd
more | "%ExeFile%" %*
del "%ExeFile%"
endlocal
goto:eof
*/
using System;
using System.IO;
public static class Program
{
public static void Main(string[] args)
{
var olderThen = args[0].Contains("older");
var targetAge = double.Parse(args[1]);
string fileName;
while ((fileName = Console.ReadLine()) != null)
{
if (string.Empty == fileName)
{
continue;
}
var fileAge = (DateTime.Now - new FileInfo(fileName).CreationTime).TotalHours;
if (olderThen ? targetAge > fileAge : targetAge < fileAge)
{
continue;
}
Console.WriteLine(fileName);
}
}
}
And can be used like this:
for /F "delims=" %f in ('dir /B *.txt^|FileByAge.bat older 2') do echo %f