Windows脚本编程核心技术精解-随书示例脚本-第十三章

' 13-1.VBS 2' 13-2.VBS 4' 13-3.VBS 6' 13-4.VBS 8' 13-5.VBS 10' 13-6.VBS 12' 13-7.VBS 15' 13-1.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' scripting.filesystemobject' wscript.shell CheckCOM'---end of COM-object checking code--- set args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a vbs script file on my icon." _ & " I'll place it into the Startup group!" WScript.Quitend ifset fs = CreateObject("Scripting.FileSystemObject")set wshshell = CreateObject("WScript.Shell")scriptfile = args(0)if not lcase(fs.GetExtensionName(scriptfile))="vbs" then MsgBox "I only accept vbs script files!" WScript.Quitend ifstartup = wshshell.SpecialFolders("Startup")name = InputBox("Under which name should I place the script " _ & """" & fs.GetBaseName(scriptfile) & """ into the StartUp-" _ & "Group?", "autostart script")set scut = wshshell.CreateShortcut(startup & "\" & name & ".lnk")scut.TargetPath = scriptfilescut.SaveMsgBox "Script has been successfully linked to your StartUp group!" ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("scripting.filesystemobject") then msg="COM-Object ""scripting.filesystemobject"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-2.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' scripting.filesystemobject' wscript.shell CheckCOM'---end of COM-object checking code--- set args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a vbs script file on my icon." _ & " I'll place it into the Startup group!" WScript.Quitend ifset fs = CreateObject("Scripting.FileSystemObject")set wshshell = CreateObject("WScript.Shell")scriptfile = args(0)if not lcase(fs.GetExtensionName(scriptfile))="vbs" then MsgBox "I only accept vbs script files!" WScript.Quitend ifkey = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\"' use this key on Windows NT/2000:'key = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run\"wshshell.RegWrite key & "myscript", scriptfileMsgBox "Script has been successfully added to the Run key!" ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("scripting.filesystemobject") then msg="COM-Object ""scripting.filesystemobject"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-3.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' wscript.shell CheckCOM'---end of COM-object checking code--- set wshshell = CreateObject("WScript.Shell")key = "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\"' use this key on Windows NT/2000:'key = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run\"wshshell.RegDelete key & "myscript"MsgBox "Deleted script entry from Registry." ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-4.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' scripting.filesystemobject CheckCOM'---end of COM-object checking code--- set fs = CreateObject("Scripting.FileSystemObject")set args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a file, folder or drive on my icon!" WScript.Quitend if' args(0) now contains the file/folder/drive name' find out which type it isif fs.FileExists(args(0)) then set object = fs.GetFile(args(0))elseif fs.FolderExists(args(0)) then set object = fs.GetFolder(args(0))else MsgBox "You did not drag a file/folder/drive - can't find out" _ & " size!" WScript.Quitend ifMsgBox """" & args(0) & """ size: " & _FormatSize(object.size), vbInformationfunction FormatSize(bytes) if bytes<1024^2 then FormatSize = FormatNumber(bytes/1024,1) & " KB" else FormatSize = FormatNumber(bytes/1024^2,1) & " MB" end ifend function ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("scripting.filesystemobject") then msg="COM-Object ""scripting.filesystemobject"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-5.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' wscript.shell CheckCOM'---end of COM-object checking code--- set wshshell = CreateObject("WScript.Shell")' find out the script file you want to use as command extensionset args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a vbs script file onto my script icon!" WScript.Quitend if' did I receive a vbs file?if not lcase(Mid(args(0), InstrRev(args(0), ".")))=".vbs" then MsgBox "I only accept vbs script files!" WScript.Quitend if' insert this key as new context menu command:name = InputBox("How do you want to call your new command?", _,"Show size")' start with this key. Folders don't have a file type so' you can use this key right away:key1 = "HKCR\Folder\shell\"' insert command namewshshell.RegWrite key1 & name & "\", name' insert commandwshshell.RegWrite key1 & name & "\command\", _"wscript.exe " & """" & args(0) & """" & " ""%L"""MsgBox "New command installed successfully!" ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-6.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' wscript.shell CheckCOM'---end of COM-object checking code--- set wshshell = CreateObject("WScript.Shell")' find out the script file you want to use as command extensionset args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a vbs script file onto my script icon!" WScript.Quitend if' did I receive a vbs file?if not lcase(Mid(args(0), InstrRev(args(0), ".")))=".vbs" then MsgBox "I only accept vbs script files!" WScript.Quitend if' which file type do you want to append?filetype = InputBox("Please enter the file extension for the " _ & "file type you want to add a context menu command to!", _ , ".vbs")if not left(filetype,1)="." then filetype = "." & filetype' find out whether this key existson error resume nextprgkey = wshshell.RegRead("HKCR\" & filetype & "\")' file type unknown!if not err.number=0 then MsgBox "File Type """ & filetype & """ is unknown!" WScript.Quitend ifon error goto 0' insert this key as new context menu command:name = InputBox("How do you want to call your new command?", _,"Show size")' start with this keykey1 = "HKCR\" & prgkey & "\shell\"' insert command namewshshell.RegWrite key1 & name & "\", name' insert commandwshshell.RegWrite key1 & name & "\command\", _"wscript.exe " & """" & args(0) & """" & " ""%L"""MsgBox "New command installed successfully!" ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.' 13-7.VBS '---the following lines have been added to ensure all required COM objects are available on your system.' script uses the following components:' wscript.shell' clip.board CheckCOM'---end of COM-object checking code--- set wshshell = CreateObject("WScript.Shell")set args = WScript.Argumentsif args.Count=0 then MsgBox "Please drag a file onto my script icon!" WScript.Quitend ifset tool = CreateObject("Clip.board")tool.SetClipBoardText args(0)wshshell.Popup "Pasted information into clipboard", 1, _"Info", vbInformation ' --- the following code had been automatically added to ensure all required COM objects are installed.function COMExists(name)' checks whether a specific COM object is installed on' your local machineset cwsh = CreateObject("WScript.Shell")on error resume nextdummy = cwsh.RegRead("HKCR\" & name & "\")if err.number<>0 then COMExists = falseelse COMExists = trueend ifend function sub CheckCOMif not COMExists("wscript.shell") then msg="COM-Object ""wscript.shell"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Reinstall the Windows Scripting Host!" & vbCrMsgBox msg, vbExclamationend ifif not COMExists("clip.board") then msg="COM-Object ""clip.board"" is required. This object is currently not installed on your system." & vbCr msg = msg & "Install the CLIPBOARD component from CD" & vbCrMsgBox msg, vbExclamationend ifend sub'--- end of COM object checking code ' (C) 2000 by Dr. T. Weltner - all rights reserved.。