- Home /
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.
a useful further tip for the below, you can go like:
dir.GetFiles("*.png");
and hence avoid .DS_Store and other nuisance files
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);
@headkit: replace "`import`" with "`using`", and "`for (file`" with "`foreach (FileInfo file`".
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.
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.
@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.
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)
{ ... }
If you have a specific file you want to, you can use *.extension like this
directory.GetFiles("*.asset");
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
The URL changed, this is the new one: https://path4tech.blogspot.com/2012/03/application-to-show-list-of-all-files.html
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");
Application.dataPath is exactly what I was looking for. Thanks
Your answer
Follow this Question
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