0

I have a file stored in a folder name testfolder my local disk.

The file text would be something like the below

Apples are red. <colour = red/> latitude 13.124165

Is it possible to get the content of this file in to a variable and then do some adjustment and overwite it?

fopen("C:\testfolder\test.text", 0);
 var cont = ``; //get content in to a variable?
 //update cont
fwrite(file, cont);
Becky
  • 5,467
  • 9
  • 40
  • 73
  • That's PHP, not JavaScript. – Adam Azad Sep 16 '16 at 07:48
  • @Tushar Thanks. That was something I found while googling. http://www.careerride.com/JScript-read-and-write-file.aspx I'm wondering if this is possible in js? – Becky Sep 16 '16 at 07:50
  • @Tushar No, I am not using NodeJS. – Becky Sep 16 '16 at 07:52
  • 1
    You should explain more about your case, for example, the javascript is running in a page on a remote web server, or is just open locally on your pc? Do you want the script to open and read this file automatically when you load the page? – Mario Santini Sep 16 '16 at 07:56
  • @MarioAlexandroSantini Thanks. This is only to be used locally on my PC. Yes, I want the script to open and read this file automatically when the page opens. – Becky Sep 16 '16 at 08:00
  • @MarioAlexandroSantini So basically I need to open a file from my local disk and to get the content of this file in to a variable and then to overwrite it. As I mentioned in my previous comment this is only to be used locally on my PC. – Becky Sep 16 '16 at 08:04
  • Possible duplicate of [Javascript - read local text file](http://stackoverflow.com/questions/14446447/javascript-read-local-text-file) – Mario Santini Sep 16 '16 at 08:12

2 Answers2

0

Is it possible to get the content of this file in to a variable and then do some adjustment and overwite it?

Short answer: Yes.

Long answer: Yes. I understand you mean synchronously. For example in node you can read and write files synchronously with fs.readFileSync() and fs.writeFileSync(), respectively.

Simple example:

user@host:~$ echo "Hello foo" > file.txt                                                               │
user@host:~$ node                                                                                      │
> var fs = require("fs");                                                                                 │
undefined                                                                                                 │
> var str = String(fs.readFileSync("file.txt"));                                                          │
undefined                                                                                                 │
> str = str.replace("foo", "bar");                                                                        │
'Hello bar\n'                                                                                             │
> fs.writeFileSync("file.txt", str);                                                                      │
undefined                                                                                                 │
> process.exit(); // (Or simply type ctrl+d)                                                              │
user@host:~$ cat file.txt                                                                              │
Hello bar
bitifet
  • 3,514
  • 15
  • 37
0

If you want to read a file from the server, then you need to send an Ajax request from the client, and listen to it on the server.

However, if you want to read a file from the user's computer, then that's (for obvious reasons) not possible.

Bálint
  • 4,009
  • 2
  • 16
  • 27