Search code examples
c#linqfileinfo

File Automation with FileInfo and Linq


Trying to come up with a way to walk a directory tree using either recursion or linq to get a file name that will have the following form: "C:\Program Files(x86)\Folder1\Folder2\TargetParentFolder\TargetFolder\TargetFolderName.ext" Basically the file name will be the exact same as the folder name and have a file extension of known type and will be in the TargetFolder directory. I then want to open the "TargetFolderName.ext" file find a particular string in that file (string will always be in the file) modify the string, save and close the file move back up the directory tree to the TargetParentFolder get the next subfolder and repeat the modification on the file in the next TargetFolder. So far what I have is this:

    FolderBrowserDialog folderPicker = new FolderBrowserDialog();
        if (folderPicker.ShowDialog() == DialogResult.OK)
        {
            lsvAddons.Items.Clear();
            List<string> folders = new List<string>(Directory.EnumerateDirectories(folderPicker.SelectedPath));
            foreach (var folder in folders)
            {
                var folderName = Path.GetDirectoryName(Path.GetDirectoryName(folder));

                var file = Directory.GetFiles(folderName, "*.toc", SearchOption.AllDirectories)
                .FirstOrDefault();
                ListViewItem item = new ListViewItem(folderName);
                item.Tag = folder;

                lsvAddons.Items.Add(item);

            }



        }

Ideally there will not be a ListView Control but a collection that will hold the folders and each matching filename to be iteratively modified once the collection is populated. Ideas?


Solution

  • I suggest using recursion, something like the following :

        void WalkDirectoryTree(System.IO.DirectoryInfo root)
            {
                System.IO.FileInfo[] files = null;
                System.IO.DirectoryInfo[] subDirs = null;
    
                try
                {
                    files = root.GetFiles("*.*");
                }               
                catch (UnauthorizedAccessException e)
                {                   
                }    
                catch (System.IO.DirectoryNotFoundException e)
                {
    
                }
    
                if (files != null)
                {
                    foreach (System.IO.FileInfo file in files)
                    {                        
                       if(file.Extension == ".ext"){
                            //open the file here, and make your modification
                        }
                    }
    
                    subDirs = root.GetDirectories();
    
                    foreach (System.IO.DirectoryInfo dirInfo in subDirs)
                    {
                        // Recursive call for each sub directory.
                        WalkDirectoryTree(dirInfo);
                    }
                }            
            }
    

    this will access each level and check the file, check this link for full example from MSDN : https://msdn.microsoft.com/en-us/library/bb513869.aspx