How to load files according to Created Date in Windows command shell via SQL Server’s xp_cmdshell

Question:

In SQL Server, I am using a query below to load all “.jpg” file names from a specific directory (e.g. z:) into a table.

I want to know if there’s a way to load files according to Created Date instead of Modified Date in Windows command prompt. The query below only works with Modified Date when executing xp_cmdshell.

07/16/2015 in the variable @Command is the Modified Date. Obviously the command forfiles doesn’t have a clue to filter files by Created Date.

Below is a few results from the query given above in which FileNames are prefixed by Modified Date.

I have also tried using dir command with timefield /t:c (the creation time) something like

It gives me the Created Date but it shows me the following result which is not as expected. I want the file names with full path/directory names as shown in the previous result.

The expected result should be something like below,

Any help would be very appreciated 🙂

Answer:

Here is one way you can parse the output of the DIR command:

Source:

How to load files according to Created Date in Windows command shell via SQL Server’s xp_cmdshell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply