user6888062
user6888062

Reputation: 373

How to create loop in VBS?

I have a VBS script that connects to a FoxPro database.

Dim oCN : Set oCN = CreateObject("ADODB.CONNECTION")
oCN.Open sCS
Dim oRS : Set oRS = oCN.Execute("SELECT SN_ANALSYS, SN_CRLIM, SN_CURRBAL FROM " & WScript.Arguments(0) & "_SNAME.DBF WHERE SN_ANALSYS != '' ORDER BY SN_ANALSYS ASC")
Do Until oRS.EOF
   WScript.Echo oRS.Fields(0).value, "50", oRS.Fields(1).Value
   WScript.Echo oRS.Fields(0).value, "51", oRS.Fields(2).Value
   oRS.MoveNext
Loop
oCN.Close

I run it via a BAT:

C:\query.vbs A > \\share\results.txt

The A is used in the query (WScript.Arguments(0)). However I want to be able to do this:

C:\query.vbs A,D > \\share\results.txt

So that it runs 2 queries using A and D but the results go to the same results.txt.

Upvotes: 6

Views: 393

Answers (1)

Tomalak
Tomalak

Reputation: 338138

Use the WScript.Arguments.Unnamed collection.

Dim arg

For Each arg in WScript.Arguments.Unnamed
    ' use arg in the SQL query
Next

and call without a comma, so that cmd.exe recognizes them as separate arguments:

C:\query.vbs A D > \\share\results.txt

Upvotes: 3

Related Questions