Skip to content

Commit

Permalink
DirectoryReader update
Browse files Browse the repository at this point in the history
Updated to handle exceptions
  • Loading branch information
ale2x72 committed Jun 16, 2024
1 parent 1dc7e1b commit 6951f2d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Binary file modified Example files/01_froGH_File I-O.gh
Binary file not shown.
61 changes: 57 additions & 4 deletions src/froGH/froGH/File I-O/DirectoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ protected override void SolveInstance(IGH_DataAccess DA)
DataTree<string> filesTree = new DataTree<string>();

GH_Path path = new GH_Path(0);

// if depthLevel is 0, add subdirs to the Dir output
if(depthLevel == 0) folderTree.AddRange(Directory.GetDirectories(P).Select(s => s.Remove(0, P.Length)).ToList(), path);
if(depthLevel == 0) folderTree.AddRange(ReadDirectories(P), path);
// else add the root dir
else folderTree.Add(P, path);

// scan the folder structure
TreeDirAndFiles(ref folderTree, ref filesTree, path, P, depthLevel);

DA.SetDataTree(0, folderTree);
Expand All @@ -76,12 +78,15 @@ protected override void SolveInstance(IGH_DataAccess DA)

private void TreeDirAndFiles(ref DataTree<string> folderTree, ref DataTree<string> filesTree, GH_Path path, string currentDir, int level)
{
// Add files in current dir
List<string> files = Directory.GetFiles(currentDir).Select(s => s.Remove(0, currentDir.Length)).ToList();
// Add files in current dir (or an error message for exceptions)
bool dirOK = ReadFiles(currentDir, out List<string> files);
filesTree.AddRange(files, path);

// if directory caused an exception return
if (!dirOK) return;

// check for subdirs
List<string> directories = Directory.GetDirectories(currentDir).ToList();
List<string> directories = ReadDirectoriesFullPath(currentDir);

// return condition
if (directories.Count == 0 || (level != -1 && path.Length > level)) return;
Expand All @@ -97,6 +102,54 @@ private void TreeDirAndFiles(ref DataTree<string> folderTree, ref DataTree<strin
}
}

public bool ReadFiles(string currentDir, out List<string> files)
{
files = new List<string>();

try
{
files = Directory.GetFiles(currentDir).Select(s => s.Remove(0, currentDir.Length)).ToList();
return true;
}
catch(Exception e)
{
files.Add("cannot read from this directory - " + e.Message);
return false;
}
}

public List<string> ReadDirectories(string currentDir)
{
List<string> directories = new List<string>();

try
{
directories = Directory.GetDirectories(currentDir).Select(s => s.Remove(0, currentDir.Length)).ToList();
}
catch (Exception e)
{
directories.Add("cannot read from this directory - " + e.Message);
}

return directories;
}

public List<string> ReadDirectoriesFullPath(string currentDir)
{
List<string> directories = new List<string>();

try
{
directories = Directory.GetDirectories(currentDir).ToList();
}
catch (Exception e)
{
directories.Add("cannot read from this directory - " + e.Message);
}

return directories;
}

public override void CreateAttributes()
{
m_attributes = new DirectoryReader_Attributes(this);
Expand Down

0 comments on commit 6951f2d

Please sign in to comment.