Search code examples
c#arraysssisetlsubdirectory

Object variable only showing the last value


The code below returns only the most recent value in the loop. What do I need to do for it to show all the values that are being iterated over? The reason I am using this method instead of SearchOption.AllDirectory is because there is a folder path I am not able to access. I did try using UnauthorizedAccessException with a try and catch, but that returns an empty value because it keeps on terminating the loop.

public void Main()
{
    string path = @"drive"; // TODO  
    ApplyAllFiles(path, ProcessFile);
}    

public void ProcessFile(string path) 
{
    /* ... */
}

public void ApplyAllFiles(string folder, Action<string> fileAction)
{
    System.Collections.ArrayList FileList = new System.Collections.ArrayList();
    List<string> logger = new List<string>();

    DateTime DateFilter = DateTime.Now.AddMonths(-6);

    foreach (string file in Directory.GetDirectories(folder))
    {
        fileAction(file);
    }
    foreach (string subDir in Directory.GetDirectories(folder))
    {
        string rootfolder = "root folder";

        if (subDir.Contains(rootfolder))
        {   
            FileList.Add(subDir);
            Dts.Variables["User::objDirectoryList"].Value = FileList;
           //MessageBox.Show(subDir);
        }
        try
        {
            ApplyAllFiles(subDir, fileAction);
        }
        catch (UnauthorizedAccessException e)
        {
            logger.Add(e.Message);

        }
        catch (System.IO.DirectoryNotFoundException e)
        {
            logger.Add(e.Message);
        }
    }
}

Solution

  • Try to remove the FileList declaration outside the function, also remove Dts.Variables["User::objDirectoryList"].Value = FileList; outside the loop:

    System.Collections.ArrayList FileList = new System.Collections.ArrayList();
    
    public void Main()
    {
        string path = @"drive"; // TODO  
        ApplyAllFiles(path, ProcessFile);
        Dts.Variables["User::objDirectoryList"].Value = FileList;
    }    
    
    public void ProcessFile(string path) 
    {
        /* ... */
    }
    
    public void ApplyAllFiles(string folder, Action<string> fileAction)
    {
    
        List<string> logger = new List<string>();
    
        DateTime DateFilter = DateTime.Now.AddMonths(-6);
    
        foreach (string file in Directory.GetDirectories(folder))
        {
            fileAction(file);
        }
        foreach (string subDir in Directory.GetDirectories(folder))
        {
            string rootfolder = "root folder";
    
            if (subDir.Contains(rootfolder))
            {   
                FileList.Add(subDir);
                //MessageBox.Show(subDir);
            }
            try
            {
                ApplyAllFiles(subDir, fileAction);
            }
            catch (UnauthorizedAccessException e)
            {
                logger.Add(e.Message);
    
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                logger.Add(e.Message);
            }
        }
    }