How to Rename File In Directory Using Javascript - javascript

I am having JavaScript file under menu directory in the root menu.js. I want to rename JavaScript file menu.js to menuOLD.js under same directory onClick.
I would have googled and found small sample as :
function renamefile(){
const myFile = new File(['hello-world'], 'my-file.txt');
const myRenamedFile = new File([myFile], 'my-file-final-1-really.txt');
console.log(myRenamedFile);
}
I have checked it's output in Console and got below output:
It's working.
But I would need to rename excatly menu.js file under menu directory.
How should I do this?

With Node.js
const fs = require('fs');
fs.rename('menu.js', 'menuOLD.js', (err) => {
console.log(err);
});
See here:

Related

How do I browse directories for json files (JavaScript)?

I want to browse directories for json files and merge all json files into one json file per directory.
My javascript skill is at beginner level.
The javascript code needs to run by calling it in the Terminal, not in the browser.
you can make use of glob npm package to retrieve all json file paths in the directories.
glob : https://www.npmjs.com/package/glob
you can simply go with :
const glob = require("glob");
const fs = require('fs-extra');
async function processJsonData(){
let parentDirectoryPath = '<specify path here>';
let data = {};
let jsonFilePaths = glob.sync(`${parentDirectoryPath}/*/*.json`);
//this will return paths for all the json files
for (const jsonFilePath of jsonFilePaths) {
let content = await fs.readJsonSync(jsonFilePath);
Object.assign(data,content);
}
await fs.writeJson(<filePath>, data, {spaces: 2});
//here specify your file path
}
ps. I am a new contributor, might have misread the question.

Creating folder,files using node.js

I'm working with node.js and looking for some examples and best practices around:
Creating a folder named test_folder
Adding a new file to that folder, named test.txt
Adding the text HI to test.txt
following is the code to create a folder and then create a file inside it with some text.
let fs = require('fs');
let dir = './test_folder'; //name of the directory/folder
if (!fs.existsSync(dir)){ //check if folder already exists
fs.mkdirSync(dir); //creating folder
}
fs.writeFile("./test_folder/test.txt", "HI", function(err) { //creating file test.txt inside test_folder with HI written on it
if(err) {
return console.log(err);
}
console.log("The file is saved!");
});

Get javascript filepath in Windows Script Host

When using .js files that are run with Windows Script Host, is it possible to get the path of the .js file that is running (which is not the same as the current working directory)? In other words, I'm wanting something similar to PowerShell's $PSScriptRoot variable. Is there any way to get this?
The reason I want this is because the working directory when these scripts are executed is not always the same as the location of the .js file itself, but I want my code to reference things relative to the location of the .js file.
You can get this with Node Js
const path = require('path'); // you can install this with npm
// __dirname = C:\dev\project
// __filename = C:\dev\project\index.js
const dir= path.basename(__dirname);
const archive= path.basename(__filename);
const fullpath = path.join(dir, archive);
console.log('Dir:', dir); // Dir: test
console.log('Archive:', archive); // Archive: index.js
console.log('Dir + Archive:', fullpath); // Dir + Archive: project\index.js

Why am I getting no such file error when using "mammoth" to convert a simple .docx file?

My code is as below. The contents of the file is a simple "hello world" I have the hello.docx file in the same folder I am calling this mammoth function.
Error result: fatal Error: ENOENT: no such file or directory, open './hello.docx'
Any idea what I am doing wrong? I am using this in my express node route
mammoth.extractRawText({path: "./hello.docx"})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
// var messages = result.messages;
})
.done();
It seems clear by the error displayed that the directory or file cannot be read. How are you uploading your file? If you are uploading your docx file using multer, you need to provide a reference to the file uploaded inside the path or buffer option as such:
mammoth.extractRawText({buffer: variable_that_holds_the_file.buffer})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
})
.done();
Else, you need to revise the path since it may not be correct.
To test this, use __dirname and or __filename in your console.log inside multer to see where your path is.
use below code it might help you. it worked for me.
const mammoth = require("mammoth");
const path = require("path");
let filePath = path.join(__dirname,'./sampledocxfile.docx');
mammoth.extractRawText({path: filePath})
.then(function(result){
var html = result.value;
var messages = result.messages;
console.log(html);
})
.done();

WinJs getFileAsync without stating a folder first

I have a string to the path of a file, and i would like to get the file. So far i have only found this, which relies on the file being within a specified folder, or a child of this folder.
var rootFolder = Windows.Storage.ApplicationData.current.localFolder;
rootFolder.getFileAsync('MY_FILE_PATH_FROM_ROOT').then(function (file) {
});
By rootFolder points to a folder where the app is installed, something like C:\Program Files....
What if i have a filePath string of something like C:\MyFiles\Picture\Pic_123.png. How would i get this file?
Sorry i am new to WinJs.
Kind of simple once you see the API. Use
Windows.Storage.StorageFile.getFileFromPathAsync('YOUR_FILE_PATH')
.then(function (file) {
});

Categories

Resources