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 ForgeStudios · Sep 12, 2013 at 02:57 AM · javascriptreading dataexternal files

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.

Comment
Add comment · Show 2
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 vexe · Sep 12, 2013 at 03:40 AM 3
Share

I think it's the same in JS, but in C# all files and directory related stuff are located in System.IO - So try to import System.IO; - And then you should have access to the Directory and File classes, you can then do stuff like Directory.GetDirectories("path"); or Directory.CreateDirectory("C:\Games"); - etc. Let me know if you got a hold of those classes and I'll make a more detailed answer.

avatar image ShadoX · Sep 12, 2013 at 07:54 AM 2
Share

http://answers.unity3d.com/questions/16433/get-list-of-all-files-in-a-directory.html ?

1 Reply

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

Answer by vexe · Sep 12, 2013 at 10:14 AM

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? :)

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

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 ForgeStudios · Sep 12, 2013 at 10:20 AM 1
Share

Fantastic! Thank you very much!

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

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

Related Questions

Importing from an external .txt file 2 Answers

Parsing a float from a string with text 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

What is wrong with this script? 2 Answers


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