2

i want to ask that if i want to rename a file in javascript, what can i do ? i have try a function which i see it online but cannot work.

function ChangeFileName()
{
    var fso, f;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.GetFile("FilePath/MyFile.txt");
    f.name = "MyFile.htm";
} 

i search online and it says that the ActiveXObject is only available for IE and i intended to use it on mozilla because mozilla comes with ubuntu.

beside this, is there any method that i can rename a file inside the javascript ? thanks in advance for your help .

2
  • the file you want changed, is on the client (user with browser) or on the server (servicing web server)? Commented May 16, 2012 at 12:47
  • I don't think you can. Javascript in browsers really isn't meant to deal with the local operating system, and certainly not in a cross-platform manner. Commented May 16, 2012 at 12:48

3 Answers 3

2

It is Javascript (in the browser), right?

If you run in the browser it is not allowed for security reasons. I think there is some way to do this using IE and ActiveX but using Pure Javascript I think it is not possible.

But you can do in JScript in the console, for example to delete a single file:

function MoveFile2Desktop(filespec)
{
   var fso;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   fso.MoveFile(filespec, "newname");
}
Sign up to request clarification or add additional context in comments.

Comments

0

No, you cannot rename a file with javascript. Javascript is not able to interact with the user's computer in any way - it is only intended to be used to interact with the content of the web page it is rendered on.

Comments

0

JavaScript has no built in means to interact with a file system.

A host object might provide such a means.

The host object (window) that is available to JavaScript loaded from a web page in a typical web browser exposes no such object. Web pages are not allowed to edit the disks of people visiting their sites. (The exception is IE, with ActiveX, and some security warnings).

If you were running JavaScript in a browser extension or in a different environment (such as node.js) then it might be possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.