Is ther any way to execute javascript from a .bat file or embed the javascript in .bat file.
I need a javascript code to write/read to a file in a local folder.This
Depends on which javascript you mean. You have few options - all the scripts bellow should be saved with .bat
extension:
1) JScript that comes with csript.exe:
@if (@X) == (@Y) @end /*
@cscript //E:JScript //nologo "%~f0" "%*"
@exit /b %errorlevel%
*/
WScript.StdOut.WriteLine("Echo from cscript's javascript");
2) javascript that comes with HTA/IExplorer (which allows you also to use UI elements and access to the local file system):
~~~~
Hello from HTA's javascript
3) JSCrip.NET - may be most powerfull option as gives you access to the .NET framework (though it creates a small .exe file):
@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
%~n0.exe %*
endlocal & exit /b %errorlevel%
*/
import System;
Console.Write("Echo from .NET")
There are ways to use it without creating this exe file ,but this is the most readable way according to me.
4) If you are aming NODE.JS there's also a way:
0* ::
@echo off
echo hello from batch
node "%~f0" %*
exit /b %errorlevel%
*/0;
console.log('Hello from Node');