I'm trying to get the last modified date of a bunch of files on Windows. On GNU systems, I can just use something like stat -c %Y.

I know about the wmic command, but the problem with that is that it is very slow. On my system, running the command wmic datafile where Name="..." get LastModified /value is around 4 to 7 times slower than using a port of GNU stat in an MSYS2 shell.

For my purposes, I need an alternative that is faster and built-in to either Windows itself, or the Visual Studio development tools. I don't really care about the output format because I can parse it manually myself.

Does anyone know if something like that exists?

  • You can almost certainly find a Windows port of stat (search for "stat.exe"). If not, it's only a few lines of C to write.
    – AFH
    Dec 16 '17 at 22:29
  • @AFH The problem is that I need a solution that is built in to either Windows or the VS dev tools. I've searched, but wmic is the only thing I've found. Dec 16 '17 at 22:41
  • If you've got VS, write the program yourself (call stat() or lstat(), depending on how you want symbolic links handled). Or it's probably straightforward in PowerShell, but I've never used that more than minimally.
    – AFH
    Dec 16 '17 at 22:59
  • When I say "built in" I mean that I need something that is already available. Of course I could write my own, but for what I am doing that isn't an appropriate solution. I'm writing a script that I need to deploy on a bunch of Windows PCs with VS dev tools installed at minimum. Bundling an executable program, or worse, source code that needs to be built, isn't going to work for my particular situation. Powershell was something I hadn't thought of before, so I did just try that. Unfortunately, it is actually much slower than the wmic command. Dec 16 '17 at 23:39
  • Unless it is difficult in the context of local security or anti-virus policies, I can't see why you shouldn't distribute an executable with your script. If the PCs are on Windows 10, the Linux on Windows subsystem will already stat. You can invoke it with bash -c, though you will need to use the Linux, not the Windows file name.
    – AFH
    Dec 17 '17 at 12:48

The Window command-line DIR should do the job, if you can parse the output by field. Pipe the output to a file as needed.

For example, dir /o:d /t:w orders files by date [/o:d] using the last time written i.e. modified) field [/t:w] and produces the following:

Directory of C:\Intel\Logs

07/06/2016 10:28 PM ..

07/06/2016 10:28 PM 0 IntelCPHS.log

05/13/2017 10:01 PM 767,814 IntelGFX.log

10/17/2017 10:54 PM 51,608 IntelGFXCoin.log

If you need time to the second, try:

forfiles /c "cmd /c echo @file @ftime"

Enter dir /? to see options for viewing files of certain attributes, output format etc.

  • Excellent - I didn't know about @... options in ForFiles. It's a pity that you need to schedule a new copy of cmd just to do an echo. In Linux echo and several other internal commands also have external binaries to avoid this, but I don't know of a standard Windows command which will do the equivalent.
    – AFH
    Dec 17 '17 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.