0

I am creating a project that my node project can open a notepad.exe

const openyeah = "notepad.exe";
const fs = require("fs");

fs.open(openyeah,"r",(err,fd)=>{
    if(err){
        console.log('errors')
    }else{
        console.log("correct")
    }
})
Allen Cris
  • 109
  • 3
  • 11
  • Hope this link will solve your problem https://stackoverflow.com/questions/19762350/execute-an-exe-file-using-node-js – Gowtham Raj Mar 13 '19 at 09:22

2 Answers2

6

You need to use the Child Process module to get this done. The child_process module provides the ability to spawn child processes which enable us to open window programs like notepad,exe

If you look at the below example once we create a spawnObj, we can pass the pass the program name which needs to be executed as a first argument (in our case the notepad.exe) and the relevant input as the second input (in our case the .txt file name. Please check and replace the C:/Users/YOUR_USER_NAME/Desktop/somefile.txt in the below example with a valid path/filename in your PC).

var spawnObj = require('child_process').spawn,
progToOpen = spawnObj('C:\\windows\\notepad.exe', ["C:/Users/YOUR_USER_NAME/Desktop/somefile.txt"]);

Hope this helps!

David R
  • 14,711
  • 7
  • 54
  • 72
  • this is just my example question bro but do you have any idea about running ccleaner auto clean using the same method? – Allen Cris Mar 13 '19 at 09:30
  • In that case you will have to omit the `["C:/Users/YOUR_USER_NAME/Desktop/somefile.txt"]` argument here `progToOpen = spawnObj('C:\\windows\\notepad.exe', ["C:/Users/YOUR_USER_NAME/Desktop/somefile.txt"]);` and also instead of `notepad.exe` you need to call the `ccleaner.exe` with its relevant path. – David R Mar 13 '19 at 09:33
  • I dont know about the array part bro? because there is no other file to open – Allen Cris Mar 13 '19 at 09:43
  • var spawnObj = require('child_process').spawn, progToOpen = spawnObj('C:\\windows\\CCleaner64.exe', ["C:\Program Files\CCleaner>ccleaner.exe/auto"]); – Allen Cris Mar 13 '19 at 09:48
  • 1
    This would do => `var spawnObj = require('child_process').spawn, progToOpen = spawnObj('C:\\windows\\CCleaner64.exe'); ` – David R Mar 13 '19 at 09:58
  • error bro what is with that windows? is it really necessary to put ? – Allen Cris Mar 13 '19 at 10:03
  • May I know where exactly the `CCleaner64.exe` is located in your PC? – David R Mar 13 '19 at 10:11
  • C:\Program Files\CCleaner – Allen Cris Mar 13 '19 at 10:13
  • I can open now but It should auto clean when open this is the only syntax – Allen Cris Mar 13 '19 at 10:16
  • C:\Program Files\CCleaner>ccleaner.exe/auto' – Allen Cris Mar 13 '19 at 10:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189942/discussion-between-david-r-and-allen-cris). – David R Mar 13 '19 at 10:25
0

fs.open is for reading and writing to file content, but starting program is not "open" but "invoke" a (executable) file.

igonejack
  • 2,366
  • 20
  • 29