Windows脚本编程核心技术精解Chapter16.pdf
《Windows脚本编程核心技术精解Chapter16.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter16.pdf(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 16Getting System InformationIn This Chapter?Determine the location of Windows special folders such as Desktop and MyDocuments?Delete your Documents menu to ensure privacy?Delete references to document types you dont work with anyway?Find out important networking details such as username and
2、computer name?Retrieve memory statistics and determine current work load,physicalmemory,and virtual memory management details?Detect wheel mouse,sound card,and boot mode?Retrieve current video resolution and change resolution and video refreshrate on-the-fly?List all installed fonts and create your
3、personal font sampler page?Analyze your processor type and Windows version?Find out if specific DLLfunctions are available on your systemScript developers need information.This chapter provides methods toretrieve all kinds of information about your system,your configuration,and your installation fol
4、ders.Finding Out Important Path InformationWhere does Windows live?The true name of the Windows folder may varyfrom system to system,and so do the names of all the other special foldersWindows uses.For your scripts,its extremely important to know the special Windows folderlocations.Maybe you want to
5、 insert your script into the StartUp programgroup to start it automatically at logon.Or maybe you need access to theRecentDocsfolder to delete references you dont need.The following sections demonstrate how to retrieve the path names of allyour special Windows folders.4684-8 ch16.f.qc 3/3/00 9:40 AM
6、 Page 483Retrieving special folder path namesThe WScript.Shellobject is your solution.It offers the SpecialFodersmethod.This method returns the actual path name of any special Windowsfolder(see Figure 16-1).Heres how to retrieve the path to your programgroups(the groups you see when you open Program
7、s in the Start menu):16-1.VBSset wshshell=CreateObject(“WScript.Shell”)path=wshshell.SpecialFolders(“Programs”)MsgBox“The path to your private Programs folder:”&vbCr&pathFigure 16-1:Retrieve path names of special Windows folders.On Windows 98,NT,and 2000,there may be individual user profiles.This is
8、 whythese Windows versions divide many user settings into private settings andcommon settings that apply to all users.To retrieve the location of the programgroups for all users,just replace“Programs”with“AllUsersPrograms”.See how you can take advantage of this:16-2.VBSset wshshell=CreateObject(“WSc
9、ript.Shell”)set fs=CreateObject(“Scripting.FileSystemObject”)path=wshshell.SpecialFolders(“Programs”)for each folder in fs.GetFolder(path).subfolderslist=list&folder.name&vbCrnextMsgBox“List of your private program groups:”&vbCr&listTable 16-1 lists all the special folder names you can use with Spec
10、ialFolders:484Part IV:Accessing the Operating SystemII4684-8 ch16.f.qc 3/3/00 9:40 AM Page 484Table 16-1Key Names for Windows FoldersNameDescriptionAllUsersDesktopDesktop items for all usersAllUsersStartMenuStart menu custom items for all usersAllUsersProgramsProgram groups for all usersAllUsersStar
11、tupAuto-starting shortcuts for all usersDesktopDesktop items for current userFavoritesFavorites items for current userFontsInstalled system fontsMyDocumentsPrivate document folder for current userNetHoodCustom items for Network Neighborhood virtual folderPrintHoodCustom items for Printers virtual fo
12、lderProgramsProgram groups for current userRecentList of recently used documents for current userSendToSendTo itemsStartMenuStart menu custom items for current userStartupAuto-starting shortcuts for current userTemplatesTemplate files for New menu itemsThe“AllUsers.”settings apply to common objects
13、only.On Windows 95,they are unavailable,and if you dont use multiple user profiles,chances arethese folders are empty.Cleaning up your Documents menuThe Documents menu inside your Start menu stores the most recently used15 files for easy access.Unfortunately,Windows doesnt care about the filetypes,s
14、o the Documents menu records just any file you happen to open.Therefore,important files can get kicked out of this menu easily by other not-so-important files.Furthermore,the Documents menu becomes unwieldy andconfusing if it contains a lot of unwanted files.You can do something about it.Clean your
15、menu up!Have a script go throughall the shortcuts,determine their targets,and delete any reference thatpoints to a file type you are not interested in!Chapter 16:Getting System Information485II4684-8 ch16.f.qc 3/3/00 9:40 AM Page 485 16-3.VBSset wshshell=CreateObject(“WScript.Shell”)set fs=CreateObj
16、ect(“Scripting.FileSystemObject”)find out location of special folderrecent=wshshell.SpecialFolders(“Recent”)access folderset folder=fs.GetFolder(recent)go through all shortcutsfor each file in folder.files get extension typeext=lcase(fs.GetExtensionName(file.name)is it a shortcut?It should be!if ext
17、=”lnk”then open shortcutset scut=wshshell.CreateShortcut(file.path)find targettarget=scut.TargetPath target still valid?if not fs.FileExists(target)then no,deletefile.deleteelse does target reference“important”file type?ext=lcase(fs.GetExtensionName(target)select case ext add extensions for all file
18、 references you want to keep:case“doc”case“bmp”case“vbp”case else points to something else,deletefile.deleteend selectend ifend ifnextwshshell.Popup“Cleaned Documents Menu!”,2Take a look at your Documents menu,then launch the script.When yourecheck your Documents menu,youll see that the script has d
19、eleted allreferences to file types you did not specify in your script.Now,theDocuments menu only contains references to files you really work with.It looks much cleaner and has gained room for new references.486Part IV:Accessing the Operating SystemII4684-8 ch16.f.qc 3/3/00 9:40 AM Page 486When you
20、call the script for the first time,it may take some seconds tocomplete the cleaning process.This is due to a Windows bugtheDocuments menu stores only the most recent 15 documents.Internally,whenever the limit is reached,Windows deletes old entries to make room for new entries.This works perfectly we
21、ll and happens inside the Registry.Outside,in your RECENTfolder,Windows often leaves behind unusedshortcuts.Over time,hundreds of shortcuts may be left behind in yourRECENTfolder,and its a good idea to clean them up.Use your new script whenever you find your Documents menu crammed withentries.Even b
22、etter,you can launch your script automatically at Windowslogon so that you always start with a clean Documents menu.See Chapter 13for details.Cleaning the Documents menu automaticallyIf you want your script to clean up the Documents menu automatically atgiven intervals,you can use this script instea
23、d:16-4.VBSset wshshell=CreateObject(“WScript.Shell”)set fs=CreateObject(“Scripting.FileSystemObject”)recent=wshshell.SpecialFolders(“Recent”)set folder=fs.GetFolder(recent)dofor each file in folder.filesext=lcase(fs.GetExtensionName(file.name)if ext=”lnk”thenset scut=wshshell.CreateShortcut(file.pat
24、h)target=scut.TargetPathif not fs.FileExists(target)thenfile.deleteelseext=lcase(fs.GetExtensionName(target)select case extcase“doc”case“bmp”case“vbp”case elsefile.deleteend selectend ifend ifnext sleep for a minuteWScript.Sleep 1000*60loopChapter 16:Getting System Information487II4684-8 ch16.f.qc 3
25、/3/00 9:40 AM Page 487This script runs forever.It cleans your Documents menu,and then it sleepsfor a minute and cleans again.You have no way of stopping this script exceptby calling the Task List and closing down WScript.exemanually.If you runscripts this way,always make sure you include a“sleep pha
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter16
限制150内