windows脚本实例(Windows Script instance)
From the Windows 98 era, all kinds of script files appear constantly, the script file is designed to directly run various script file in Windows interface or the Dos command prompt, Microsoft in the system into a 32 bit Windows platform, scripting environment based on independent, and named it "Windows Scripting Host (hereinafter referred to as the WSH Windows script host)".
WSH was soon popularized in the Windows series after its birth. In addition to Windows 98, Microsoft in Internet, Information, Server, Windows, ME, Windows 2000 series products, as well as Windows, XP, Professional and other products are embedded in the WSH. Generally speaking, all kinds of software must consider the habits and needs of most people, and for some personalized needs, it is difficult to satisfy common software. Now using WSH, we can do a lot of interesting and useful functions, and the implementation of the code is very simple, and completely tailored for themselves, 100% to meet their own needs. How can you let go of such simple, practical and interesting things?
First, how does WSH work?
The premise of WSH's normal work is that the system must have more than IE3.0 version, because WSH needs to use the VBScript and JScript explain engines in IE at work.
First of all, let's look at a simple example of opening notepad and writing it down:
WScript.Echo (Hello! Computer ()
Then it is with.Vbs or.Js suffix (do not be saved as a TXT file, save it in the "file type" select "all files") file to be saved, and then turn off the notepad. From the icon of the file, it has turned into a script file. Double click to execute the file and see the result (Figure 1), and the first script we edited has been successfully run!
Figure 1
Now, let's take a look at the script file implementation through WSH. First, WSH queries the required script engines in the system registry, such as VBScript or JScript, depending on the script file suffix. Then execute the script commands, use the corresponding script engine which can use WSH script command some built-in objects (such as processing, registry entries) at this time, the command will request the WSH, and these instructions are completed by WSH.
So, how do you write and run WSH scripts? WSH script writing is very convenient, you can use any text editor, after that, you only need to save it for the WSH supported by the file name (such as.Js files,.Vbs files). The most commonly used editor is the Notepad that we wrote for the first script file.
I. preliminary WSH
Below, we look at the initial WSH some examples, in which each instance can achieve a simple function, in our understanding of each instance of the function, we will put these examples
of integrated into a very practical script to help you achieve strong power, practical personality can.
1. create shortcuts on the desktop
WSHShell = WScript.CreateObject ("WScript.Shell");
DesktopPath = WSHShell.SpecialFolders ("Desktop");
Shortcut1 = WSHShell.CreateShortcut (DesktopPath + "\ \ Notepad.Lnk shortcut");
Shortcut1.TargetPath = "c:\\Windows\\"";
Shortcut1.Save ();
This function is used to read the SpecialFolders desktop path, secure desktop path, creating a "\ Notepad.Lnk shortcut on the desktop file, and the file shortcut , finally, the informa
tion will be saved, the foot of the work is done.
2. perform specific commands
The following example opens the C:\autoexec.bat file with notepad,
And open the DOS command window (and list the results of executing dir c:\Windows), the program code is as follows:
WSH / / Shell
Shell = WScript.CreateObject ("WScript.Shell");
/ / open notepad and load c:\autoexec.bat
Shell.Run (" c:\\autoexec.bat");
/ / open a DOS command window and dir c:\Windows
Shell.Run ("CMD /K dir c:\\Windows");
In the example above, the open application stays open, and the WSH continues to execute the next program code. To wait until the application is closed to continue the subsequent WSH program code, you can add additional parameters after run (). What should we do if we need to continue after we close Notepad?
How the WSH / / execute other applications, and wait for the application to continue after the end of the WSH code
Shell = new ActiveXObject ("WScript.Shell");
IntReturn = shell.Run ("NOTEPAD" + WScript.ScriptFullName, 1, true);
Shell.Popup (notepad has been closed! ");
3. lists all the files in a particular directory
/ / create a file system object
FSO = new ActiveXObject ("Scripting.FileSystemObject");
/ / to the specified folder
Dir= "c:\\Windows\\temp"";
notepad++Fsofolder = fso.GetFolder (DIR);
Collect / folder contains files
ColFiles = fsofolder.Files;
FC = new, Enumerator (colFiles);
/ / display and continue to read the other file name until completion
WScript.Echo ("Files, under \", "+dir+", \ ":");
For (?, fc.atEnd (), fc.moveNext ()) {
WScript.Echo (fc.item ());
}
After the script executes, all the files in the specified directory (C:\windows\temp) are listed one by one.
4. displays the native IP address
Ws = new ActiveXObject ("MSWinsock.Winsock");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论