How to append data to txt in javascript? - javascript

I'm writing a program, which needs to take some JSON from text file, then modify it and put it back to the same file, something like overwriting. I don't have problem with reading from file and modifying data, but I was searching long and found nothing about writing into files. If it's important - it's server-side file.
Ex. text file: {"name":"John","age":20}
Result which I need: {"name":"John","age":21}

You can use the fs module in node to write to a file!
information is located here on it https://www.w3schools.com/nodejs/nodejs_filesystem.asp
The two methods you will want to look at is .writeFile or .appendFile depending on your specific need.

Related

Get the headers from a CSV file (and only that line)

How do I read the first non-empty line from a text file, in plain javascript, possibly by using a new FileReader()?
I wish to get only the first non-empty line, not the whole text in the file (which might be enormous). By "non-empty line" I mean a line ending with \r\n and containing some non-blank char.
One of my practical goals could be to pick the first "good" line in a huge CSV file, to possibly get the "headers" (names of variables of the dataset).
The file I wish to read is now locally on my hard disk, but when the script is ready, I would also like to put it online on my domain along with the script to process it.
Thank you!
I think you want something kinda like this:
fs = require('fs')
fs.readFile('/testFile.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var firstLineRegEx = /^(.*)$/m
var csvHeader = firstLineRegEx.exec(data)
});
Assuming you want to stick to client-side/in-browser JavaScript (as opposed to node.js), there's not really a way to open a file and read it line-by-line; that would require a level of direct filesystem access that's simply not available. You can certainly process it line-by-line, once it's fully-loaded and you've split it into lines.
If you want to be sure that you only ever load the first line in the browser, you'll need some server-side code for that. Node.js may or may not be a good option, depending on your existing web site.
The FileReader API you mentioned looks like it's geared towards handling arbitrary, user-provided files, which is probably the wrong direction if you'll always be in control of what file to load.
You've actually got two, distinct problems here: how do I load only part of a file in client-side JS, and how can I find the header line in a string containing CSV formatted data? (You could probably break this down differently, but it's multiple parts regardless.)

HTML5 with javascript to parse and display textfile

I have an HTML5 page (index.html), and I need to display the contents of a .txt file (p001wide.txt). Both are on the server in the same directory.
The text file is generated from a CMS, so I cannot change the format of that file.
In the .txt file is a variable named widetxt. I need to display the contents of the widetxt variable on the page.
What do I need to do to parse the the textfile and display it in the HTML page?
Do I need to use javascript, and if so, how?
Hm, I found this question which appears to be similar to yours as far as reading a file goes. As for parsing though why not use a database such as MySQL to store your data? This way you can quickly add and query through your data.
As both your index file and the text file you wish to read reside on the server you should be able to read the text file on the server and insert what you wish from it into the index file using PHP.
See http://www.w3schools.com/php/php_file.asp for a tutorial on how that can be done without resorting to client side script.
If you cannot alter your index file on the server and can put a PHP file on the server you can use AJAX to ask your PHP file to read the contents of the text file and return it to you. Then you would use javascript to insert it as you wish. I presume that you can alter the index file because otherwise you could not alter its javascript either.
You can do this with JavaScript, you basically need to learn how to read and write files (and you mention the variable inside, this would be parsing). Start reading here - http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm other information, just Google read write files javascript, or parsing text with javascript.
Best of luck!

Organize Csv file based on filestructure using JavaScript.

Here's the whole problem:
I need to create a plain csv table from a file structure from the server.
Everyline should be organized as follows:
,
Currently, all the files on the server are organized like this:
L:\Dados\rd\20110727000002978\110614.pdf
Where the string 20110727000002978 is the docid and L:\Dados\rd\20110727000002978\110614.pdf is the file path.
So, the CSV table should have the data like this
L:\Dados\rd\20110727000002978\110614.pdf, 20110727000002978
Currently I'm using a BATCH file to do a DIR /S/B > FILELIST.TXT to list all the files, and MANUALLY updating the
Is it possible to automate this using javascript? I guess it would be a simple script, but I had no idea how to start this.
I guess I don't need to use JavaScript, as I long as I don't need any copyrighted copiler - I guess I can use anything.
As far as I know, the code itself is rather simple, but all programming I know is BATCH and a little PASCAL.
If I understand your question, you won't be able to do this entirely in JavaScript. You'll need a sever-side language, like PHP (as an example) to do the file processing.

How can I make a web page show the contents of a file even after I change the file?

Hey everyone I got a wiki question. We store files in *.7z in one server and have a wiki on another. I need to make a script that pulls the file names from one script and adds the code behind it so I can post it in the wiki. We do a ton of files a month and typing them all by hand won't cut it.
Here is what I need.
Var filename or string
For each file in the list {
Filename= File
Write ('<br><file>SERVERNAME\C$\VideosArchive\ '+Filename+ '|' +Filename+ </file> <br/>)
So when the output comes out it has to look like.
<file>\\SERVERNAME\c$\VideoArchive\FILENAME.7z|FILENAME</file>
but i need something that auto generates that if that makes since.
Something like this?
pushd \\servername\C$\VideosArchive
rem by the way, you should make that a share instead of using C$. Plays nicer with privileges and stuff
for %%f in (*) do (
echo ^<br^>^<file^>SERVERNAME\C$\VideosArchive\%%~nxf^|%%~nxf^</file^>^</br^>
rem do I need to ask why you are using "br" as if it had content? It's a tag without content ...
)
popd
Sounds like a very simple task.
You could create an app or PowerShell script to perform the dynamic creation of this file.

How to include a definition language file in javascript?

I have a big javascript file common.js
with all my functions and stuff...and i would like to write a separate file Js
to store only the definitions for all the alert/courtesy messages etc
spread along the file...i don't want to seek and change one by one...
i have created something like this in php, where I have my file, en.php / fr.php /de.php for each language i need...
i was wondering:
1. if i can do the same in Js
2. if there is any way to use the php instead...so woudl be even better to have just one and only file to edit
thanks
Sure -- all you need to do is to create a definitions file for each language that just creates an object with the needed name: value pairs. Your existing JS can then insert defsObject["name"] wherever needed. Your PHP would then determine which of the JS definition files to load by changing the src value of the script tag.
If you are unfamiliar with the object notation, there's loads of examples available on Stack Overflow (tagged JSON).
I don't believe there's a satisfying way using js to include the definitions.

Categories