Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
user6888062 Avatar asked Oct 10 '16 09:10

user6888062


People also ask

What is looping statement in VBScript?

Looping statements are used to run the same block of code a specified number of times. In VBScript we have four looping statements: For... Next statement - runs code a specified number of times.

Do loops continue VBS?

There is no "Continue" in VBScript. Unfortunately VBScript does not have a "continue" statement that works in For or For Each loops. You will need to use an If statement to skip the rest of your loop. VBScript does, however, have the Exit For statement to exit from a For or For Each loop.


1 Answers

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
like image 149
Tomalak Avatar answered Oct 08 '22 00:10

Tomalak