Getting file names from a remote directory

Is there a way in Javascript where I can return the contents of a directory (in any form?). I know that fileUtil is for handling files in directories, but before I go there I just want to return all the names of the files/folders from somewhere like C:\Games.

OK, I just checked out the JS syntax, it’s almost exactly the same.

Everything you need about files and directories is located inside the System.IO namespace.
So if you’re in JS you have to import System.IO; C#: using System.IO;

To get all files in a directory, use string [] GetFiles(string path) which is located inside the Directory class.

var Desktop = "path to your desktop";
var wanted = "secret.txt";
for (var _file in Directory.GetFiles(Desktop)) 
{
	if (_file.Contains(wanted)) // we can't say if(_file == wanted) cuz '_file' is a full path, not just the name of the file.
	{
		File.Delete(_file);
		break; //* found the file, no need to loop anymore, assuming that there's only one copy of the file in that directory.
	}
}

To iterate over directories, there’s a similar method Directory.GetDirectories(string path); (has more overloads)

I have made a summary, of the most useful things you could get out of the File, Directory and Path classes (For basic usage) - Note that there could be more than one overload for some of the methods, for please refer to the official documentation. Again, this is a simple usage list.

 __________________________________________________________________________________________
(1)
 * -----DIRECTORY CLASS------
 * 1-string [] GetFiles(string path);
 * 2-string [] GetDirectories(string path);
 * 3-string [] GetLogicalDrives();
 * 4-Time Getters Methods. (GetCreationTime, GetLastAccessTime, etc)
 * 5-DirectoryInfo GetParent(string path);
 * 6-DirectoryInfo CreateDirectory(string path);
 * 7-void Move(string SourcePath, string DestinationPath);
 * 8-void Delete(string path, bool Recursive);
  __________________________________________________________________________________________
 
 (2)
 * -----FILE CLASS-----
 * 1-void Delete(string path)
 * 2-void Copy(string SourcePath, string DestinationPath)
 * 3-void Move(string SourcePath, string DestinationPath)
 * 4-bool Exists(string path)
 * 5-FileStream Create(string path)
  __________________________________________________________________________________________
 
 (3)
 * -----PATH CLASS-----
 * 1-string GetDirectoryName(string path)
 * 2-string GetFullPath(string path)                   Location+Name+Extension.
 * 3-string GetFileName(string path)                   Name+Extension.
 * 4-string GetFileNameWithoutExtension(string path)   Name.
 __________________________________________________________________________________________

There are some methods that don’t exist in Unity, although from this, it says that Unity uses .NET 4.0 - Which means stuff like Path.GetFileExtension and Directory.EnumerateFiles should be available, but I just checked it, it’s not. So in Unity, you can’t access everything .NET 4.0 has to offer.

But GetFileExtension isn’t something that difficult to write, right? :slight_smile:

C# SYNTAX:

string GetFileExtension(string path)
{
   return path.Substring(path.LastIndexOf('.'));
}

My poor JS attempt:

function GetFileExtension (path: String): String
{
    return path.Substring(path.LastIndexOf('.'));
}

If you’re on C#, It would be nicer if you made this an extension to the path class.

For more info, check my old answer on Directory creation.
For MUCH more info, like what’s a DirectoryInfo, FileInfo, DriveInfo or any other thing related to the 3 classes I mentioned above, please check out this old tutorial I made on the subject where I cover a lot of ground in depth.

Hope I helped :slight_smile: