spenibus.net
2014-11-15 02:25:27 GMT

Command line PHP and Javascript under windows - simple CLI setup

Update 2015-05-02 23:18 +0200 -- <http://spenibus.net/b/p/q/Command-line-PHP-under-windows-update-console-window-wait-to-close-switch> --- Update 2014-11-29 18:25 +0100 -- Replaced @="F:\\progs\\php-cli\\run.bat %0 -- %* with @="F:\\progs\\php-cli\\run.bat \"%0\" -- %* to handle spaces in filenames Replaced @="F:\\progs\\js-cli\\run.bat %0 %*" with @="F:\\progs\\js-cli\\run.bat \"%0\" %*" to handle spaces in filenames --- Prologue - When you need to perform simple, repetitive tasks, little scripts are fantastic allies. Renaming files, mining data from twitter or even fetching pictures from your camera and checking the integrity of the transfer with some hashing. And as lazyness is a programmer's best friend, why waste time installing and learning perl or python when you can use your existing knowledge of PHP and Javascript ? >"But it's annoying to start my web server to use PHP for a small script" >"Javascript runs in my web browser, that is rather limited" Yes. Yes. But also no. PHP can be run from the command line: <http://php.net/manual/en/features.commandline.php> Javascript can be run from the command line using node.js: <http://nodejs.org> I will hereby share my personal setup for both cases. Note -- I use the extensions .phpcli and .jscli to avoid potential issues. You can do as you please. PHP setup -- First we create a folder and a few files within it: ```` F:/progs/php-cli/ hello.phpcli php_script.ico run.bat shell-disable.reg shell-enable.reg ```` hello.phpcli -- ```` <?php echo "hello there"; ?> ```` This is a test file. php_script.ico -- php_script.ico is available at the link below. You can obviously use another icon, or none. <http://php.net/download-logos.php> run.bat -- ```` @call "F:/progs/WAMP/php/php.exe" -f %* @echo. @set /p="Hit ENTER to exit" ```` This contains the actual command that will run the script. I chose this method so I could keep the cmd.exe window open after the script has finished running. The same could have been achieved by putting `fgets(STDIN);` at the end of each PHP script. Obviously, you must use the path of your own `php.exe`. shell-disable.reg -- ```` REGEDIT4 [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.phpcli] [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli] ```` This disables the execution of PHP files if you ever need it. shell-enable.reg -- ```` REGEDIT4 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.phpcli] @="php-cli" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli] @="PHP CLI Script" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli\DefaultIcon] @="F:\\progs\\php-cli\\php_script.ico,0" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli\shell] @="run" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli\shell\run] @="Run script" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\php-cli\shell\run\command] @="F:\\progs\\php-cli\\run.bat \"%0\" -- %*" ```` This will add entries to the windows registry that will allow PHP files to execute. Now we run `shell-enable.reg` to enable the execution of PHP files. To see the icon, we could reboot but there is a faster way. Execute `ie4uinit.exe -ClearIconCache` from the command line, then refresh your folder view with F5. You should see something like this: ![](http://spenibus.net/f/g/5p/screenshot-20141115-023617-003437.png) We double-click on hello.phpcli and we should see this: ![](http://spenibus.net/f/g/5q/screenshot-20141115-024439-003438.png) And we're done with PHP. Javascript setup -- We create folder and files: ```` F:/progs/js-cli/ hello.jscli js.ico run.bat shell-disable.reg shell-enable.reg ```` hello.jscli -- ```` console.log('hello there'); ```` js.ico -- I use the icon available at the link below. <http://www.iconarchive.com/show/adobe-cs4-icons-by-hopstarter/File-Adobe-Dreamweaver-JavaScript-icon.html> run.bat -- ```` @call "F:/progs/node/node.exe" %* @echo. @set /p="Hit ENTER to exit" ```` Don't forget to use your own node.exe path. shell-disable.reg -- ```` REGEDIT4 [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.jscli] [-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli] ```` shell-enable.reg -- ```` REGEDIT4 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.jscli] @="js-cli" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli] @="javascript cli via node.js" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli\DefaultIcon] @="F:\\progs\\js-cli\\js.ico,0" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli\shell] @="run" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli\shell\run] @="Run" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\js-cli\shell\run\command] @="F:\\progs\\js-cli\\run.bat \"%0\" %*" ```` Now we run `shell-enable.reg` to enable the execution of Javascript files. Again, we execute `ie4uinit.exe -ClearIconCache` from the command line and refresh the folder view with F5. ![](http://spenibus.net/f/g/5r/screenshot-20141115-030449-003439.png) We double-click on hello.jscli and we should see this: ![](http://spenibus.net/f/g/5s/screenshot-20141115-030644-003441.png) And we're done with Javascript. Epilogue -- You should now be able to run serverless PHP and browserless Javascript.