none
Move latest file to another folder based on the Filename timestamp

    Dotaz

  • Hi All,

    I need one help . I have a folder contains files like 

    Test050120180623.txt

    Test060220180623.txt

    Test061220180623.txt  

    I want to pick Test061220180623.txt    this latest file  based on this Timestamp in the name and move to another folder in ssis. 

    Can you please any one help me on this. any script or solution is appreciated. we dont have lastmodifiled date.

    Thanks,

    Srinivas

    čtvrtek 14. června 2018 10:53

Všechny reakce

  • https://stackoverflow.com/questions/1179970/how-to-find-the-most-recent-file-in-a-directory-using-net-and-without-looping

    OR in script file

    public void Main()
             {

          // TODO: Add your code here
                 var directory= new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());

                FileInfo[] files = directory.GetFiles();
                DateTime lastModified = DateTime.MinValue;

                 foreach (FileInfo file in files)
                {
                    if (file.LastWriteTime > lastModified)
                    {
                        lastModified = file.LastWriteTime;
                        Dts.Variables["User::VarFileName"].Value = file.ToString();
                    }
                }

                 MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());


                 Dts.TaskResult = (int)ScriptResults.Success;
             }


    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance
    SQL Server Integration Services: Business Intelligence

    čtvrtek 14. června 2018 11:02
  • see

    http://www.techbrothersit.com/2013/12/ssis-how-to-get-most-recent-file-from.html


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    čtvrtek 14. června 2018 11:29
  • Thanks for the reply . But Its not working .. i dont have Last modified date.  i have to check in the file name only 

    if (file.LastWriteTime > lastModified)  this condition will not work.  because all of my files have same lastModified value

     

    • Upravený Srisai4me čtvrtek 14. června 2018 11:42
    čtvrtek 14. června 2018 11:39
  • Thanks for the reply but Its not working .. i dont have Last modified date.  i have to check in the file name only 

    if (file.LastWriteTime > lastModified)  this condition will not work.  because all of my files have same lastModified value

    In this techbro article  they are comparing with lastmodifieddate. in my case all my files are same date only .. I mean i am downloading all files at a time .


    • Upravený Srisai4me čtvrtek 14. června 2018 11:42
    čtvrtek 14. června 2018 11:41
  • Its not working .. i have Last modified date.  i have to check in the file name only 

     

    Then you've to first have a for each loop first to get all filenames to a table. You can use execute sql task inside loop to insert filenames to table

    Have another execute sql task to get max value from the rows and return the corresponding filename

    store this in a variable in SSIS

    then have a file system task with source set as variable and pointing to the above created variable. Use operation as Move file and point to destination folder where you want to move the file.


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    čtvrtek 14. června 2018 11:43
  • Thanks, let me try this solution. 

    čtvrtek 14. června 2018 13:53