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);
Related
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.
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.
I am developing WinJs app and I want to create several files in my app installed location in order to navigate to them locally. When I am trying to create new file I am getting Access denied exception :
"WinRTError: Access is denied"
This is the code which I'm using for file creation:
var folder = Windows.ApplicationModel.Package.current.installedLocation;
folder.createFileAsync("index.html", Windows.Storage.CreationCollisionOption.replaceExisting)
Is there a way to allow this functionality, or is it just blocked for security reasons and there's nothing that you can do about it?
Craeting those files in local folder cause another issue that I want to prevent - that's why I am trying to create them in installed location.
Thanks
You can't do that ! this folder is a read-only.
But ... (if you have to do this)
You can write anithing in the localFolder
Windows.ApplicationModel.Package.current.LocalFolder
And write a lot of code to read this files that you created, to load them dynamically in your app
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.
How can I check to see if a file is already open by another user in javascript? As it is right now, the program I'm trying to fix will open/edit a file then fail on trying to save if the file is already in use.
Also, is there an easy way to add a lock on the file so another process knows it's in use?
Edit: the program is a .hta using Active X Objects.
i guess i should have been more specific, here's some code about how it is opening/editing/saving the files.
var FileSystem = new ActiveXObject( "Scripting.FileSystemObject" );
var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
var fFile = FileSystem.GetFile( strPath );
xmlDoc.load( fFile.Path );
// some method's to edit documentElement in xmlDoc...
xmlDoc.save( fFile.Path );
Are you sure it's just JavaScript and not a combo of maybe an ActiveX or flash component? Is the file on the client or server? If server, this question makes more sense to me (ie. using some AJAX solution).
I'm not too familiar with ActiveX, but maybe when you open a file you could create a temporary file like file.ext.lock (and delete it when you save the file), so when another user tries to open the same file and sees the .lock file exists, you know it's being used.
You would probably need a server side locking feature. The javascript would call the server's 'save' script, which would return either a 'successful' status, or 'file locked'.
The simplest lock method that most programs use is creating another file with the same name but an extension such as '.lock'. A process checks if the file exists when opening the original, if so the file is in use and can only be opened as read only. If not, the lock file is created and the original can be edited.
will open/edit a file then fail on trying to save.
Javascript cannot open files or save them.
That may be your problem.
It could "edit" them - you can use JS to manipulate or edit an HTML page. [Even running a whole Rich Text Editor.]
But you then have to pass the page back to some other script to actually save those changes.
This is actually not true if you have Aptana or similar server side Javascript, or if it is being used [mozdev] to pass data to SQLite which can save its own data. If this is your case you should specify, as it is hardly typical Javascript usage.