用Javascript调用cmd并执行命令?

JavaScript027

用Javascript调用cmd并执行命令?,第1张

建立test.bat文件,存于D:根目录下,作用是将*txt文件拷贝到d:/test目录下。

md test

copy d:/*.txt d:/test

pause

创建WScript.Shell对象,由该对象直接运行test.dat文件。

var objShell

objShell=new ActiveXObject("WScript.Shell")

var iReturnCode=objShell.Run("c:/test.bat",0,true)

创建WScript.Shell对象,由该对象直接运行CMD命令。

var objShell

var objShell= new ActiveXObject("WScript.Shell")

var iReturnCode=objShell.Run("cmd.exe /c md test",0,true)

iReturnCode=objShell.Run("cmd.exe /c copy d:/*.text mytest",0,true)

在js中调用php的代码:

<SCRIPT Language = "JavaScript">

function func()

{ if(confirm("Are you OK with this?"))

{ this.location = "ok.php?action=ok"}

else

{ this.location = "ok.php?action=cancel"} }

</SCRIPT>

<html>

<head>

</head>

<body>

<a href="#" href="#" onClick="javascript:func()">Please Click</a>

</body>

</html>

if($_GET["action"]=="ok") 

{ echo "I'm OK!"}

else

{echo "I'm not OK!"}

ActiveXObject是IE的东西, 你在node里要用child_process, 例如

var

spawn = require('child_process').spawn,

call = spawn('cmd.exe', ['/c','echo','echo something'])

call.stdout.on('data', function(Q){console.log(Q.toString())})

call.stderr.on('data', function(Q){console.log('error',Q.toString())})

call.on('exit', function(Code){console.log('Exited',Code)})