HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Importing different type of files into Lists

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
intotypedifferentfileslistsimporting

Problem

I am using a recursive directory scan to find all objects inside a Unity3D project. After that, I wish to move all these files to a sorted List. Is there a cleaner and/or more optimal way of doing so? I have the feeling the AssignFileToList could be less complex than it currently is.

```
//Requestable total values
public int
TotalDirectory,
TotalFile,
TotalMaterial,
TotalTexture,
TotalCSharp,
TotalJava,
TotalBoo;

//Returns the total sum of all script coutns
public int TotalScript
{
get
{
return allCSharp.Count + allUnit.Count + allBoo.Count;
}
}

//Request a List with the sorted materials (not relevant to my current question)
public List MaterialList(string list)
{
if (list == "unique")
return unique;
else if (list == "broken")
return broken;
else if (list == "duplicate")
return duplicate;
else if (list == "dupof")
return dupof;
else
return null;
}

#endregion

//Assorted Lists
private List allMat = new List();
private List allTex = new List();
private List allCSharp = new List();
private List allBoo = new List();
private List allUnit = new List();
private List allShader = new List();

//not sure if gonna be used in future, but is here anyway.
private List allPrefab = new List();

//After shader sorting (not relevant to my current question)
private List unique = new List();
private List broken = new List();
private List duplicate = new List();
private List dupof = new List();

public void ScanSubDir(string dir)
{
//Count all directorys we reach and find all files inside of it.
TotalDirectory++;
FetchFilesFromDirectory(dir);

//Find all subdirectories of dir
List subDir = new List(Directory.GetDirectories(dir));
if (subDir.Count > 0)
{
//Let each sub directory run through again to see if another sub directory exists within
foreach (string subdir in subDir)
{
ScanSubDir(subdir);
}

Solution

I'm going to reiterate ANeves's answer and suggest that you not use Contains() to determine what the file extension is, but I don't recommend using EndsWith() either. It would be much better to use System.IO.Path's GetExtension method. Doing so should save you a number of headaches down the road when you come across some oddly named directories. (Do use a switch though!)

I don't like public variables.

//Requestable total values
public int 
    TotalDirectory,
    TotalFile,
    TotalMaterial,
    TotalTexture,
    TotalCSharp,
    TotalJava,
    TotalBoo;


As it is, client code could set these variables and you'd be left searching for a bug in the wrong part of the code base. It would be better to create read only properties from these. For example:

private int directoryCount;
public int TotalDirectories { get {return directoryCount;} }


Now you're counts are safe from inadvertent changes by the client.

Other properties could just return the count from whichever list you're interested in.

I feel that your ScanSubDir method is a classic example of recursion and well done, but it seems that there's a lot of void going on. I try to avoid using void when ever possible. It tends to mean that the code is manipulating things at a larger scope than it should. Consider if your methods should be returning something instead. Perhaps a count of the directories or files found.

This should definitely be converted to an enum and a switch.

//Request a List with the sorted materials (not relevant to my current question)
public List MaterialList(string list)
{
    if (list == "unique")
        return unique;
    else if (list == "broken")
        return broken;
    else if (list == "duplicate")
        return duplicate;
    else if (list == "dupof")
        return dupof;
    else
        return null;
}

Code Snippets

//Requestable total values
public int 
    TotalDirectory,
    TotalFile,
    TotalMaterial,
    TotalTexture,
    TotalCSharp,
    TotalJava,
    TotalBoo;
private int directoryCount;
public int TotalDirectories { get {return directoryCount;} }
//Request a List with the sorted materials (not relevant to my current question)
public List<Material> MaterialList(string list)
{
    if (list == "unique")
        return unique;
    else if (list == "broken")
        return broken;
    else if (list == "duplicate")
        return duplicate;
    else if (list == "dupof")
        return dupof;
    else
        return null;
}

Context

StackExchange Code Review Q#75134, answer score: 6

Revisions (0)

No revisions yet.