- Home /
how to do infinitive loop for finding all sub folders / files C#
ok I've managed to do a loop of 5 Subfolders and all files in them
but I wander how do I create a loop that loops it self and knows he's own loop changes a bit each time it loops it self of it has finished it's loop
I know it's a bit complicated - how do I make this code going infinitive (or untill last subfolder found)
public List`<`TextureC> texturec ;
public string[] dirPaths;
public string[] dirPaths1;
public string[] dirPaths2;
public string[] dirPaths3;
public string[] dirPaths4;
public string[] dirPaths5;
public string[] filePaths;
public string[] filePaths1;
public string[] filePaths2;
public string[] filePaths3;
public string[] filePaths4;
public string[] filePaths5;
void ResizeTextures(){
dirPaths = Directory.GetDirectories(Application.dataPath + "/Resources/Textures/");
for (int di = 0; di < dirPaths.Length ; di++){
filePaths = Directory.GetFiles(dirPaths[di]);
for (int fi = 0; fi < filePaths.Length; fi++){
texturec.Add (new TextureC (filePaths[fi]));
}
dirPaths1 = Directory.GetDirectories(dirPaths[di]);
for (int dii = 0; dii < dirPaths1.Length; dii++){
filePaths1 = Directory.GetFiles(dirPaths1[dii]);
for (int fii = 0; fii < filePaths1.Length; fii++) {
texturec.Add (new TextureC (filePaths1[fii]));
}
dirPaths2 = Directory.GetDirectories(dirPaths1[dii]);
for (int diii = 0; diii < dirPaths2.Length; diii++){
filePaths2 = Directory.GetFiles(dirPaths2[diii]);
for (int fiii = 0; fiii < filePaths2.Length; fiii++) {
texturec.Add (new TextureC (filePaths2[fiii]));
}
dirPaths3 = Directory.GetDirectories(dirPaths2[diii]);
for (int diiii = 0; diiii < dirPaths3.Length; diiii++){
filePaths3 = Directory.GetFiles(dirPaths3[diiii]);
for (int fiiii = 0; fiiii < filePaths3.Length; fiiii++) {
texturec.Add (new TextureC (filePaths3[fiiii]));
}
dirPaths4 = Directory.GetDirectories(dirPaths3[diiii]);
for (int diiiii = 0; diiiii < dirPaths4.Length; diiiii++){
filePaths4 = Directory.GetFiles(dirPaths4[diiiii]);
for (int fiiiii = 0; fiiiii < filePaths4.Length; fiiiii++) {
texturec.Add (new TextureC (filePaths4[fiiiii]));
}
dirPaths5 = Directory.GetDirectories(dirPaths4[diiiii]);
for (int diiiiii = 0; diiiiii < dirPaths5.Length; diiiiii++){
filePaths5 = Directory.GetFiles(dirPaths5[diiiiii]);
for (int fiiiiii = 0; fiiiiii < filePaths5.Length; fiiiiii++) {
texturec.Add (new TextureC (filePaths5[fiiiiii]));
}
}
}
}
}
}
}
}
and if you wander about class (nothing special yet)
my Class
using UnityEngine;
using System.Collections;
[System.Serializable]
public class TextureC {
public string names ;
public string paths ;
public string filenames ;
public Texture2D texture ;
// Constructors
public TextureC (){}
public TextureC (string path){
paths = path ;
}
public TextureC (Texture2D tex){
texture = tex ;
}
}
sorry about generic list forgot how to make >< visible around TextureC (public list TextureC texturec)
Answer by Dave-Carlile · Feb 10, 2013 at 02:55 PM
How about Directory.GetFiles?
You can pass SearchOption.AllDirectories
to Directory.GetFiles
and it will recurse all subfolders for you.
:D totally forgot this one. However in some situations you might want to exculde certain directories.
Anyway +1
my mistake for comments thanks
dirPaths = Directory.GetFiles(Application.dataPath + "/Resources/Textures/", "*.jpg" ,SearchOption.AllDirectories);
worked like charm
Answer by Bunny83 · Feb 10, 2013 at 03:09 PM
The easiest way is to use a recursive function. However you can also use a iterative loop, but in this case you have to use your own Stack / List / Queue.
//C# - recursive (untested)
public List<TextureC> texturec ;
void ProcessDirectory(string aDir)
{
var files = Directory.GetFiles(aDir);
foreach(var fileName in files)
{
texturec.Add(new TextureC(fileName));
}
var subDirs = Directory.GetDirectories(aDir);
foreach(var subDir in subDirs)
{
ProcessDirectory(subDir);
}
}
void ResizeTextures()
{
texturec = new List<TextureC>();
ProcessDirectory(Application.dataPath + "/Resources/Textures/");
}
I'm not sure if GetFiles and GetDirectories will return the full path or not, but the basice function should be the same.
You can pass SearchOption.AllDirectories
to Directory.GetFiles
and it will recurse all subfolders for you.
Answer by exvalid · Jul 15, 2018 at 02:06 PM
i did it like this
when searching for unity files
GamesFound = Directory.GetFiles(LocationComp, "*.unity",SearchOption.AllDirectories);
Your answer
![](https://koobas.hobune.stream/wayback/20220613092448im_/https://answers.unity.com/themes/thub/images/avi.jpg)