Scripts for Windows Scripting Host by John Goalby Listing One ' ================================================================== ' Script to exectute a query against an ADO compliant database ' ================================================================== ' ------------------------------------------------------------------ ' Call the main subroutine ' ------------------------------------------------------------------ call Main() ' ------------------------------------------------------------------ ' We are finishing now ' ------------------------------------------------------------------ WScript.Quit (0) ' ------------------------------------------------------------------ ' The main subroutine ' ------------------------------------------------------------------ Sub Main ' -------------------------------------------------------------- ' Get an object of the params passed in to this script ' -------------------------------------------------------------- Set objArgs = Wscript.Arguments ' -------------------------------------------------------------- ' Check the arguments passed ' -------------------------------------------------------------- If objArgs.Count < 1 Then WScript.Echo "Please specify a query to execute." WScript.Echo "" WScript.Echo "Valid paramters are " WScript.Echo " DSN:" WScript.Echo " User:" WScript.Echo " Password:" WScript.Echo " Query:" WScript.Echo "" WScript.Echo "If there is no keyword the parameter is " &_ "assumed to be the query." WScript.Echo "" WScript.Echo "Example : DB DSN:MyDSN ""User:AUser"" """ &_ "select * from table""" WScript.Quit (1) End If ' -------------------------------------------------------------- ' This is where you could set defaults ' -------------------------------------------------------------- strDSN = "" strUser = "" strPassword = "" strQuery = "" ' -------------------------------------------------------------- ' For each of the arguments passed see if they start with a ' keyword and a : If so get the remaining text from the param ' -------------------------------------------------------------- For I = 0 to objArgs.Count - 1 strArg = objArgs(I) If (Left (UCase (strArg), 4) = "DSN:") Then strDSN = Right (strArg, Len (strArg) - 4) ElseIf (Left (UCase (strArg), 5) = "USER:") Then strUser = Right (strArg, Len (strArg) - 5) ElseIf (Left (UCase (strArg), 9) = "PASSWORD:") Then strPassword = Right (strArg, Len (strArg) - 9) ElseIf (Left (UCase (strArg), 6) = "QUERY:") Then strQuery = Right (strArg, Len (strArg) - 6) Else strQuery = strArg End If Next ' -------------------------------------------------------------- ' Check that we have something to do ' -------------------------------------------------------------- If (Len (strQuery) = 0) Then WScript.Echo "Please specify a query to execute" WScript.Quit (2) End If ' -------------------------------------------------------------- ' Create an ADO connection ' -------------------------------------------------------------- Set objDB = WScript.CreateObject("ADODB.Connection") ' -------------------------------------------------------------- ' See if can connect to the passed in DSN using passed username ' and password ' -------------------------------------------------------------- objDB.open strDSN, strUser, strPassword ' -------------------------------------------------------------- ' Execute the query passed ' -------------------------------------------------------------- Set objRS = objDB.Execute (strQuery) ' -------------------------------------------------------------- ' Check to make sure that open state ' -------------------------------------------------------------- if objRS.State = 1 Then ' ---------------------------------------------------------- ' Go through recordset and output each field returned ' ---------------------------------------------------------- Do While Not objRS.Eof For Each oField In objRS.Fields if oField = "" or IsNull(oField) Then WScript.Echo "" Else WScript.Echo oField.Name & " : " & oField.Value End If Next ' ------------------------------------------------------ ' Skip to next record and space between records ' ------------------------------------------------------ objRS.MoveNext WScript.Echo "" Loop else WScript.Echo "Nothing returned" End if ' -------------------------------------------------------------- ' Clear objects ' -------------------------------------------------------------- Set objDB = Nothing End Sub ' ------------------------------------------------------------------ ' End of script ' ------------------------------------------------------------------ Listing Two >DB DSN:ADOSamples "select * from Products where UnitPrice > 450" ProductID : 14 ProductCode : AW535-11 ProductType : Tent ProductIntroductionDate : 5/29/96 ProductName : Galaxy ProductDescription : durable 3-person all-purpose tent, maximum headroom, no-leak floor, beige/purple (bp) ProductImageURL : /advworks/multimedia/catalog/camping/dtents19.gif UnitPrice : 535 OnSale : False Listing Three ' ================================================================== ' Script to get the contents of a web page ' ================================================================== ' ------------------------------------------------------------------ ' Call the main subroutine ' ------------------------------------------------------------------ call Main() ' ------------------------------------------------------------------ ' We are finishing now ' ------------------------------------------------------------------ WScript.Quit (0) ' ------------------------------------------------------------------ ' The main subroutine ' ------------------------------------------------------------------ Sub Main ' -------------------------------------------------------------- ' Get an object of the params passed in to this script ' -------------------------------------------------------------- Set objArgs = Wscript.Arguments ' -------------------------------------------------------------- ' Check the arguments passed, only have 1 param in this script ' -------------------------------------------------------------- If objArgs.Count <> 1 Then ' ------------------------------------------------------ ' Tell the user the usage of this script ' ------------------------------------------------------ WScript.Echo "Please pass only 1 param, " &_ "the URL that you would like retrieved" WScript.Quit (1) End If ' ------------------------------------------------------------------ ' Create the Java helper object ' ------------------------------------------------------------------ Set objJUtil = CreateObject ("Java.JUtil") ' -------------------------------------------------------------- ' Get the actual web page and output to the console ' -------------------------------------------------------------- strResponseText = objJUtil.getWebPage (objArgs(0)) WScript.Echo strResponseText ' ------------------------------------------------------------------ ' Clear up ' ------------------------------------------------------------------ Set objJUtil = Nothing End Sub ' ------------------------------------------------------------------ ' End of script ' ------------------------------------------------------------------ Listing Four strResponseText = objJUtil.getWebPage (objArgs(0)) WScript.Echo strResponseText Listing Five favorites.vbs ' ================================================================== ' Script to check your IE favorites for dead links. ' ================================================================== ' ------------------------------------------------------------------ ' Create the Java helper object ' ------------------------------------------------------------------ Set g_objJava = CreateObject ("Java.JUtil") ' ------------------------------------------------------------------ ' Access to the file system ' ------------------------------------------------------------------ Set g_objFSO = CreateObject ("Scripting.FileSystemObject") ' ------------------------------------------------------------------ ' A couple of global booleans, one for HTTP redirection and the ' other to determine if you want to see that progress is happening. ' ------------------------------------------------------------------ g_bRedirect = true ' ------------------------------------------------------------------ ' Call the main subroutine ' ------------------------------------------------------------------ call Main() ' ------------------------------------------------------------------ ' Clear up ' ------------------------------------------------------------------ Set g_objJava = Nothing Set g_objFSO = Nothing ' ------------------------------------------------------------------ ' We are finishing now ' ------------------------------------------------------------------ WScript.Quit (0) ' ------------------------------------------------------------------ ' The main subroutine ' ------------------------------------------------------------------ Sub Main ' -------------------------------------------------------------- ' Get an object of the params passed in to this script ' -------------------------------------------------------------- Set objArgs = Wscript.Arguments ' -------------------------------------------------------------- ' Check the arguments passed, only have 1 param in this script ' -------------------------------------------------------------- If objArgs.Count = 1 Then ' ---------------------------------------------------------- ' Determine if the user wants to stop automatic redirects ' ---------------------------------------------------------- If (UCase (objArgs(0)) = UCase ("NoAutoRedirect")) Then g_bRedirect = false Else ' ------------------------------------------------------ ' Tell the user the usage of this script ' ------------------------------------------------------ WScript.Echo "Please pass only 1 param, " &_ """NoAutoRedirect"" for turning off auto redirect" WScript.Quit (1) End If Else ' ---------------------------------------------------------- ' Too many params ' ---------------------------------------------------------- If objArgs.Count > 1 Then ' ------------------------------------------------------ ' Tell the user the usage of this script ' ------------------------------------------------------ WScript.Echo "Please pass only 1 param, " &_ """NoAutoRedirect"" for turning off auto redirect" WScript.Quit (2) End If End If ' -------------------------------------------------------------- ' Get the location of the Favorites Special Folder ' -------------------------------------------------------------- Set objShell = WScript.CreateObject ("WScript.Shell") strFavesPath = objShell.SpecialFolders ("Favorites") ' -------------------------------------------------------------- ' Get the physical folder for the favorites ' -------------------------------------------------------------- Set objRootFolder = g_objFSO.GetFolder (strFavesPath) ' -------------------------------------------------------------- ' Indicate that something is going on, color is rgb value ' -------------------------------------------------------------- g_objJava.setFillColor (100) g_objJava.showProgress "Favorites progress", 250, 50 ' -------------------------------------------------------------- ' Process all folders below this one ' -------------------------------------------------------------- ProcessFolders (objRootFolder) ' -------------------------------------------------------------- ' Indicate that something is going on ' -------------------------------------------------------------- g_objJava.hideProgress ' -------------------------------------------------------------- ' Clear up ' -------------------------------------------------------------- Set g_objShell = Nothing End Sub ' ------------------------------------------------------------------ ' Process a folder, process all files in passed folder then process ' all of the subfolders. ' ------------------------------------------------------------------ Sub ProcessFolders (ByVal objRootFolder) ProcessFiles (objRootFolder) For Each objCurFolder In objRootFolder.SubFolders ProcessFolders (objCurFolder) Next End Sub ' ------------------------------------------------------------------ ' Process files in the passed folder ' ------------------------------------------------------------------ Sub ProcessFiles (ByVal objCurFolder) For Each objCurFile In objCurFolder.Files ' ---------------------------------------------------------- ' Only deal with files that have a .URL extension ' ---------------------------------------------------------- If (UCase (g_objFSO.GetExtensionName (objCurFile)) _ = "URL") Then ' ------------------------------------------------------ ' Open the URL file ' ------------------------------------------------------ Set objURLFile = g_objFSO.OpenTextFile (objCurFile) ' ------------------------------------------------------ ' Go through the file looking at each line ' ------------------------------------------------------ Do While objURLFile.AtEndOfStream <> True strURLLine = objURLFile.ReadLine ' -------------------------------------------------- ' Match line that starts with URL string ' -------------------------------------------------- If (UCase (Left (strURLLine, 3)) = "URL") Then ' ---------------------------------------------- ' Get the URL from the text ' ---------------------------------------------- strURL = Right (strURLLine, Len (strURLLine) - 4) ' ---------------------------------------------- ' Get the response from the web page ' ---------------------------------------------- iResponseCode = g_objJava.getWebPageResponse _ (strURL, g_bRedirect) ' ---------------------------------------------- ' If not a good response code then tell user ' ---------------------------------------------- If (iResponseCode <> 200) Then WScript.Echo _ "------------------------------------" WScript.Echo iResponseCode WScript.Echo objCurFile WScript.Echo strURL WScript.Echo _ "------------------------------------" End If ' ---------------------------------------------- ' Show progress ' ---------------------------------------------- g_objJava.updateProgress End If Loop ' ------------------------------------------------------ ' Close the URL file ' ------------------------------------------------------ objURLFile.Close End If Next End Sub ' ------------------------------------------------------------------ ' End of script ' ------------------------------------------------------------------ Listing Six set objShell = WScript.CreateObject ("WScript.Shell") set objSF = objShell.SpecialFolders For Each Folder In objSF WScript.Echo Folder Next Listing Seven ' ================================================================== ' Script to color filenames based on their extension ' ================================================================== ' ------------------------------------------------------------------ ' Access to the file system ' ------------------------------------------------------------------ Set g_objFSO = CreateObject ("Scripting.FileSystemObject") ' ------------------------------------------------------------------ ' Create instance of Console color COM server ' ------------------------------------------------------------------ Set objColor = WScript.CreateObject ("ConsoleColor.ConsoleColor") ' ------------------------------------------------------------------ ' Call the main subroutine ' ------------------------------------------------------------------ call Main() ' ------------------------------------------------------------------ ' Clear up ' ------------------------------------------------------------------ Set objColor = Nothing Set g_objFSO = Nothing ' ------------------------------------------------------------------ ' We are finishing now ' ------------------------------------------------------------------ WScript.Quit (0) ' ------------------------------------------------------------------ ' The main subroutine ' ------------------------------------------------------------------ Sub Main ' -------------------------------------------------------------- ' Get an object of the params passed in to this script ' -------------------------------------------------------------- Set objArgs = Wscript.Arguments ' -------------------------------------------------------------- ' Default to the current directory ' -------------------------------------------------------------- strParam = "." ' -------------------------------------------------------------- ' Check the arguments passed, if 1 then take it ' -------------------------------------------------------------- If objArgs.Count >= 1 Then strParam = objArgs(0) End If ' -------------------------------------------------------------- ' Blank line to output ' -------------------------------------------------------------- WScript.Echo "" ' -------------------------------------------------------------- ' Save the current color settings ' -------------------------------------------------------------- wVal = objColor.GetCurrentColor() ' -------------------------------------------------------------- ' Get the physical folder for the favorites ' -------------------------------------------------------------- Set objCurFolder = g_objFSO.GetFolder (strParam) ' -------------------------------------------------------------- ' Holder for the longest filename so that can line them up ' -------------------------------------------------------------- iCurFileLen = 0 ' -------------------------------------------------------------- ' For each folder in the obtained folder work out longest name ' -------------------------------------------------------------- For Each aFolder In objCurFolder.SubFolders If (Len (aFolder .Name) > iCurFileLen) Then iCurFileLen = Len (aFolder.Name) End If Next ' -------------------------------------------------------------- ' For each file in the obtained folder work out longest name ' -------------------------------------------------------------- For Each objCurFile In objCurFolder.Files If (Len (objCurFile.Name) > iCurFileLen) Then iCurFileLen = Len (objCurFile.Name) End If Next ' -------------------------------------------------------------- ' For each folder in the obtained folder ' -------------------------------------------------------------- For Each aFolder In objCurFolder.SubFolders ' ---------------------------------------------------------- ' Get the folder attributes ' ---------------------------------------------------------- strAttr = getAttribute (aFolder.Attributes) ' ---------------------------------------------------------- ' Store the name ' ---------------------------------------------------------- strFileName = aFolder.Name ' ---------------------------------------------------------- ' Add spaces to the end of the name so all the same length ' ---------------------------------------------------------- For iIndex = Len(aFolder.Name) To iCurFileLen + 1 strFileName = strFileName + " " Next ' ---------------------------------------------------------- ' Output the information we have on the folder ' ---------------------------------------------------------- WScript.Echo strAttr & aFolder.DateLastModified &_ vbtab & strSize & vbtab &_ strFileName & aFolder.Type Next ' -------------------------------------------------------------- ' For each file in the obtained folder ' -------------------------------------------------------------- For Each objCurFile In objCurFolder.Files ' ---------------------------------------------------------- ' Change the colors back to original ' ---------------------------------------------------------- objColor.SetColor (wval) ' ---------------------------------------------------------- ' Depending upon the extension color the output, you can ' choose to do this a more extensible way using arrays ' if you like. ' ---------------------------------------------------------- strExtension = g_objFSO.GetExtensionName (objCurFile) If (UCase (strExtension) = "VBS") Then objColor.SetForegroundColor (12) ElseIf (UCase (strExtension) = "DOC") Then objColor.SetForegroundColor (14) ElseIf (UCase (strExtension) = "JAVA") Then objColor.SetForegroundColor (10) ElseIf (UCase (strExtension) = "TXT") Then objColor.SetForegroundColor (14) ElseIf (UCase (strExtension) = "BAT") Then objColor.SetForegroundColor (11) ElseIf (UCase (strExtension) = "SYS") Then objColor.SetForegroundColor (13) ElseIf (UCase (strExtension) = "DLL") Then objColor.SetForegroundColor (15) ElseIf (UCase (strExtension) = "CLASS") Then objColor.SetForegroundColor (9) ElseIf (UCase (strExtension) = "COM") Then objColor.SetForegroundColor (12) ElseIf (UCase (strExtension) = "ZIP") Then objColor.SetForegroundColor (11) End If ' ---------------------------------------------------------- ' Get the file attributes ' ---------------------------------------------------------- strAttr = getAttribute (objCurFile.Attributes) ' ---------------------------------------------------------- ' Store the name ' ---------------------------------------------------------- strFileName = objCurFile.Name ' ---------------------------------------------------------- ' Add spaces to the end of the name so all the same length ' ---------------------------------------------------------- For iIndex = Len(objCurFile.Name) To iCurFileLen + 1 strFileName = strFileName + " " Next ' ---------------------------------------------------------- ' Format the size to have the thousand separator ' ---------------------------------------------------------- strSize = FormatNumber (objCurFile.Size, 0, false, false, true) ' ---------------------------------------------------------- ' Output the information we have on the file ' ---------------------------------------------------------- WScript.Echo strAttr & objCurFile.DateLastModified &_ vbtab & strSize & vbtab &_ strFileName & objCurFile.Type Next ' -------------------------------------------------------------- ' Change the colors back to original ' -------------------------------------------------------------- objColor.SetColor (wval) End Sub ' ------------------------------------------------------------------ ' Format a string for the given attribute value ' 1 = read only r ' 2 = hidden h ' 4 = system s ' 8 = volume v ' 16 = directory d ' 32 = archive a ' 2048 = compressed (NT Only) c ' ' Output will be in the form "[ cadshr ]" ' ------------------------------------------------------------------ Function getAttribute (iAttr) strAttr = "[ " If (iAttr AND 2048) Then strAttr = strAttr + "c" else strAttr = strAttr + " " End If If (iAttr AND 32) Then strAttr = strAttr + "a" else strAttr = strAttr + " " End If If (iAttr AND 16) Then strAttr = strAttr + "d" else strAttr = strAttr + " " End If If (iAttr AND 8) Then strAttr = strAttr + "v" else strAttr = strAttr + " " End If If (iAttr AND 4) Then strAttr = strAttr + "s" else strAttr = strAttr + " " End If If (iAttr AND 2) Then strAttr = strAttr + "h" else strAttr = strAttr + " " End If If (iAttr AND 1) Then strAttr = strAttr + "r" else strAttr = strAttr + " " End If strAttr = strAttr + "] " getAttribute = strAttr End Function ' ------------------------------------------------------------------ ' End of script ' ------------------------------------------------------------------ Listing Eight objDevStudio.Documents.Open objArgs(0) Set objProjects = objDevStudio.Projects If (objProjects.Count = 0) Then WScript.Echo "No current project" Else For Each aProject in objProjects WScript.Echo aProject.Name WScript.Echo aProject.Type Next End If Listing Nine ' ================================================================== ' Script to illustrate use of automating DevStudio ' ================================================================== ' ------------------------------------------------------------------ ' Call the main subroutine ' ------------------------------------------------------------------ call Main() ' ------------------------------------------------------------------ ' We are finishing now ' ------------------------------------------------------------------ WScript.Quit (0) ' ------------------------------------------------------------------ ' The main subroutine ' ------------------------------------------------------------------ Sub Main ' -------------------------------------------------------------- ' Get an object of the params passed in to this script ' -------------------------------------------------------------- Set objArgs = Wscript.Arguments ' -------------------------------------------------------------- ' Check the arguments passed, only have 1 param in this script ' -------------------------------------------------------------- If objArgs.Count <> 1 Then WScript.Echo "Please pass only 1 param, the Developer " &_ "Studio project (dsw) to open" WScript.Quit (1) End If ' -------------------------------------------------------------- ' Create an instance of Developer Studio automation server ' -------------------------------------------------------------- Set objDevStudio = CreateObject ("MSDev.Application") ' -------------------------------------------------------------- ' We don't want it to be visible, make it visible for debugging ' -------------------------------------------------------------- objDevStudio.Visible = false ' -------------------------------------------------------------- ' Open the specified project ' -------------------------------------------------------------- objDevStudio.Documents.Open objArgs(0) ' -------------------------------------------------------------- ' Get the projects object ' -------------------------------------------------------------- Set objProjects = objDevStudio.Projects ' -------------------------------------------------------------- ' Check that we have some projects ' -------------------------------------------------------------- If (objProjects.Count = 0) Then WScript.Echo "No current project" Else ' ---------------------------------------------------------- ' For each of the projects ' ---------------------------------------------------------- For Each aProject in objProjects ' ------------------------------------------------------ ' Output name and type ' ------------------------------------------------------ WScript.Echo aProject.Name WScript.Echo aProject.Type ' ------------------------------------------------------ ' If a buildable project output condifurations ' ------------------------------------------------------ If (aProject.Type = "Build") Then For Each myConfiguration in aProject.Configurations WScript.Echo myConfiguration.Name Next End If Next End If ' -------------------------------------------------------------- ' Get the current documents ' -------------------------------------------------------------- Set objDocuments = objDevStudio.Documents ' -------------------------------------------------------------- ' Check that we have some documents open ' -------------------------------------------------------------- If (objDocuments.Count = 0) Then WScript.Echo "No documents" Else ' ---------------------------------------------------------- ' Output the name and type of the document ' ---------------------------------------------------------- For Each aDoc in objDocuments WScript.Echo aDoc.Name WScript.Echo aDoc.Type Next End If ' -------------------------------------------------------------- ' Close developer studion and Clean up ' -------------------------------------------------------------- objDevStudio.Quit Set objDevStudio = Nothing End Sub ' ------------------------------------------------------------------ ' End of script ' ------------------------------------------------------------------ 15