Unanswered question

How to rename a local file with javascrpt

using JavaScript, how to rename a file in my local drive like C:\Users\Public\TEMP\FileName.csv

I tried to use bat file with below script, its not working.

java.lang.Runtime.getRuntime().exec("C:\\Users\\Public\\TEMP\\WEX\\rename.bat")

venkata S.
venkata S.

venkata S.

Level
0
47 / 100
points

Answers

Nouredine A.
Nouredine A.

Nouredine A.

Level
4
5000 / 5000
points
Team

I'm not a JAVA or Javascript specialist but by googling a bit i found some examples of JAVA code to move a file to a different name using the code below:

var fileToCopy = new Packages.java.io.File("C:/Users/foo/AppData/Local/Temp/changeit.txt");
var NewName = new Packages.java.io.File("C:/Users/foo/AppData/Local/Temp/changeit2.txt");

if (NewName.exists())
   throw new java.io.IOException("file exists");

// Rename file (or directory)
var success =  fileToCopy.renameTo(NewName);

if (!success) {
   // File was not successfully renamed
}

I tried it and it worked fine.