'On Error Resume Next If (lcase(right(wscript.fullname,11)) "wscript.exe") then set objShell=createObject("wscript.shell") objShell.Run("Wscript //nologo "&chr(34)&wscript.scriptfullname&chr(34)) Wscript.Quit end if
Set pCmd=CreateObject("WScript.Shell") Set pFso=CreateObject("Scripting.FileSystemObject") Set pShell = CreateObject("Shell.Application") Set pSysEnv = CreateObject("WScript.Shell").Environment("system") strComputer = "." Set pWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Function LGetPath(pFile) iLastSepPos = InstrRev(pFile, "/", -1, 1) If iLastSepPos = 0 Then LGetPath="" Exit Function Else LGetPath = Left(pFile, iLastSepPos-1) End If End Function
Function GetLnkTarget(linkPath) linkPath=Replace(linkPath, "/", "http://") Set pFiles = pWMIService.ExecQuery("Select * From Win32_ShortcutFile WHERE Name = " & "'" & linkPath & "'") For Each pFile in pFiles GetLnkTarget=pFile.Target Exit For Next End Function
Function ListSysCmd(pFileName) SysCmdPath=pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") Set pFolder = pFso.GetFolder(SysCmdPath) Set pFile = pFso.OpenTextFile(pFileName, 2, True)
For Each file in pFolder.Files linkPath=SysCmdPath & "/" & file.name IF UCASE(Right(file.name, 4))=".LNK" Then Set lnkFiles = pWMIService.ExecQuery("Select * From Win32_ShortcutFile WHERE Name = " & "'" & Replace(linkPath, "/", "http://") & "'") For Each lnkFile in lnkFiles pFile.WriteLine(linkPath & " " & lnkFile.Target) Next Else pFile.WriteLine linkPath End IF Next End Function
Function GetConfigPath Set pFolder = pShell.BrowseForFolder(0, "請選擇一個目錄:", 0, "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") If pFolder Is Nothing Then Wscript.Quit End If Set pFolderItem = pFolder.Self GetConfigPath = pFolderItem.Path End Function
Sub SetEnv(pPath, pValue) If pValue="" Then pSysEnv.Remove(pPath) Else pSysEnv(pPath) = pValue End IF End Sub
Function GetEnv(pPath) GetEnv = pSysEnv(pPath) End Function
'判斷一個字符串是否匹配一個正則表達式 ' ^/w+[@]/w+[.]/w+$ E-Mail地址 ' ^[0-9-]+$ 數字 Function IsMatch(Str, Patrn) Set r = new RegExp r.Pattern = Patrn IsMatch = r.test(Str) End Function
Sub AddNewCmd(pShortCutName, pTargetPath, pCmdLocation) IF pShortCutName "" Then LinkDir = pCmdLocation Set pCmdLink = pCmd.CreateShortcut(LinkDir & "/" & pShortCutName & ".lnk") pCmdLink.TargetPath = pTargetPath pCmdLink.WindowStyle = 1 pCmdLink.Hotkey = "" pCmdLink.IconLocation = "%systemroot%/system32/shell32.dll,146" pCmdLink.Description = "Shortcut Created At " & Date() & " " & Time() pCmdLink.WorkingDirectory = LGetPath(pTargetPath) pCmdLink.Save End IF End Sub
Set pArgs=Wscript.Arguments If pArgs.Count = 0 Then '無參運行,復制自身到SendTo文件夾。
If Right(SysCmdPath, 1)="/" Then SysCmdPath = Left(SysCmdPath, Len(SysCmdPath)-1) SysCmdPathPattern ="(;)?(" & Replace(SysCmdPath, "/", "http://") & ")(//)?(;|$)" If Not IsMatch(Path, SysCmdPathPattern) Then Path = Path " & SysCmdPath SetEnv "Path", Path End If
thisFile = WScript.ScriptFullName IF thisFile SysCmdPath & "/" & WScript.ScriptName Then pFso.CopyFile thisFile, SysCmdPath & "/" thisFile = SysCmdPath & "/" & WScript.ScriptName End IF
AddNewCmd "Q", pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd"), pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") AddNewCmd "QC", thisFile, pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") MsgBox "安裝成功!" ElseIf pArgs.Count = 1 Then IF UCase(pArgs(0))="EDIT" Then '只有一個參數且為Edit時, 打開此腳本進行編輯。 pCmd.Run("Notepad.exe " & WScript.ScriptFullName) WScript.Quit() ElseIF UCase(pArgs(0))="LIST" Then '只有一個參數且為List時, 列出所有已經建立的快捷方式和其對應的目標文件。 ResultFile=pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") & "/ListSysCmd.txt" ListSysCmd ResultFile pCmd.Run("Notepad.exe " & ResultFile) WScript.Quit() Else '只有一個參數時, 默認處理方式是建立傳入的文件路徑的快捷方式。 pShortCutName=InputBox("請輸入該快捷方式的名字:", "創建快捷命令...", "") IF pShortCutName="" Then WScript.Quit() AddNewCmd pShortCutName, pArgs(0), pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") End IF ElseIf pArgs.Count = 2 Then IF UCase(pArgs(0))="S" Then '查找快捷方式目標文件位置 '判斷是否包括/,從右鍵菜單執行會直接傳遞目標地址,從快捷方式中需要組合快捷方式的地址。 IF Instr(pArgs(1), "/") 0 And Instr(pArgs(1), ".lnk") = 0 Then '從右鍵菜單執行 pCmd.Run("Explorer.exe /select, " & pArgs(1)) Else '從命令行執行 IF Instr(pArgs(1), "/") 0 Then '全路徑.lnk路徑 linkPath=pArgs(1) Else linkPath=pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") & "/" & pArgs(1) & ".lnk" End IF linkPath=Replace(linkPath, "/", "http://") Set pFiles = pWMIService.ExecQuery("Select * From Win32_ShortcutFile WHERE Name = " & "'" & linkPath & "'") For Each pFile in pFiles pCmd.Run("Explorer.exe /n, /select, " & pFile.Target) Next End IF Else 'FileLocation, ShortCutName pShortCutName=pArgs(1) AddNewCmd pShortCutName, pArgs(0), pCmd.RegRead("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/SysCmd") End IF End If