Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Get FileName, Actual File Created date and Filesize in to sql table

$
0
0

Hi All

I have a scenario where I need capture FileName, File Created Date and file size in to a sql table. I was able to use a script component. But unfortunately the file creation time is not the actual file created date but its capturing the modified date.

Consider a file was created on source folder at 2.30 PM and I copied the file to target folder at 2.45 Pm , My code is pulling up the copied timestamp not the actual file created time stamp. Below is the code , Can you please how do I capture the actual creation date

  public void Main()
  {

            SqlConnection myADONETConnection = new SqlConnection();
            myADONETConnection = (SqlConnection)(Dts.Connections["GetFileInfo_Target"].AcquireConnection(Dts.Transaction) as SqlConnection);
            //MessageBox.Show(myADONETConnection.ConnectionString, "ADO.NET Connection");
            string DirPath = Dts.Variables["User::VarDirectoryPath"].Value.ToString();
            //get all files from directory
            string[] files = Directory.GetFiles(DirPath);
            SqlCommand sqlCmd= new SqlCommand();
            sqlCmd.Connection = myADONETConnection;
            //loop through files
            foreach (string filename in files)
            {
                FileInfo file = new FileInfo(filename);
                sqlCmd.CommandText = "INSERT INTO [dbo].[FileAttributes] ([Filename],[FileCreatedDateTime],[FileSize]) Values('" +
                file.Name + "','" + file.CreationTime + "','" + file.Length + "')";
                //MessageBox.Show(sqlCmd.CommandText);
                sqlCmd.ExecuteNonQuery();
              }

   Dts.TaskResult = (int)ScriptResults.Success;



Viewing all articles
Browse latest Browse all 24688


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>