在 .bat 文件中嵌入 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2325420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
embed Javascript in .bat files
提问by SNA
Is ther any way to execute javascript from a .bat file or embed the javascript in .bat file.
有什么方法可以从 .bat 文件中执行 javascript 或将 javascript 嵌入到 .bat 文件中。
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.
我需要一个 javascript 代码来写入/读取本地文件夹中的文件。这个 javascript 我应该能够使用 .bat 执行它。
Is it possible?.
是否可以?。
Thanks
谢谢
SNA
国民账户体系
回答by Michael Dillon
Follow these two steps to run Javascript in a Windows batch file, either .BAT or .CMD.
按照以下两个步骤在 Windows 批处理文件(.BAT 或 .CMD)中运行 Javascript。
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.
第二步:编写您的 Javascript 以仅使用 Windows Scripting Host 中可用的对象,即使用 Wscript.Echo() 在标准输出上打印输出。
Here is a complete example ready to run by typing calen 2011 02
这是一个完整的示例,可以通过键入来运行 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.
只需将它放在 calen.bat 或 calen.cmd 中,然后在任何最近的 Windows 上运行它。再也不用纠结于另一个复杂的批处理文件了。
回答by YAHsaves
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.
我知道我对此有点晚了,但是如果您的系统上安装了 node.js,您可以使用 bat 文件调用节点函数并执行您的 javascript 代码。
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..
Node.js(使用模块时)可以让你做很多 bat 文件可以做的事情。仅使用 javascript,您就可以编辑/短语本地文件,运行 exe 文件等...等..
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:
设置此操作的分步指南是:1) 下载并安装 node.js https://nodejs.org/en/download/2) 在您的计算机上使用您要运行的 js 代码创建一个 javascript 文件。在此文件中,您将添加代码以允许 node.js 访问您机器上的本地文件。将以下代码放在 javascript 文件的顶部:
//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:
3)创建一个bat文件,如下所示:
echo off
node "path-to-javascript\index.js"
And you're all setup!
你都设置好了!
回答by Leon van der Walt
on Windows, in a batch file, try
在 Windows 上,在批处理文件中,尝试
cscript jsfile.js
cscript jsfile.js
回答by npocmaka
Depends on which javascript you mean. You have few options - all the scripts bellow should be saved with .batextension:
取决于您的意思是哪个 javascript。您几乎没有选择 - 下面的所有脚本都应使用.bat扩展名保存:
1)JScript that comes with csript.exe:
1)csript.exe自带的JScript:
@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):
2)HTA/IExplorer 附带的 javascript(它还允许您使用 UI 元素和访问本地文件系统):
<!-- :
@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):
3)JSCrip.NET - 可能是最强大的选项,因为它可以让您访问 .NET 框架(尽管它会创建一个小的 .exe 文件):
@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.
有多种方法可以在不创建此 exe 文件的情况下使用它,但据我所知,这是最易读的方法。
4)If you are aming NODE.JS there's also a way:
4)如果您使用的是 NODE.JS,还有一种方法:
0</* ::
@echo off
echo hello from batch
node "%~f0" %*
exit /b %errorlevel%
*/0;
console.log('Hello from Node');
回答by VoidPointer
You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.
您可以使用基于命令行的脚本宿主CScript.exe从批处理文件运行 JScript 脚本。
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.
您需要将脚本放在一个单独的文件中,尽管您将其作为参数传递给 CScript.exe。如果您想将所有内容保存在单个文件中,您可以将 JScript 代码嵌入您的批处理文件中,将其转储到您传递给 CScript.exe 的临时文件中,然后删除临时脚本文件。
There might be more elegant solutions (hopefully)
可能有更优雅的解决方案(希望如此)
回答by Pekka
回答by p.marino
Short answer: no.
简短的回答:没有。
Javascript (when run from a browser) can't access the client resources (this is by design, to avoid security risks).
Javascript(从浏览器运行时)无法访问客户端资源(这是设计使然,以避免安全风险)。
You can use Javascript outsideof 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).
您可以在浏览器之外使用 Javascript来创建脚本,但您应该提供有关您尝试执行的操作(以及您希望在什么操作系统上运行)的更多详细信息。
Also, check this pagefor more details.
此外,请查看此页面以了解更多详细信息。
回答by VoidPointer
You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.
您可以使用基于命令行的脚本宿主CScript.exe从批处理文件运行 JScript 脚本。
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.
您需要将脚本放在一个单独的文件中,尽管您将其作为参数传递给 CScript.exe。如果您想将所有内容保存在单个文件中,您可以将 JScript 代码嵌入您的批处理文件中,将其转储到您传递给 CScript.exe 的临时文件中,然后删除临时脚本文件。
There might be more elegant solutions (hopefully)
可能有更优雅的解决方案(希望如此)

