I cant figure out how to run a .bat file onclick of a button or input. Ive tried several different configurations.
The batch file is in the root directory of the nwjs application. Then once the batch is executed ill do various task on the system via CMD
<button id="run-button">Run Batch File</button>
const child_process = require('child_process');
// Define the path to the batch file
const batPath = 'F:/file.bat';
// Add an event listener to the button
document.getElementById('run-button').addEventListener('click', function() {
// Spawn a child process to run the batch file
const bat = child_process.spawn(batPath);
// Listen for any data from the child process
bat.stdout.on('data', function(data) {
// Print the data to the console
console.log(data.toString());
});
// Listen for any errors from the child process
bat.stderr.on('error', function(error) {
// Print the error to the console
console.error(error);
});
// Listen for the exit event of the child process
bat.on('exit', function(code) {
// Print the exit code to the console
console.log('Child process exited with code ' + code);
});
});
If this doesn't work,
file.batisn't in the current working directory. You can tweak thepath.resolve()line to fix this (possibly usingnw.App.startPath).