- Home /
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 which I'm missing but it would be great if you could help me. -Thanks
Just use the actual path to the file, rather than whatever else you were trying to do.
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 '/'
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.
print this in debug and check System.IO.Directory.GetCurrentDirectory() + "\\" + $$anonymous$$usicFolder, "$$anonymous$$enuAmbience01.wav"
if it is the correct path
wat is the value of musicfolder varaiable
I went:
print(System.IO.Directory.GetCurrentDirectory() + "\\" + $$anonymous$$usicFolder);
and it is the correct folder
J:\13 DESIGNED GA$$anonymous$$ES $$$$$$$$$$$$$$$\FULL GA$$anonymous$$ES\A$$anonymous$$BIENTGA$$anonymous$$E\Game $$anonymous$$enu
UnityEngine.$$anonymous$$onoBehaviour:print(Object) Sub$$anonymous$$enuAmbience:Start() (at Assets/SCRIPTS/Sub$$anonymous$$enuAmbience.js:20)
Answer by Bunny83 · May 13, 2014 at 10:25 AM
Like others already said Exists only takes one parameter which 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 this 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.
So glad it works now, thanks all of you for helping me out!
Your answer
Follow this Question
Related Questions
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