1

I'm trying to write the simple part of code in pure java script that should just check if the file exists in some specific folder. I lost a day already trying to get it work, I've seen many similar threads, but none of them worked for me.

I'm trying to run the simple html page on my PC on the win7.

I've tried the code from this thread javascript check if file exists

    function fileExists(url)
    {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
    }

But I'm getting error in http.send()line "A network error occurred."

Also this thread doesn't help: Can't check if file on sdcard exists

    String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
    File f = new File(path);
    if (f.exists()) {
       // do your stuff
    }
    else {
      // do your stuff
    }

The line with new File(path) gives an error: "File requires at least 2 arguments, but only 1 were passed"

Well i found out the correct syntax for it, it should look something like:

File f = new File([""], path); 

It's really annoying that i found on this forum plenty times the same incorrect syntax for new File with only 1 parameter instead of 2 of them, but ok. Even when i fixed this in that way it doesn't work - the method

f.exists()

always returns the false

This is the current code that i have now:

    function MyFunc()
    {
        var myfile = new File([""], 'F:\\abc.txt');

        if (!myfile.exists) {
                alert(myfile.name + " could not be found!");
        }
        else
        {
            alert("file exists!");
        }


    }

I tried to see what is inside the myfile array with such methods:

     myfile.name 
     myfile.size
     myfile.webkitRelativePath

but seems that only first of them works (the name of the file). Other return null data. So maybe the reason is that something wrong in line var myfile = new...

Barmar
  • 741,623
  • 53
  • 500
  • 612
Alex
  • 11
  • 1
  • 2
  • 1
    JavaScript doesn't use type declarations, `String path` and `File f` are not JS. – Barmar Jul 11 '19 at 08:02
  • The Android question you linked to is using Java, not JavaScript. The names are similar, but they're nothing alike. – Barmar Jul 11 '19 at 08:04
  • so what can i use instead of them? – Alex Jul 11 '19 at 08:04
  • Are you trying to check a file on the server or the client? JavaScript in browsers has very little access to the client filesystem. – Barmar Jul 11 '19 at 08:05
  • i'm trying to check on the clients side. It's like i'm running the simple html page on my laptop and it should check if the file apeared in some folder. For example file has been downloaded or created in some specific folder. If so - it moves on. That what i want to do. I don't need to open the file and access its data, just see if it exists. – Alex Jul 11 '19 at 08:08
  • 4
    Let me clarify. JavaScript in the client has *no* access to the filesystem. – Barmar Jul 11 '19 at 08:11
  • unless the client opens some file in the menu? I thought it has been done for security purpose while you're trying to get access to the files data, but didn't think even all of the folders and files are hidden for it – Alex Jul 11 '19 at 08:19
  • Anyway you still can get access to some files that are in the same folder where the html-file or its .js file is based? Or not? For example some img files that are based in some specific folders. Can't you see the list of it and the properties like size etc. from your own code? – Alex Jul 11 '19 at 08:21
  • You can access some information about a file that's been selected from an open file dialogue, I think just the name and size, but not the folder or contents. – Barmar Jul 11 '19 at 08:28
  • You can use the [`FileReader`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) API to read from files, but I don't think you can access directories. And the user has to select the file from a dialog, you can't give a filename to the API. – Barmar Jul 11 '19 at 08:30

1 Answers1

0

You would have to execute javascript with node.js to get access to local file system. You can use the fs API https://nodejs.org/api/fs.html

You can then do something like this

var fs = require('fs');

fs.stat('path/to/file', function(err) {
    if (!err) {
        console.log('file or directory exists');
    }
    else if (err.code === 'ENOENT') {
        console.log('file or directory does not exist');
    }
});

Jørgen Vik
  • 726
  • 1
  • 8
  • 27
  • But i can't use that in browser, can i? The node.js works just like the separated module, if i need to use it in the page i develop i can't stick to the node.js? – Alex Jul 11 '19 at 09:58
  • You could build an express app in node.js, where you can serve your html-page and access it in the browser. You can not simply add node.js as a "module" to your current project. This tutorial covers how to serve an HTML-file with a simple express application https://www.youtube.com/watch?v=p5eCYKiZN-4. Source code for tutorial here https://github.com/mschwarzmueller/nodejs-basics-tutorial/blob/master/03-node-only-render-html/server.js. This will require you to use the node.js runtime to execute your js. I suggest reading up on node.js and NPM. – Jørgen Vik Jul 11 '19 at 11:42