开发者

embed Javascript in .bat files

开发者 https://www.devze.com 2022-12-20 22:22 出处:网络
Is ther 开发者_如何学Goany way to execute javascript from a .bat file or embed the javascript in .bat file.

Is ther 开发者_如何学Goany 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 javascript i should be able to execute it using a .bat.

Is it possible?.

Thanks

SNA


Follow these two steps to run Javascript in a Windows batch file, either .BAT or .CMD.

First step: add these lines to the beginning of your file

@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/

Second step: write your Javascript to only use objects that are available in Windows Scripting Host, i.e. use Wscript.Echo() to print output on the standard output.

Here is a complete example ready to run by typing calen 2011 02

@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/
x = WScript.Arguments
Yr = x(0) ; Mo = x(1)

YS = "JanFebMarAprMayJunJulAugSepOctNovDec"
MN = Mo<1 || Mo>12 ? Mo : YS.substr(3*Mo-3, 3) // Month Name
WScript.echo(" ", Yr, "         ", MN)
WScript.echo(" Mo Tu We Th Fr Sa Su")
WD = new Date(Yr, Mo-1, 1).getDay() ;
if (WD==0) WD = 7 // Week Day Number of 1st
LD = new Date(Yr, Mo, 0).getDate() // Last Day of month
Wk = "" ; for (D=1 ; D < WD ; D++) Wk += "   "

for (D=1 ; D<=LD ; D++) {
  Wk = Wk + " " + (D<10 ? "0"+D : D) ; WD++
  if ((WD==8) || (D==LD)) { WScript.echo(Wk) ; WD = WD-7 ; Wk = "" }
  }

WScript.echo("        ------       ")

Just put this in calen.bat or calen.cmd and run it on any reasonably recent Windows. Never struggle with another convoluted batch file again.


I know I am a bit late to this, but if you have node.js installed on your system you can use the bat file to call in a node function and execute your javascript code.

https://nodejs.org/en/

Node.js (when using modules) lets you do quite a few things bat files can do. Using only javascript you can edit/phrase local files, run exe files ect... ect..

A step by step guide to setting this up would be: 1) download and install node.js https://nodejs.org/en/download/ 2) Create a javascript file on your computer with the js code you want to run. In this file you will add the code to allow node.js to access local files on your machine. Put the below code at the top of your javascript file:

//Load required modules
fs = require('fs')

//Function to read local files
fs.readFile('path-to-file/this-is-my-file.txt', 'utf8', function (err,data) {
  if (err) {

    return console.log(err);
  }
  //data is the text file
}

3) create a bat file like this:

echo off

node "path-to-javascript\index.js"

And you're all setup!


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):

<!-- :
@echo off
mshta.exe "%~f0"
exit /b %errorlevel%
-->

<html>
<head>
<title>~~~~</title>
<!--meta http-equiv="X-UA-Compatible" content="IE=edge"--> 
</head>
<body>

<font size="15"><b>Hello from HTA's javascript</b></font>
    <script type="text/javascript">
        setTimeout(function() {
            document.body.innerHTML=document.body.innerHTML + "<p/><p><font size='7'>Hello from HTA's javascript</font></p>";;
        }, 2000);
    </script>
</body>
</html>

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');


on Windows, in a batch file, try

cscript jsfile.js


You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.

You would need the script in a separate file though which you pass as an argument to CScript.exe. If you want to keep everything in a sinle file, you can embed the JScript code in you batch-file, dump it to a temporary file which you pass to CScript.exe and delete the temporary script file afterwards.

There might be more elegant solutions (hopefully)


It is, sort of. On Windows only, of course.

That said, there is probably a better way to do this. What do you need the JavaScript to do? Why does it have to be JavaScript?


Short answer: no.

Javascript (when run from a browser) can't access the client resources (this is by design, to avoid security risks).

You can use Javascript outside of the browser, to create scripts, but you should provide more details on what you are trying to do (and what Operative System you expect to run this on).

Also, check this page for more details.


You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.

You would need the script in a separate file though which you pass as an argument to CScript.exe. If you want to keep everything in a single file, you can embed the JScript code in you batch-file, dump it to a temporary file which you pass to CScript.exe and delete the temporary script file afterwards.

There might be more elegant solutions (hopefully)

0

精彩评论

暂无评论...
验证码 换一张
取 消