1

What's the correct syntax to search a .txt file for a keyword in JavaScript?

EDIT: I'm working with a subset of JavaScript called UnityScript in a program called Unity3D. It outputs .exe programs. Here's an example of UnityScript:

import System.IO;

function ReadFile () {
    var sr = new StreamReader(Application.dataPath + "/" + readFilePath);
    var fileContents = sr.ReadToEnd();
    sr.Close();

    var lines = fileContents.Split("~"[0]);
    for (line in lines) {
        Debug.Log (line);
    }
}

I thought that if I could get a function from JavaScript I could import it into my program. I see now that perhaps I was wrong.

Thanks - Elliot Bonneville

derHugo
  • 83,094
  • 9
  • 75
  • 115
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • File access was traditionally not allowed in JavaScript. Where is the file hosted? Note that it is possible in *some* modern browsers with HTML5. – Anurag Jul 29 '10 at 01:01
  • Just right on my computer. I'm using a sub-set of Javascript called UnityScript, but I thought that a JS function would work too. They're very similar. Was I wrong? – Elliot Bonneville Jul 29 '10 at 01:03
  • More details please, we need to know what you are trying to do, and what you have done thus far. – Josh Stodola Jul 29 '10 at 01:14
  • See my comment downstairs. (the answer below) – Elliot Bonneville Jul 29 '10 at 01:15
  • Please update this question with the information that your JS is embedded into an exe. Can you provide specifics about how that's handled? And as I suggested on my question, it is much better idea to use the exe to handle system level stuff like file IO. – Razor Storm Jul 29 '10 at 01:15

3 Answers3

3

It depends, in modern browsers there are some ways to access locally stored files (meaning on the same machine as the user viewing your webpage). However, if the file is stored on the server side, meaning the machine hosting the website, javascript alone is not enough.

If the file is hosted on the client machine, please take a look here.

However, if the file is hosted on the server machine, you may start an AJAX request to the server, and have the server feed back the text file. (Simply printing the file to STDOUT will send it as a response to the HTTP request).

http://en.wikipedia.org/wiki/Ajax_%28programming%29 http://www.w3schools.com/Ajax/Default.Asp

After you receive the data you can use xmlhttpobject.responsetext.match("keyword") to find whether it exists.

Community
  • 1
  • 1
Razor Storm
  • 12,167
  • 20
  • 88
  • 148
  • I'm not actually working in a browser. I'm running an .exe with JS embedded. That being the case, I'm not working with AJAX. – Elliot Bonneville Jul 29 '10 at 01:08
  • Oh interesting, can you give a bit more details about how the exe is programmed? It seems like a better idea to use the exe itself to accomplish this task. C/C++/C#/perl/whatever you're using to program the exe is going to be much more fit to do this job than javascript is. – Razor Storm Jul 29 '10 at 01:09
  • I'm working with a program called Unity3D actually. It is to my program as Visual is to a C++ app. I'm kinda hacking the system as it's 3D but I'm creating an interface app. Anyways, I ended up working with a subset of Javascript called Unityscript. I thought that if I could get a function from Javascript it would work in US. – Elliot Bonneville Jul 29 '10 at 01:14
1

Javascript does not have access to the file system. Not without ActiveX plugins or Flash.

Sounds like you need a desktop application or a powershell script.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
1

Try this:

function process(url, send, RegExp) 
{ 
    with(new XMLHttpRequest) { 
        open((send) ? "POST" : "GET", url , false);
        setRequestHeader("Content-Type:","text/Plain");
        send(send);
        if(readyState == 4)
            return RegExp != null ? responseText.match(RegExp) : responseText
    }
}

example

file.txt :

name=frank&id=12&foo=a

Call the function like

process("file.txt", null, /name=([^&]+).id=(\d+)&foo=([^\n]+)/g)
derHugo
  • 83,094
  • 9
  • 75
  • 115
Jet
  • 1,283
  • 10
  • 7