Friday, May 18, 2012 Register  Login

This site uses DNS Made Easy. Use it for reliable and professional DNS services.

RSS Feeds
Categories
  
Blog Archives
  
Blog

Recursing through both files and directories

I got tired of having to hand-copy file paths into the InnoSetup script for PainlessSVN. I'm specifically referring to the Subversion server files. There's something like 278 files, that are in nested folders. I had just finished pasting in all the paths for Subversion 1.6.13, when 1.6.15 was released and I was seriously dreading repeating this whole excersise again.

I needed something that would recurse through both files and folders. It's easy doing one or the other, but not both at the same time (at least for me). I spent about 3 hours last night working on this. The good thing about this code is that I needed something similar for the new DeleteDevCrumbs utility that I'm polishing for release.

As it currently stands, this code will create a list of files from the directory that is passed in through a command-line argument, scan the child directories, and create a text file for you. The file will contain a ready made list that can be pasted right in the [Files] section of an InnoSetup script.

Here's a console app with this code: 

using System.Collections.Generic;
using System.IO;

namespace ScanFilesTest {     class Program     {         private static List<string>  _tree = new List<string>();         static void Main(string[] args)         {             string rootDirectory = args[0];             var rootInfo = new DirectoryInfo(rootDirectory);             if (!rootInfo.Exists)             {                 throw new DirectoryNotFoundException(string.Format("{0} does not exist!", rootInfo.FullName));             }             FileInfo[] files = rootInfo.GetFiles();             if (files.Length > 0)             {                 ScanFolders(files);             }             else             {                 DirectoryInfo[] folders = rootInfo.GetDirectories();                 ScanFolders(folders);             }             TextWriter subversionList = new StreamWriter("D:\\subversionfilelist.txt");             foreach (var file in _tree)             {                 var currInfo = new FileInfo(file);                 string currDir = currInfo.FullName.Replace(rootDirectory, string.Empty);                 subversionList.WriteLine(string.Format("Source: {0}; DestDir: {{pf}}\\Subversion{1}; Components: Subversion", file, currDir));                 subversionList.Flush();             }             subversionList.Close();         }         private static void ScanFolders(FileInfo[] files)         {             foreach (var file in files)             {                 if (file != null && !string.IsNullOrEmpty(file.FullName))                 {                     _tree.Add(file.FullName);                  }             }             if (files.Length > 0)             {                 string root = files[0].DirectoryName;                 var rootInfo = new DirectoryInfo(root);                 DirectoryInfo[] folders = rootInfo.GetDirectories();                 if (folders.Length > 0)                 {                     ScanFolders(folders);                  }              }         }         private static void ScanFolders(IEnumerable<DirectoryInfo> folders)         {             foreach (var folder in folders)             {                 if (folder.GetFiles().Length > 0)                 {                     ScanFolders(folder.GetFiles());                 }                 else                 {                     if (folder.GetDirectories().Length > 0)                     {                         ScanFolders(folder.GetDirectories());                     }                     else                     {                         if (folder != null && !string.IsNullOrEmpty(folder.FullName))                         {                             _tree.Add(folder.FullName);                         }                     }                 }             }         }     } }

posted @ Thursday, January 27, 2011 10:18 AM by Hector Sosa, Jr

Actions:Tweet This Share on Facebook Share on LinkedIn Emakl Permalink del.icio.us

Previous Page | Next Page

Name (required)

Email (required)

Website

CAPTCHA image
Enter the code shown above:

Terms Of Use | Privacy Statement | SystemWidgets
Copyright 2002-2012 by SystemWidgets
Google Analytics Alternative