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

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.