locked
loop through files based on last modified date RRS feed

  • Question

  • I have folder with files in it.I need to loop through all the files and load then into a table.But, the files should be loaded in a order .They should be modified based on lastmodified date .So, the oldest modified date file will be loaded first.

    After reasearching i came to know that , this sorting of files based on modified time cannot be done using for each loop container , we need to use script task and then the for each loop with ado enumerator .

    I am using visual studio 2005.

    Can some one suggest how should i load the files based on last modified  date.

    Friday, March 30, 2012 2:23 AM

Answers

  • Hi, 

    you can do one thing, load the file names and last modified datetime into some table. Create object type variable and then use this variable in Execute SQL Task. Query already populated table in the sequence you want and return result set to FullResultSet.

    Use foreach loop and object type variable, that way you will be able to load in sequence that you want.

    You can use this code to load filenames and modified date time into table. 

    http://sqlage.blogspot.com/2011/02/load-files-information-to-sql-server.html

    Thanks

    Aamir


    http://sqlage.blogspot.com/

    • Proposed as answer by Challen Fu Tuesday, April 3, 2012 8:58 AM
    • Marked as answer by Challen Fu Sunday, April 8, 2012 12:41 PM
    Friday, March 30, 2012 5:00 AM
  • Hi,

    Try using the FileInfo object and sort by the CreationTime property before you return it as the FullName which is the path. and put these results into the sql server table and then use the foreach loop to iterate this complete table according to id.

    public List<string> MapMyFiles() 
    { 
        List<FileInfo> batchaddresses = new List<FileInfo>(); 
        foreach (object o in lstViewAddresses.Items) 
        { 
            try 
            { 
                string[] files = Directory.GetFiles(o.ToString(), "*.csv"); 
                files.ToList().ForEach(f => batchaddresses.Add(new FileInfo(f))); 
            } 
            catch { } 
        } 
     
        return batchaddresses.OrderBy(f => f.CreationTime).Select(f => f.FullName).ToList(); 
    } 

    and another way is through WMI. please have a look in the following article:

    http://www.codeproject.com/Articles/46390/WMI-Query-Language-by-Example

    • Edited by Zaim Raza Friday, March 30, 2012 5:02 AM
    • Proposed as answer by Challen Fu Tuesday, April 3, 2012 8:58 AM
    • Marked as answer by Challen Fu Sunday, April 8, 2012 12:41 PM
    Friday, March 30, 2012 5:01 AM

All replies

  • Hi, 

    you can do one thing, load the file names and last modified datetime into some table. Create object type variable and then use this variable in Execute SQL Task. Query already populated table in the sequence you want and return result set to FullResultSet.

    Use foreach loop and object type variable, that way you will be able to load in sequence that you want.

    You can use this code to load filenames and modified date time into table. 

    http://sqlage.blogspot.com/2011/02/load-files-information-to-sql-server.html

    Thanks

    Aamir


    http://sqlage.blogspot.com/

    • Proposed as answer by Challen Fu Tuesday, April 3, 2012 8:58 AM
    • Marked as answer by Challen Fu Sunday, April 8, 2012 12:41 PM
    Friday, March 30, 2012 5:00 AM
  • Hi,

    Try using the FileInfo object and sort by the CreationTime property before you return it as the FullName which is the path. and put these results into the sql server table and then use the foreach loop to iterate this complete table according to id.

    public List<string> MapMyFiles() 
    { 
        List<FileInfo> batchaddresses = new List<FileInfo>(); 
        foreach (object o in lstViewAddresses.Items) 
        { 
            try 
            { 
                string[] files = Directory.GetFiles(o.ToString(), "*.csv"); 
                files.ToList().ForEach(f => batchaddresses.Add(new FileInfo(f))); 
            } 
            catch { } 
        } 
     
        return batchaddresses.OrderBy(f => f.CreationTime).Select(f => f.FullName).ToList(); 
    } 

    and another way is through WMI. please have a look in the following article:

    http://www.codeproject.com/Articles/46390/WMI-Query-Language-by-Example

    • Edited by Zaim Raza Friday, March 30, 2012 5:02 AM
    • Proposed as answer by Challen Fu Tuesday, April 3, 2012 8:58 AM
    • Marked as answer by Challen Fu Sunday, April 8, 2012 12:41 PM
    Friday, March 30, 2012 5:01 AM
  • I think Zaims script is useful,

    This is what comes to my mind right now...Create an object array[sFileName,sLastModifiedDate] Now sort this rray and now iterate the array on at a time.


    Please vote as helpful or mark as answer, if it helps
    Cheers, Raunak | t: @raunakjhawar | My Blog

    Friday, March 30, 2012 5:38 AM