Reputation: 688
There are certain file operations in Windows (7/10) I'd like to automate (ie moving all files of a certain file type in a given directory up a directory, and then deleting that directory and the rest of the files in it).
I know how to write a program in Node to do this, but rather than going into each directory and right click -> Git Bash Here -> run my script, I'd like to be able to just right click on the directory and have an option in the directory's context menu to run my Node script directly.
Is there some sort of way to do this through the Windows registry without having to write a full-blown, installed, native application?
I'm just looking for a simple, lightweight, hacky(?) way to run a small Node script from the windows directory context menu (that does simple operations on the files in that directory).
Upvotes: 3
Views: 3034
Reputation: 821
Note: index.js specified file will be executed
Run this script using nodemon:-
const express = require('express');
const app = express();
const { exec } = require('child_process');
const port = process.env.NODE_ENV === 'production' ? (process.env.PORT || 80) : 3000;
const server = app.listen(3001, function () {
console.log("running server @ 3001");
exec(`reg add "HKEY_CLASSES_ROOT\\Directory\\shell\\REFI-Created\\command" /f /d "C:\\Program Files\\nodejs\\node.exe 'C:\\Users\\Sahil_Shikalgar\\Desktop\\Export chart as a file\\Refresh All charts functionality\\index.js' '%V'"`,
(err, res) => {
console.log(res);
console.log(err);
})
});
Description:
1) Key creation path: HKEY_CLASSES_ROOT\Directory\shell\REFI-Created\command
2) Node Application: C:\Program Files\nodejs\node.exe
3) Your script file to execute: C:\Users\Sahil_Shikalgar\Desktop\Export chart as a file\Refresh All charts functionality\index.js
4) '%V' to pass selected context menu directory path.
Upvotes: 1
Reputation: 688
Figured it out.
Open the Registry Editor (Start > regedit):
Click on command, then double click on (Default), enter:
"C:\Program Files\nodejs\node.exe" "X:\PATH\TO\script.js" "%V"
When running the script, process.argv[2] will be a string containing the directory you called the script on (i.e. the value of %V).
Upvotes: 6