Windows脚本编程核心技术精解Chapter01.pdf
《Windows脚本编程核心技术精解Chapter01.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter01.pdf(28页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 1Script Development with EaseIn This Chapter?Write and edit script files?Create blank script files with a mouse click?Select script lines automatically?Get help when scripts misbehave?Execute scripts line by line?Log your scripts activity?Catch errors and handle them yourselfScripts are plai
2、n text files(see Figure 1-1),so you can use any text editor to develop them.However,most text editors are not designed to meetdevelopers needs.For example,they often lack the ability to mark specificlines youd like to take a closer look at.This is unfortunate because when theScripting Host encounter
3、s a problem,it tells you the line number that causedthe hiccupand its nice to have a convenient way of jumping to thatparticular line.In this chapter,youll learn how easy it is to use hidden script techniques toenhance any text editor that has line-marking capabilities.You will also discoverconvenie
4、nt ways of starting new script files with a mere right-click of the mousebutton.In addition,youll discover how scripts are executed line by line and learn about the Script Debugger,which allows you to step through your scriptcode while it is executed.This“slow motion”execution helps you understand t
5、he script mechanics and is also perfect for finding(and resolving)script errors easily.Writing Your First ScriptWriting scripts is as easy as launching your favorite text editor.Even thesimple Windows editor will sufficeJust select Run from your Start menuand enter NOTEPAD.If you like,you can even u
6、se your favorite word-processing software for writing scripts,by using line-numbering or other advanced features.Remember though,word processors dont save plain text files as a defaultthey use their own proprietary binary format.The Scripting Host cant4684-8 ch01.f.qc 3/3/00 9:30 AM Page 3decipher t
7、his format,so youll need to be sure to save your scripts as filetype plain text.In most cases,a plain text editor is the better choice.Now youll see your blank text editor window.This is your playground,theplace where you can start developing your scripts.The traditional first stepin programming boo
8、ks is to greet the world.Lets do it!Figure 1-1:Scripts are just plain text files.At this point,your script is a plain text file.In order to have Windowsinterpret it as a VBScript file and feed its contents to the Scripting Host,youll need to save it with the file extension.vbs(see Figure 1-2).Choose
9、Save As from the File Menu,change the Save in the listbox to Desktop,andenter the filename welcome.vbs.Figure 1-2:Save scripts with a.vbs file extension.Your script will be placed right on your desktop(see Figure 1-3).If you havecorrectly set up the Scripting Host as outlined previously,it will have
10、 thetypical script icon.4Part I:Scripting KickstartII4684-8 ch01.f.qc 3/3/00 9:30 AM Page 4Figure 1-3:A.vbsscriptTo see your script,just open up the file.It will pop up a small dialog box andsay hello to the world(see Figure 1-4).Figure 1-4:It works!Your script says hello to the world.Editing the co
11、ntents of a scriptDuring script development,you will probably edit a script many times,inorder to fix errors and adopt new features(see Figure 1-5).To keep thingssimple,its a good idea to break larger projects into small pieces and run thescript after each step is completed.This way,it is much easie
12、r to identify andcorrect script errors.Figure 1-5:Choose Edit to change a script anytime you feel like itTo change a script,right-click its icon and choose Edit.Make whateverchanges you like,and then save it again.Editing a script is like editing a letter,except that you cant open the script file to
13、 change it,because opening thefile always runs the script.Use the Edit command instead,or drag the scriptfile onto the program icon you want to use for displaying the file contents.Chapter 1:Script Development with Ease5II4684-8 ch01.f.qc 3/3/00 9:30 AM Page 5Conveniently starting new scriptsYou don
14、t have to go through all this clicking and renaming of file extensionsjust to start a new scripting project.Instead,use the scripting capabilities toplace a new command into the New menu.This way,its easy to get newscript files,of the right file type,anytime you want.To insert a new document type in
15、to your New menu,just execute thefollowing script:1-1.VBS this is the file extension the new command should generate:filetype=“.vbs”connect to WScript.Shell for registry access:set WSHShell=CreateObject(“WScript.Shell”)read in the name of the vbs-section:prg=ReadReg(“HKCR”&filetype&“”)read in the of
16、ficial name for vbs-files:prgname=ReadReg(“HKCR”&prg&“”)ask for a new nameask=“What should be the name for new VBScript scripts?”title=“New menu entry”prgname=InputBox(ask,title,prgname)save the new program description:WSHShell.RegWrite“HKCR”&prg&“”,prgname add a New menu entry asking for an empty n
17、ew file:WSHShell.RegWrite“HKCR”&filetype&“ShellNewNullFile”,“”reads a registry key should the key be invalid,complain and stop:function ReadReg(key)on error resume nextReadReg=WSHShell.RegRead(key)if err.Number0 then key could not be read:complain!error=“Error:Registry-Key“”&key _&“”could not be fou
18、nd!”MsgBox error,vbCriticalWScript.Quitend ifend functionThis script is a great example of how powerful the Scripting Host can be.With it,you can change the official name of.vbsfiles and write informationinto the windows registry to make the.vbsfiles available in the New menu.Just enter a name under
19、 which VBScript files will appear in your New menu(see Figure 1-6).6Part I:Scripting KickstartII4684-8 ch01.f.qc 3/3/00 9:30 AM Page 6It may take a few seconds before the new command becomes visible.To force Explorer to recognize the change,click an empty spot on the desktopand press F5.Figure 1-6:C
20、hoose a name for your.vbsscript filesTo start a new script(see Figure 1-7),right-click the mouse,choose New,andthen click the name you specified for.vbsfiles.Thats all there is to it.Windows will generate a new and empty.vbsfile for you.Figure 1-7:Start new script files with the help of New.All that
21、 is left to do is to rename the file and then right-click the file:Edit willopen the empty file in your text editor,and youll be ready to bring yourscript to life.For more information about how the script accomplishes all of this,seeChapter 13.For now,its just a convenient tool.Getting rid of a new
22、entry is even easier:1-2.VBSset WSHShell=CreateObject(“WScript.Shell”)filetype=“.vbs”Chapter 1:Script Development with Ease7II4684-8 ch01.f.qc 3/3/00 9:30 AM Page 7WSHShell.RegDelete“HKCR”&filetype&“ShellNew”MsgBox“Command removed!”Automatically renaming and opening new scriptsScripts can help you t
23、o automate routine tasksits their job.Lets startwith a convenient way to create new scripts from scratch!Placing file templates into the New menu is common practice for many filetypes,and you have just seen how to do this with.vbsfiles.The New menucan do much more,though.You can also place commands
24、into this menu,launching scripts that take over much of the work involved in getting new files.The next script places a new command into the New menu.This commanditself launches another script,and then the subsequent script takes over the responsibility of generating a new script file.1-3.VBS Name o
25、f your new command:commandname=“Get New VBScript file”connect to Wscript.Shell for registry access:set WSHShell=CreateObject(“WScript.Shell”)get path to windows folder:windir=WSHShell.ExpandEnvironmentStrings(“%WINDIR%”)name of the script to be executed by the new command:script=“newvbsfile.vbs”comm
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter01
限制150内