Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
24
Question by Ony · May 01, 2010 at 06:18 PM · listfilesfolderdirectory

Get list of all files in a directory?

How can I get a list of all files in a given local directory (standalone player)?

For instance, I have a "characters" folder that goes inside my app directory, and I would like to be able to get a list of all XML (or whatever) files in that folder at runtime to parse through.

Preferably in Javascript but C# is fine.

Comment
Add comment · Show 1
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 Fattie · May 29, 2012 at 10:31 AM 1
Share

a useful further tip for the below, you can go like:
dir.GetFiles("*.png");
and hence avoid .DS_Store and other nuisance files

4 Replies

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

Answer by Eric5h5 · May 01, 2010 at 07:34 PM

Use DirectoryInfo:

 import System.IO;
 ...
 var info = new DirectoryInfo(path);
 var fileInfo = info.GetFiles();
 for (file in fileInfo) print (file);

Comment
Add comment · Show 9 · 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 Ony · May 01, 2010 at 07:44 PM 0
Share

AWESO$$anonymous$$E, thank you, that worked perfectly.

avatar image headkitgames · Jul 21, 2011 at 02:01 PM 0
Share

and in C#? :-)

avatar image Eric5h5 · Jul 21, 2011 at 04:06 PM 0
Share

@headkit: replace "`import`" with "`using`", and "`for (file`" with "`foreach (FileInfo file`".

avatar image Julien-Lynge · Oct 13, 2011 at 08:34 PM 11
Share

In C#, you can also simplify things greatly like so:

 foreach (string file in System.IO.Directory.GetFiles(path))
 { }

^ Note that this doesn't require 'using System . IO'

It's strange the Unity's $$anonymous$$ono implementation supports GetFiles but not the IEnumerable methods like EnumerateFiles.

avatar image triangle4studios Julien-Lynge · Jun 17, 2021 at 11:29 PM 2
Share

The Correct way (Calls the GetFiles() Method only once):

  string [] files = System.IO.Directory.GetFiles(path);
     foreach (string file in files)
     {
          //Do work on the files here
     }

The Incorrect way(Calls the GetFiles() Method every iteration):

 foreach (string file in System.IO.Directory.GetFiles(path))
 {
      //Do work on the files here
 }

Laziness leads to bugs.

avatar image Eric5h5 · Oct 18, 2011 at 02:18 PM 1
Share

@NOAA_Julien: You can do that in JS too, but I broke it down into steps to make it more clear. That's not necessarily simplified; you might very well want to keep the references in variables like I showed, depending on what you're doing elsewhere. It's also pretty likely you're doing other file operations, so importing the System.IO namespace avoids having to append that to every file command you do.

Show more comments
avatar image
42

Answer by headkitgames · Jul 21, 2011 at 02:02 PM

In C# you can do it with:

 using System.IO;
 DirectoryInfo dir = new DirectoryInfo(myPath);
 FileInfo[] info = dir.GetFiles("*.*");
 foreach (FileInfo f in info) 
 { ... }
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 Aggressor · Jul 19, 2015 at 05:07 PM 5
Share

If you have a specific file you want to, you can use *.extension like this

directory.GetFiles("*.asset");

avatar image Udhayarajan0504 · Aug 30, 2020 at 02:35 AM 0
Share

Does it works on Android?

avatar image
1

Answer by path4tech · Mar 13, 2012 at 11:42 AM

I found Some Simple Code For this, Click here http://path4tech.blogspot.in/2012/03/application-to-show-list-of-all-files.html

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 DebbyX3 · Dec 03, 2020 at 08:58 AM 0
Share

The URL changed, this is the new one: https://path4tech.blogspot.com/2012/03/application-to-show-list-of-all-files.html

avatar image
0

Answer by drudiverse · Mar 16, 2016 at 12:09 PM

ANSWER UPDAtE 2015 ERICS ANSWER IS NOT CURRENT SYNTAX>..





YES THIS IS CURRENT ANSWER YOU SAW THAT HAHA

 function Start () {
 GetFiles();

 // var filePaths : String[] = Directory.GetFiles(info);
 // for (file in filePaths) print (file);


 }

or

 function GetFiles(){

 var info :String = Application.dataPath + "/AudioWave/";
  var fileInfo =Directory.GetFiles(info);
  for (file in fileInfo) print (file);


   }

 System.IO.Path.GetFileName(fullPath)  

to get the name of only the file from string.. fullpath string.

to get the files by type do this i.e. text

var fileInfo =Directory.GetFiles(info,"*.txt");

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 robnwfoo · Jan 15, 2017 at 01:39 PM 0
Share

Application.dataPath is exactly what I was looking for. Thanks

avatar image Udhayarajan0504 · Aug 30, 2020 at 02:36 AM 0
Share

Does this method works on Android?

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

14 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 avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Loading from mobile internal storage and/or sd card 0 Answers

What is the difference between Path.GetTempPath() vs Application.temporaryCachePath 0 Answers

A way copy my extra files to the Build? 2 Answers

How to get a list of AudioClips from a directory at runtime? 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