I am trying to run a script that will run every 5 minutes in a shared hostings wordpress folder that will rename the newest CSV file in that folder.
/wp-content/csv/sample.csv
I tried putting a js file in the folder within that folder and run it.
var fs = require('fs');
function runClear()
{
fs.readdir("", (err, files) => {
files.forEach(file => {
console.log(file);
});
})
}
runClear();
setInterval(runClear, 300*1000);
However, it seems like I got client side and server side scripting confused. It seems like I need node.js.
What would be the best approach for this?
Regards,
Yes you are right you are confused in client side and server side script.
Javascript is a client side script which deal with all the user interactions like what will happen user click something or submit a form, hover over some element, scroll the web page etc.
Where as server side script like php deals with data stored on server like mysql records or the physical files.
what you are trying to do is to change the server resource from client side script. and you can not do that directly.
Instead you can call an ajax function which send an HTTP request to some script placed on server. And in that server script write the code to read the existing files in a directory and rename them using file handling operations.
Related
I am trying to read a project file that will be updated(occasionally) by me and my website will read it dynamically upon the loading of the page, will i have to build a server for this or can javascript handle it?
If so, could you share code that could achieve this.
Thanks in advance!!!
The problem seems to be how can one:
read a plaintext file stored on the website's local directory
use the contents of the file to modify a web page's content
This could be handled with JavaScript on the server side, but only if the server is configured to run JavaScript (like a node.js server).
Since the original question reads "will i have to build a server for this", it's probably likely that this is a vanilla server like Apache.
Most Apache servers are configured to run PHP code, so PHP is probably the simplest solution for grabbing the plaintext file.
Code below will load the contents of a plaintext file on the server into a JavaScript string, which then can be acted upon in the browser with JavaScript.
This code would go inside a <script> element in the HTML file:
function actUponProjectFile () {
// read project file on server using PHP, and store into JavaScript
// constant `projectFileString`
const projectFileString = `<?php
$pfText = file_get_contents('./my_project_file.txt');
echo $pfText; ?>`;
// insert code to act upon the `projectFileString` here
}
// run `actUponProjectFile()` after the window loads
window.onload = () => {
actUponProjectFile();
}
Save your data in a Javascript file and include it in your html
// data.js
const data = [
{
type: 'airtime',
price: 20,
size: '5GB',
},
{
type: 'electricity',
price: 10,
size: '40mw',
},
];
Then include the file in your html file
<script type="text/javascript" src="data.js"></script>
You can use nginx to share static files and use fetch request in js to get this file from server. If you have only static files you don't need to use server, nginx will be enough for you.
I have a csv file and I want to open it (in excel) with plain javascript.
I've searched a lot of websites and none of them seem to have the answer.
ANSWER
const { exec } = require('child_process');
exec('start ./csv/Fixture.csv', (err, stdout, stderr) => {});
You will need to use node.js or some sort of server to deliver the payload or the csv file to a JavaScript buffer such as via a websocket. Client side browser JS is a sandbox and you need to get your data from the server, or use node.js server to write javascript that runs on the server and can access files, but I think you mean you want to access a csv file on a webpage, and to do that you would need to have server side code send it to you, by using a XHR or WebSocket transfer.
Use the new HTML anchor tag attribute, for example:
<a href="abc.csv" download> Click Here... </a>
I am creating a local web application where I want to be able to clear the content of my directory.
How can I remove the files from a directory without deleting the directory itself?
I need it to be empty.
Is it better to remove the whole dir and create it again?
If javascript can delete directory contents, that would a pretty big security concern. Javascript by itself cannot delete system contents. You can make an AJAX call to the server which in turn can delete the files
For deleting the files of your local system you need a server-side scripting language. Vanilla Js cannot do that for you as it is a client-side scripting language.
If you are using Nodejs, as it is a server side you can definitely do that using unlink() for deleting file asynchronously or use unlinkSync() for deleting file synchronously you can learn about how to use that function here...
Demo code unlinkSync() -
const fs = require('fs');
let filename = "D:\\temp\\temp.zip";
fs.unlinkSync(filename);
I am trying to make a JS program using a text file on the client side. The thing is that it was difficult to find out how to do such a basic functionality because it is not a desired feature. I want to open a text file which is not in the local directory of a user but in the directory with the html file. So, what I want is the JS version of this code (which is in python)
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n");
# Close opend file
fo.close()
Thanks!
You can read a file with FileReader() (as a result of a user selecting files) but fortunately we can't modify client's or server's files with JavaScript in client side (there is no FileWriter()). To do that, you should upload the file to the server (through Ajax or a simple html form) and modify it with server side code.
Is there a way that i can get all the files and directories on the server using js?
Lets say there is a folder on the server called Files, inside the files folder, there are other folders and files but not a set value and can change constantly. Is there a way to scan the Files folder?
you can achieve this wiht a server side script, for example PHP
http://php.net/manual/en/function.scandir.php
this function returns the dirs, you could nested to get all the dirs and files in directories
then you can return to the javasript with
<? echo json_ecnode($array_with_dirs);?>
with an ajax Request
If you are asking about client-side JavaScript only, no, you cannot do that. You can read URLs using Ajax. If a URL corresponds to a file, you've read the file. If a URL corresponds to a directory and the server responds server-generated index, you could parse that index and recursively read files and indices. The "crawler" programs used by, e.g. Google, employ a similar technique, but do not depend on server-generated indices; they just follow links.
If the files you ask about are not accessible to the web server program, i.e. outside the server's document root, then you cannot read them using only client-side code.