Use shebang/hashbang in Windows Command Prompt
Yes, this is possible using the PATHEXT
environment variable. Which is e.g. also used to register .vbs
or .wsh
scripts to be run "directly".
First you need to extend the PATHEXT
variable to contain the extension of that serve script (in the following I assume that extension is .foo as I don't know Node.js)
The default values are something like this:
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
You need to change it (through the Control Panel) to look like this:
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.FOO
Using the control panel (Control Panel -> System -> Advanced System Settings -> Environment Variables is necessary to persist the value of the PATHEXT variable.
Then you need to register the correct "interpreter" with that extension using the commands FTYPE
and ASSOC
:
ASSOC .foo=FooScriptFTYPE FooScript=foorunner.exe %1 %*
(The above example is shamelessly taken from the help provided by ftype /?
.)
ASSOC and FTYPE will write directly into the registry, so you will need an administrative account to run them.
Actually looks like someone who knows how to write batch files better than I has also approached this. Their batch file may work better.
http://whitescreen.nicolaas.net/programming/windows-shebangs
Command prompt does not support shebang , however there are a lot hybrid techniques for different languages that you allow to combine batch and other languages syntax in one file.As your question concerns node.js here's a batch-node.js hybrid (save it with .bat
or .cmd
extension):
0</* :{ @echo off node %~f0 %* exit /b %errorlevel%:} */0;console.log(" ---Self called node.js script--- ");console.log('Press any key to exit');process.stdin.setRawMode(true);process.stdin.resume();process.stdin.on('data', process.exit.bind(process, 0));
It is possible to be done with many other languages like Ruby,Perl,Python,PHP and etc.