Import File Logging Application

In this post, I will use T-SQL and Microsoft C# to create a application to log file metadata into a SQL Server table.

Often times when processing data into and between data systems you need away to determine if all the data that is in the file is added to your tables. I like to store file information in the database and use this information as a way to true-up the data, in my data systems. When a file is stored on a file system it is stored with metadata that can provide very useful information. This information include file name, Creation time, Last Modified Time, File Path, File Size and you can all so get row count information. Collecting this information requires you to create a table inside the database to store the file metadata. You will also need to create an application to pull the metadata information from the files system folder directories.

In the code below I used C# to create an application that will accept a directory file path string. This path will then be used to search all sub directories in the path and log details about the files contained in the folders into a table. The C# code below contains three method calls that will be used to log the file information into a table.

The ProcessFile method is used to get the file name, Create Time, Last Access Time, File Size in Bytes and Line Count.

These data points are then passed to the LogToDB method. This method creates a SQL Connection to the database and executes a query to insert the data into a table named FileLogDetail.

To configure this to run you must first create a table to store the file information. Execute the below code in SQL Server Management Studio.

After the Table is created you will then need to create a C# console application. The code below is the source code needed.

Note: You will need to change the LogToDB method to connect your your SQL Server DB.


I created this solutions as a way to get detail information about the files on my file system. Although this is not the only way to achieve the goal logging information about files but it one way. I have found this code useful.


Leave a Reply