[Resolved]How to map the files and subdirectories of a directory?

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

        public class RecursiveFileProcessor 
        {
            public static void Main(string[] args) 
            {
                foreach(string path in args) 
                {
                    if(File.Exists(path)) 
                    {
                        // This path is a file
                        ProcessFile(path); 
                    }               
                    else if(Directory.Exists(path)) 
                    {
                        // This path is a directory
                        ProcessDirectory(path);
                    }
                    else 
                    {
                   Console.WriteLine("{0} is not a valid file or directory.", path);
               }        
           }        
       }

       // Process all files in the directory passed in, recurse on any directories  
       // that are found, and process the files they contain. 
       public static void ProcessDirectory(string targetDirectory) 
       {
           // Process the list of files found in the directory. 
           string [] fileEntries = Directory.GetFiles(targetDirectory);
           foreach(string fileName in fileEntries)
               ProcessFile(fileName);

           // Recurse into subdirectories of this directory. 
           string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
           foreach(string subdirectory in subdirectoryEntries)
        ProcessDirectory(subdirectory);
       }

       // Insert logic for processing found files here. 
       public static void ProcessFile(string path) 
       {
           Console.WriteLine("Processed file '{0}'.", path);	    
       }
   }

2 Answers

2

For those who are humble. Who waste their time helping people. I leave the solution below. With this code you can map all subfolders and files in a given directory:
NOTE: I could not alone have helped me!

             ////////////////////
    using System.IO;

using UnityEngine;

using System;	

using System.Collections;

using System.Collections.Generic;

              

public class ListDirectory: MonoBehaviour {

public String[] paths;


void Start()
{
	foreach(string path in paths)
	{

		if(File.Exists(path))
		{
			Debug.Log("2" + path);
			// This path is a file
			ProcessFile(path);
		}              
		else if(Directory.Exists(path))
		{
			Debug.Log("3" + path);
			// This path is a directory
			ProcessDirectory(path);
		}
		else
		{
			Debug.Log("4" + path);
		}        
	}        
}


// Process all files in the directory passed in, recurse on any directories  
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
	string [] fileEntries = Directory.GetFiles(targetDirectory);
	foreach(string fileName in fileEntries)
		ProcessFile(fileName);
	
	string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
	foreach(string subdirectory in subdirectoryEntries)
		ProcessDirectory(subdirectory);
}

public static void ProcessFile(string path)
{
	Debug.Log("Processed file '{0}'." + path);          
}
}

@Jeff_Kesselman = NOB

wow! Thaanks!

And how much do you think he learned by your doing it for him? I am happy to help people. I do that all the time. Helping means leading them through their questions to find their own answers. I don't just write other people's projects for them, or rather, when I do, I charge $70 to $100 an hour. http://www.linkedin.com/in/kesselman If your time and skills aren't worth that much then thats really your business.

@Jeff_Kesselman, The world is made of people like you: OWNER OF YOUR OWN KNOWLEDGE. I very much appreciated the curriculum, but change its didactic teaching. She hides her humility. NOTE: Leave this forum if your interest is money!

@Jeff_Kesselman, I have no intention to offend you. Do not feel offended. I greatly admire all teachers. Just did replicate your answer, but tell that to David Helgason'm very happy for his life. I know your intention was that I hard I tried for my objectives. God bless you!

You don’t have an args list jun Unity so there is no way to use it as written.

Get your list of files however you are going to get it, then say

                foreach(string path in myFileList) 
                {
                    if(File.Exists(path)) 
                    {
                        // This path is a file
                        ProcessFile(path); 
                    }               
                    else if(Directory.Exists(path)) 
                    {
                        // This path is a directory
                        ProcessDirectory(path);
                    }
                    else 
                    {
                   Console.WriteLine("{0} is not a valid file or directory.", path);
               }

@Jeff_Kesselman, could you be more specific? I'm not able to make this code work when the game starts.

Thats pretty specific. Honestly, if you don't understand what a main() is and why its different in Unity, nothing short of my writing it for you is going to make it work. And Im really not into writing other peoples games for them. Now, if you have more specific error messages you would like explained, post them and I'll see what I can do.