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

Parameterized query in Classic Asp

In my code, this is how I get a recordset from a command: Set rs = server.createobject(“ADODB.Recordset”) Set cmd = server.createobject(“ADODB.Command”) cmd.ActiveConnection = Conn //connection object already created cmd.CommandText = “SELECT * FROM lbr_catmaster where catname = ?” cmd.CommandType = adCmdText cmd.CommandTimeout = 900 set prm = cmd.CreateParameter(“@prm”, 200, 1, 200, “development”) cmd.Parameters.Append prm ‘ … Read more

Launch programs whose path contains spaces

Try:- Dim objShell Set objShell = WScript.CreateObject( “WScript.Shell” ) objShell.Run(“””c:\Program Files\Mozilla Firefox\firefox.exe”””) Set objShell = Nothing Note the extra “”s in the string. Since the path to the exe contains spaces it needs to be contained with in quotes. (In this case simply using “firefox.exe” would work). Also bear in mind that many programs exist … Read more