• 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
Question by Lemo76 · May 13, 2014 at 09:34 AM · checkdirectorysystem.io

Check if file in directory exists (JS)

I want to be able to check if a file in a directory exists but the System.IO.File.Exists only really lets me check if the file in the base directory exists, not in a specific folder.

Here's my code:

 var MusicFolder = "Game Menu";
 
 function Start () {
 
 if (System.IO.Directory.Exists(MusicFolder)) {
 } else {
 System.IO.Directory.CreateDirectory(MusicFolder);
 }
 
 
 if (System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + "\\" + MusicFolder, "MenuAmbience01.wav"))
 {
 print ("exists");
 }

I'm sure there is an easy fix w$$anonymous$$ch I'm missing but it would be great if you could help me. -Thanks

Comment

People who like this

0 Show 5
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 Benproductions1 · May 13, 2014 at 09:56 AM 1
Share

Just use the actual path to the file, rather than whatever else you were trying to do.

avatar image maddFrogg · May 13, 2014 at 09:59 AM 1
Share

Not sure about JS, does the Exists method take 2 arguments?

Check your double slash thing, are you sure you need 2 slashes? Try it also with this one '/'

avatar image Lemo76 · May 13, 2014 at 10:04 AM 0
Share

The error it gives is: The best overload method 'System.IO.File.Exists(String)' is not compatible with the argument list '(String, String)'

I can't just use the actual path to the file as I prefer it to be portable so it calls up on its own directory rather than a directory path it creates in the hard drive.

avatar image flamy · May 13, 2014 at 10:08 AM 0
Share

print this in debug and check System.IO.Directory.GetCurrentDirectory() + "\\" + MusicFolder, "MenuAmbience01.wav"

if it is the correct path

wat is the value of musicfolder varaiable

avatar image Lemo76 · May 13, 2014 at 10:14 AM 0
Share

I went:

print(System.IO.Directory.GetCurrentDirectory() + "\\" + MusicFolder);

and it is the correct folder

 J:\13 DESIGNED GAMES $$$$$$$$$$$$$$$\FULL GAMES\AMBIENTGAME\Game Menu 
 UnityEngine.MonoBehaviour:print(Object) SubMenuAmbience:Start() (at Assets/SCRIPTS/SubMenuAmbience.js:20)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · May 13, 2014 at 10:25 AM

Like others already said Exists only takes one parameter w$$anonymous$$ch is the full path to your file. You should either combine your path manually or use System.IO.Path.Combine to not have to worry about slashes. To simplify the access to the IO methods you might want to add t$$anonymous$$s at the top:

 import System.IO;

 function Start()
 {
     // [...]
     var fileName = "MenuAmbience01.wav";
     var path = Path.Combine(Directory.GetCurrentDirectory(), MusicFolder);
     path = Path.Combine(path, fileName);
     if (File.Exists(path))
     {
         print ("exists");
     }
 }

Or combine it yourself but keep in mind to watch the slashes / backslashes

 import System.IO;

 function Start()
 {
     // [...]
     var fileName = "MenuAmbience01.wav";
     var path = Directory.GetCurrentDirectory() + "\\" + MusicFolder + "\\" + fileName;
     if (File.Exists(path))
     {
         print ("exists");
     }
 }

GetCurrentDirectory usually returns a path without trailing backslash. However there are some situations where it does have one, for example in the case of "C:\". It's more save to use Path.Combine.

Comment
Lemo76
flamy

People who like this

2 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 Lemo76 · May 13, 2014 at 10:29 AM 0
Share

So glad it works now, thanks all of you for helping me out!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Using Directory.Move to rename a folder is leaving the old folder behind 1 Answer

Read and write from IOS to Mac 0 Answers

JS-Complicated Directory Array Texture Method 0 Answers

How do you check if a directory exists c#? 2 Answers

Return Folder Name In String 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