MV Check if a file exist

Hi there~
Is it possible to check if a file exist in a target folder through a script?

Ie. Check if "Readme.txt" exists in a given file path (idk... C:/Users/Documents....)

If possible I need the script to work inside a normal Conditional Branch (I'm much more familiar with CBs than script calls, so it would be easier to use for me)

Thanks to anyone who wants to try to answer^^
require('fs').existsSync(path) would probably work.
Here's a function I made for one of my scripts, it will search if a file exists inside www folder
JavaScript:
function fileExists(file, extension = 'json'){
    let fs = require('fs');
    let absolutePath = process.cwd();
    if(!absolutePath.includes('www')) absolutePath = absolutePath + '\\www';
    absolutePath = absolutePath + '\\' + file + '.' + extension;
    return fs.existsSync(absolutePath);
}

Remove the line that checks for www folder if you want to check in the folder where the game executes, but do note that when play testing it will default to inside of www
require('fs').existsSync(path) would probably work.
This seems to work only if the file is placed in the game folder or in one of its sub folders, like:

require('fs').existsSync('sample.txt')
require('fs').existsSync('img/sample.txt')

but it doesn't work if I do something like:

require('fs').existsSync('C:/users/asus/documents/sample.txt')

I was wondering if it is possible to search/check anywhere on the pc by specifying a path.
I was wondering if it is possible to search/check anywhere on the pc by specifying a path.
...there's literally a post immediately above your reply that gives you instructions on how to do that.
...there's literally a post immediately above your reply that gives you instructions on how to do that.
Oh, I'm sorry.
I don't understand that really well then.
I thought that with that script I can only check in www folder OR in the folder where game executes if I remove that line.

But what if I need to search in a folder that isn't www or where the game runs?
Your syntax is just off. You're typing it like a Windows path (but with the wrong slashes). However, JavaScript is a Web-oriented language, and not a DOS/Windows-derived one.

Try file://C:/users/asus/documents/sample.txt

However, note that it will only work on your computer, of course...I'm not sure why you need to have the file there instead of your game directory, but give it a go.
Your syntax is just off. You're typing it like a Windows path (but with the wrong slashes). However, JavaScript is a Web-oriented language, and not a DOS/Windows-derived one.

Try file://C:/users/asus/documents/sample.txt

However, note that it will only work on your computer, of course...I'm not sure why you need to have the file there instead of your game directory, but give it a go.
I did some more researches and it works this way for me:

require('fs').existsSync("C:/users/asus/documents/sample.txt")

I tried using " instead of ' and it worked
Thank you a lot for your suggestions^^
You shouldn't search outside of the game folder, that will bring you problems with other OSes, for example you are searching in your example in the "asus" user, but another pc will have another username. Or you may not have permissions (for permissions you could try to use %programdata% folder, but only on windows)

I tried using " instead of ' and it worked
That is happening because slashes should be double in a javascript string '//' ('c://folder//anotherFolder')
The letter assigned to the operating system partition and the user name vary from computer to computer, as each device has its own path.
{Drive letter assignment}://users/{CURRENT_USER}/documents

It would be best to use the variable environments (Node.js):
process.env

Documents:
JavaScript:
process.env.USER_MYDOCS

In game:
JavaScript:
require('fs').existsSync(process.env.USER_MYDOCS+"\\sample.txt")
JavaScript:
require('fs').existsSync(process.env.USER_MYDOCS+"/sample.txt")
Return bolean
(Always use double backslash or forwardslash)
The letter assigned to the operating system partition and the user name vary from computer to computer, as each device has its own path.
{Drive letter assignment}://users/{CURRENT_USER}/documents

It would be best to use the variable environments (Node.js):
process.env

Documents:
JavaScript:
process.env.USER_MYDOCS

In game:
JavaScript:
require('fs').existsSync(process.env.USER_MYDOCS+"\\sample.txt")
Return bolean
(Always use double backslash)
Thank you!
Before your answer I also tried to solve the user name problem with this and it seemed to work (but I don't know if this has some limits):

var os = require("os")
$gameVariables.setValue(281, os.homedir())
var phrase = $gameVariables.value(281)
var phraseArr = phrase.split("\\")
$gameVariables.setValue(288, (phraseArr[0]))
$gameVariables.setValue(289, (phraseArr[1]))
$gameVariables.setValue(290, (phraseArr[2]))

and then checking with:

require('fs').existsSync($gameVariables.value(288)+"/"+$gameVariables.value(289)+"/"+$gameVariables.value(290)+"/documents/sample.txt")

Since we are talking about this I ask one more thing: do you know if is it possible to search a file only by its name without an extension? (In this case returning true if a file in "documents" is called "sample" (no matter what it is... .txt, .jpg....))
I would have to read all the files in a folder and check with SPLIT each filename without extension, I have no way to test it now, but this example can show you the way:

Github: crubier/applyRegexToAllFilesInDirectory

Edited (2):

@Mark91 I believe this will work, after testing please give feedback here.
This ready-made command is not a function, it is focused only on finding any file named "Sample" in the game folder, but don't look in the subdirectories.

JavaScript:
var fs = require('fs');
fs.readdir(".", function(err, filenames) {
    if (err) {
        onError(err);
        return;
    }
    filenames.forEach(function(filename) {
        if(filename.split(".")[0] == "Sample"){
            console.log(filename);
        }
    });
});

List to Array:
JavaScript:
/* Before Function */
var filelist=[];

/* Function inside */
if(filename.split(".")[0] == "Sample"){
    filelist.push(filename);


}

Create a function from this and then combine it with the method I explained before to read the files in "Documents" of the current user.
I would have to read all the files in a folder and check with SPLIT each filename without extension, I have no way to test it now, but this example can show you the way:

Github: crubier/applyRegexToAllFilesInDirectory

Edited (2):

@Mark91 I believe this will work, after testing please give feedback here.
This ready-made command is not a function, it is focused only on finding any file named "Sample" in the game folder, but don't look in the subdirectories.

JavaScript:
var fs = require('fs');
fs.readdir(".", function(err, filenames) {
    if (err) {
        onError(err);
        return;
    }
    filenames.forEach(function(filename) {
        if(filename.split(".")[0] == "Sample"){
            console.log(filename);
        }
    });
});

List to Array:
JavaScript:
/* Before Function */
var filelist=[];

/* Function inside */
if(filename.split(".")[0] == "Sample"){
    filelist.push(filename);


}

Create a function from this and then combine it with the method I explained before to read the files in "Documents" of the current user.
😅 I'm sorry...
Thank you for your consideration, but you must have overestimated me a little; I've never studied javascript and I just started learning something through my project.
I can apply it only in a basic way in RPG Maker.
I tried to put the pieces together but I failed, I'll keep trying however^^

Should it be something like this?

JavaScript:
var filelist=[];
function readFiles(dirname, onFileContent, onError) {
var fs = require('fs');
fs.readdir(".", function(err, filenames) {
    if (err) {
        onError(err);
        return;
    }
    filenames.forEach(function(filename) {
        if(filename.split(".")[0] == "Sample"){
            console.log(filename);
        }
    });
});
if(filename.split(".")[0] == "Sample"){
    filelist.push(filename);
    }
}
When you have only one answer above, there is no need for "QUOTE", it was enough to just post.

the function parameters are not being used in this case and within the code you cannot use:
"Sample"
Instead, it has to be one of the variables of the function parameter.
It seems to me that you copied exactly the function of the link I gave you, it is used for form files.

I have no way to test it now, but it would be something like this:
JavaScript:
var filelist=[];
function checkFile(mydir, myfile){
   filelist=[]; /*Clear Array*/
    var fs = require('fs');
    fs.readdir(mydir, function(err, filenames) {
        if (err) {
            onError(err);
            return;
        }
        filenames.forEach(function(filename) {
            if(filename.split(".")[0] == myfile){
                filelist.push(filename);
            }
        });
    });
}

To call the function, example 1:
Code:
checkFile(".", "Sample")

To call the function, example 2:
Code:
checkFile(".\\img\\tilesets\\", "Dungeon_A1")
Or
Code:
checkFile("./img/tilesets/", "Dungeon_A1")
Result: ["Dungeon_A1.png", "Dungeon_A1.txt"]

Edited:
@Mark91 Working!

Original thread: https://forums.rpgmakerweb.com/threads/check-if-a-file-exist.136729/