View Full Version : Read from A Flat Text Database

10-09-2003, 06:25 PM
I have a n application that reads and writes text files to a network drive on an intranet. I currently pull all of my info from multiple text files. (each record is it's own file). I'm not allowed to use any server side scripting access DB, SQL basically anything you would normally use for a DB is not allowed. The real question I had here is in regards to flat-text databases. I see I can probably do the same thing I am doing with one file rather than (getting on a 1000 now) multiple files. I've found lots of tutorials on using flat-text with PHP and ASP but none for Active-X and Javascript, probably because it is a bit unorthodox, but does any one know how I can pull my records in this manner?

I just don't know how to get it to display the line in question. How do I tell it to grab the line where the ID is 3. Once I get that down I can pretty much take it from there.

Here is the code I have for my DB:

1|Bob Makie|11665|11-9-03|6:00am-7:00am|open|open|open|open|n|n
2|Shift Open|open|11-10-03|10:00am-11:00am|open|open|open|open|n|n
3|Shift Open|open|11-10-03|12:00pm-1:00pm|open|open|open|open|n|n
4|Shift Open|open|11-11-03|2:00pm-3:00pm|open|open|open|open|n|n

Any help is much appreciated.

10-09-2003, 06:34 PM
i'm not sure it's even doable. generally, the rule is no file i/o with javascript. with the addition of ActiveX, it might be possible, but i've never seen it done.

Roy Sinclair
10-09-2003, 09:19 PM
To get that flat text file into Javascript you'd have to load it into the page somehow. Using a hidden iframe you could load it and then using javascript read through each line of text, parse each line into it's individual fields and then store them into an array or arrays. The basic problems with this idea is that as the file grows larger the responsiveness of your web page will go down since more data has to be transferred to the client which will be really bad for dialup users.
Another problem stems from the fact that you can't update the text file from the web page either. So any use of that data would be read-only.

10-09-2003, 09:20 PM
As I understood, you are not allowed to use databases (even Access?).

If I misunderstood you, this works:

var conn = new ActiveXObject("ADODB.Connection");
var connString = "dsnTest";
var rs = conn.Execute("SELECT Name FROM Test WHERE ID=" + 3 + "");
if (!rs.EOF){
name = rs("Name").Value;
} else {
alert("User not found.");

Otherwise you should be able to use the FileSystemObject. I don't have it installed on my computer, so I can't test it. I have only used it in JScript in ASP.
Something like this should do it in that case:

function readFile(pFile, filterField, filterValue) {
var str = "";
var i = 0;
var fieldNames = [];
var records = [];
var tempFields = [];
var filterFieldNr = -1;
while (!pFile.AtEndOfStream){
str = pFile.ReadLine() + "\n";
if (i == 0) {
for (var j=0;j<fieldNames.length;j++) {
if (fieldNames[j] == filterField) {
filterFieldNr = j;
if (records[filterFieldNr] == filterValue) {
return records;

var fso = new ActiveXObject("Scripting.FileSystemObject");
var datafile = fso.OpentextFile("db.txt", 1,false,0);
var rs = readFile( datafile, "ID", 3 );

10-09-2003, 10:03 PM
That is just what I was looking for Danne. Thanks a million!

10-09-2003, 10:15 PM
No problem. Just remeber that the object(s) needs to be installed on all clientcomputers.

10-09-2003, 11:12 PM
They are, currently I am using pretty much the same method just from multiple files. Seems as though this will be a nice work around.

10-10-2003, 02:54 AM
OK, I have another question in regards to this. Looks like I am going to have this working off the single file. In order to update a record in the file I am basically reading the whole file parsing it into an array updating the array elements based on the user's input and then writing the info back into the text file, overwriting it. What effect will it have if I have multiple users doing things at the same time? Could one person overwrite another persons entry if the timing was right? Again this is all working off a network drive on an intranet using active-x and javascript, entirely client side. Did I make this clear please let me know if it is not.


10-10-2003, 08:33 AM
yes, that set up could easily lead to problems with overwriting, entries. that's why this sort of thing isn't normally done with javascript; it's not really designed for this

10-10-2003, 03:25 PM
Hmm, yes if you want to write to the file it becomes complex with multiusers.

One solution would be to have a global varible indicating if another user has changed the file and in that case prevent the user from saving it. Like a lastChangeTime-variable. Since the program runs on each client, you can't really use a global var, but you can put it in a file. So everytime the user tries to save you need to check that variable.

If it has been changed since the user read it you need to decide what to do. If you should allow the user to overwrite another users changes, or if the user should lose his/hers changes. Or try to update only the records that were changed.

To do so:
In the client you could keep track of which records have been changed using a unique id. That way it's possible to read the file again and avoid losing data by not replacing the users changed records - only the others.

There's still a problem of overwriting if the same record was changed.

Maybe you got some ideas anyway...

10-10-2003, 07:34 PM
Thanks for the reply loos like I'll have to re-think this a bit.

10-11-2003, 04:56 AM
Does your intranet use IE 4.0+? If so, this (http://javascriptkit.com/javatutors/tdc.shtml) might be of help.

Hope that helps!

Happy coding! :)

10-11-2003, 07:54 PM
nolachrymose you are my new best friend. I found a site outlining that a while back and couldn't find it again. That is EXACTLY the information I was looking for.

10-11-2003, 08:04 PM
Hehe...glad to help. :o