Windows Command Prompt – Load Files by Created Date

batchcmd.execommand linesql serverwindows

In SQL Server using xp_cmdshell, I was able to load files according to Modified date using a command for e.g.,

z: & forfiles /m *.jpg /s /d 07/16/2015 /c “cmd /c echo @fdate @ftime @path”

But I wanna know how to load files according to Created date. Any help would be very appreciated.

A question’s been asked on stackoverflow but no answer yet. Please take a look at the link for the details How to load files according to Created Date in Windows command prompt via SQL Server's xp_cmdshell

Best Answer

This is cheating a little bit, because it involves you compiling a little program that helps do this. It involves a little simple c# program but mostly cmd.

The thinking is, if only cmd included a program/command that when given a filename, it'd give give on one line, the file's full path and creation date/time. Then in cmd one could use a cmd's for command, applying that (show file's path and creation date/time) command to every file. So i've written a little program that can take a filename and show its full path and creation date/time. And then I use it in cmd.

The program..

C:\blah>type a.cs
using System.IO;

class asdf
{

 public static void Main(string[] args) {
    if(args.Length==0) {
         System.Console.WriteLine("Pass a filename");
         return;
     }
    System.IO.FileInfo fileInfo = new FileInfo(args[0]);
    System.Console.Write(fileInfo.FullName+"      ");

     System.DateTime creationTime = fileInfo.CreationTime;
    System.Console.WriteLine("cdt "+creationTime);

     }


}
C:\blah>

Compiling it

C:\blah>csc a.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.18408
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.


C:\blah>

so that creates a.exe

a.exe is a little program that when passed a filename as a parameter, gives the full path and creation date/time

C:\blah>a.exe w.obj<ENTER>
C:\blah\w.obj      cdt 25/07/2015 1:03:52 AM

C:\blah>

The rest is cmd

If you don't have a C# compiler

I've included a link here to a.cs (the source code above) a.exe and since chrome tries to block any exe, you could download a.ex2 and rename it. Or when Chrome tries to block the exe then say "dismiss" and go to downloads within chrome and say recover file. But however you want to do it, you can get that EXE. Maybe visual studio express (like a free small version of visual studio) will have csc.exe letting you compile it, if you want to do it that way.

http://ge.tt/2LdAiGN2

Once you have that little program then you can do this in cmd

c:\blah>for /r %f in (*.jpg) do @a.exe %f
c:\blah\a.jpg      cdt 31/07/2015 3:55:40 PM
c:\blah\a10.jpg      cdt 3/11/2013 8:19:07 AM
c:\blah\a11.jpg      cdt 3/11/2013 8:19:16 AM
c:\blah\a12.jpg      cdt 3/11/2013 8:48:45 AM
C:\blah\dff\Calendar.jpg      cdt 25/08/2009 3:00:02 AM
C:\blah\dff\CheckDST.jpg      cdt 28/07/2005 2:00:02 AM

So what you can do, is do that command but dumped to a file

C:\blah>del mynewfile.a

C:\blah>for /r %f in (*.jpg) do @a.exe %f >>mynewfile.a

C:\blah>

Then you can use find or gnuwin32's grep

C:\blah>find "22/05/2015" mynewfile.a

---------- MYNEWFILE.A
C:\blah\sdff\a.jpg      cdt 22/05/2015 5:54:53 AM
C:\blah\sdff\a2.jpg      cdt 22/05/2015 6:20:40 AM
C:\blah\sdff\b.jpg      cdt 22/05/2015 5:56:12 AM

or with gnuwin32's grep

C:\blah>grep "22/05/2015" mynewfile.a
C:\blah\sdff\a.jpg      cdt 22/05/2015 5:54:53 AM
C:\blah\sdff\a2.jpg      cdt 22/05/2015 6:20:40 AM
C:\blah\sdff\b.jpg      cdt 22/05/2015 5:56:12 AM

C:\blah>
Related Question