How do you get the names of the files and/or directories inside a given folder on your system with Grasshopper? You can do it with the C# component. James Ramsden has provided a simple solution to get the directory names inside a given path using the DirectoryInfo class. You can do the same to get file names using the FileInfo class. The better part is that it works both in the MacOS and Windows versions of Rhino.

Use the following code inside a C# component with a input named path taking objects of type string.

private void RunScript(string path, ref object Dirs, ref object Files)
{
  System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(path);
  System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
  System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");
  System.IO.FileInfo[] fileInfos = dirInfo.GetFiles("*.*");

  List<string> dirNames = new List<string>();
  List<string> fileNames = new List<string>();

  foreach(System.IO.DirectoryInfo d in dirInfos)
  {
    dirNames.Add(d.Name);
  }

  foreach(System.IO.FileInfo f in fileInfos)
  {
    fileNames.Add(f.Name);
  }

  Dirs = dirNames;
  Files = fileNames;
}


Leave a Reply

Your email address will not be published. Required fields are marked *

nine + nine =


@