VBScript: What is the simplest way to format a string?

Replace (strFormat, “{0}”, value1) Based on your code snip, I’m guessing you believe Replace mutates strFormat directly. It doesn’t work like that; You assign the result to the original variable like this: strFormat = Replace (strFormat, “{0}”, value1) You can also assign to another variable to store the changed results, like this: strFormat2 = Replace … Read more

ADODB.Parameters error ‘800a0e7c’ Parameter object is improperly defined. Inconsistent or incomplete information was provided

Common occurrence is likely you do not have adVarChar defined, so the CreateParameter() method is “improperly defined”. This is just an example of one of the many named constants found in the ADODB Library. A messy approach to dealing with this is to just define the value yourself something like; Const adVarChar = 200 The … Read more

Order of Files collection in FileSystemObject

Is it really too much code to sort? set fso = CreateObject(“Scripting.FileSystemObject”) Set outputLines = CreateObject(“System.Collections.ArrayList”) for each f in fso.GetFolder(“.”).files outputLines.Add f.Name next outputLines.Sort() ‘ 5 lines… For Each outputLine in outputLines set file = fso.GetFolder(“.”).files.item (outputLine&””) Wscript.Echo file.name ‘ TODO: your thing here Next

Lists in VBScript

Get rid of the dictionary and unleash the power of an ArrayList. Option Explicit dim list Set list = CreateObject(“System.Collections.ArrayList”) list.Add “Banana” list.Add “Apple” list.Add “Pear” list.Sort list.Reverse wscript.echo list.Count ‘ –> 3 wscript.echo list.Item(0) ‘ –> Pear wscript.echo list.IndexOf(“Apple”, 0) ‘ –> 2 wscript.echo join(list.ToArray(), “, “) ‘ –> Pear, Banana, Apple EDIT: I … Read more

Run Command Line & Command From VBS

The problem is on this line: oShell.run “cmd.exe /C copy “S:Claims\Sound.wav” “C:\WINDOWS\Media\Sound.wav” Your first quote next to “S:Claims” ends the string; you need to escape the quotes around your files with a second quote, like this: oShell.run “cmd.exe /C copy “”S:\Claims\Sound.wav”” “”C:\WINDOWS\Media\Sound.wav”” ” You also have a typo in S:Claims\Sound.wav, should be S:\Claims\Sound.wav. I also … Read more

ByRef and ByVal in VBScript

Eric Lippert has a great article on using parentheses in VBScript: What do you mean “cannot use parentheses?” Your example illustrates one of the points he mentions, namely: enclosing a ByRef argument in parentheses passes it as ByVal. In short, parentheses in VBScript subroutine calls can be put not only around the argument list, but … Read more

Access network share from within VBScript eg FileSystemObject

OK, I was laboring under a misconception – that FSO would not “pick up” the network credentials established with “NET USE” (or Wscript.Network “MapNetworkDrive”). It turns out that it does, and the following sample code works very nicely (without needing to set up temporary network drives): ServerShare = “\\192.168.3.56\d$” UserName = “domain\username” Password = “password” … Read more

VBscript code to capture stdout, without showing console window

I usually use this: Wscript.echo execStdOut(“ping google.com”) Function execStdOut(cmd) Dim goWSH : Set goWSH = CreateObject( “WScript.Shell” ) Dim aRet: Set aRet = goWSH.exec(cmd) execStdOut = aRet.StdOut.ReadAll() End Function For more advanced commands youc an wrap to comspec (cmd) my res = execStdOut(“%comspec%” & ” /c ” & “””” & “dir /b c:\windows\*.exe” & “””” … Read more

How do I sort arrays using vbscript?

From microsoft Sorting arrays in VBScript has never been easy; that’s because VBScript doesn’t have a sort command of any kind. In turn, that always meant that VBScript scripters were forced to write their own sort routines, be that a bubble sort routine, a heap sort, a quicksort, or some other type of sorting algorithm. … Read more