'Need to run bat file through onclick using node
I'm trying to create what I think should be a very simple thing. I plan to use something like nexe to eventually turn this into an exe file. All I need it to do is open a page I built, that has button, and when you push that button it runs a .bat file I have stored locally.
This is all running on a virtual machine so i'm not worried about security.
So I need it to open a command prompt, navigate to the location of the bat file, and then open it. I just have a skeleton. Sorry for the dumb question- please help me learn.
<body>
<div id="backDiv">
<button onClick="execute" id="button">RUN THAT BAT</button>
</div>
<script type="text/javascript">
function execute(){
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c', 'fileiwanttorun.bat']);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stderr.on('data', (data) => {
console.error(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
}
</script>
</body>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
