Execute .bat files and cmd commands using the WScript.Shell object in Javascript

WScript.Shell (Windows Script Host Runtime Library) is an object, and the corresponding file is C:/WINDOWS/system32/wshom.ocx. Wscript.shell is a component used by the server system. Shell means "shell". This object can perform operations commonly used by operating system shells, such as running programs, reading and writing the registry, and environment variables. This object is usually used in VB or VBS programming.

  Install WScript.Shell object: regsvr32 WShom.Ocx

  Uninstall the WScript.Shell object: regsvr32 -u WShom.Ocx or regsvr32 /u WShom.Ocx

  For Example:

  1. Create the test.bat file and save it in the D: root directory. The function is to copy the *txt file to the d:/test directory.  

1
2
3
md test
copy d:/*.txt d:/test
pause

   2. Create a WScript.Shell object and run the test.dat file directly by this object.

1
2
3
var  objShell;
objShell= new  ActiveXObject( "WScript.Shell" );
var  iReturnCode=objShell.Run( "c:/test.bat" ,0, true );

   3. Create a WScript.Shell object and run CMD commands directly from this object.

1
2
3
4
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 )

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326642776&siteId=291194637