Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sdgd · Feb 10, 2013 at 02:36 PM · c#foldersallfinding

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 10, 2013 at 03:11 PM 0
Share

:D totally forgot this one. However in some situations you might want to exculde certain directories.

Anyway +1

avatar image sdgd · Feb 10, 2013 at 03:46 PM 0
Share

my mistake for comments thanks

 dirPaths = Directory.GetFiles(Application.dataPath + "/Resources/Textures/", "*.jpg" ,SearchOption.AllDirectories);

worked like charm

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Feb 10, 2013 at 03:11 PM 0
Share

You can pass SearchOption.AllDirectories to Directory.GetFiles and it will recurse all subfolders for you.

avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent player to move past a certain point until all enemies are killed? 1 Answer

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

NullReference Exception for instantiate 0 Answers

Does commented code take up space after build? 1 Answer

C# GetComponent()... Not finding script problem. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges