FileSystemObject and reading multiple script files

Goal: A vbscript that will execute multiple script files from directory that include batches with "GO"
Status: The code below allows me to successfully execute ONLY ONE script file, but I need to alter it a bit to loop through multiple script files in a specific directory(folder).
Also, in experimenting, I have several commented out lines of code, but still could not get it to work.
Question: How do I (1) specify the specific folder (2) loop through all the script files within this folder?

Thank you in advance.

'----------------------------------------------------------------------code-----------------
'Use Common.asp include file to get connection string
Dim DBTRACS
Set DBTRACS = new clsDBTRACS
DBTRACS.Open
sConn = DBTRACS.ConnectionString
DBTRACS.Close

Const ForReading = 1, ForWriting = 2
Dim cn, path, folder, File, fso, sConn, intCommentBegin, intCommentEnd, sqlScript, strAfterComment, strBeforeComment, strLine, strToGo, wshShell

'Execute sql script file(s) in directory that contain batches with "GO"
Set fso = CreateObject("Scripting.FileSystemObject")
Set cn = CreateObject("ADODB.Connection")
'test this line
path = "C:\Inetpub\wwwroot\INBOX\JASP1-TRACS-dbo-tbl_contactPOC.sql"
'path = Server.Mappath("/INBOX/") & "\"
'Set folder = fso.GetFolder(path)
cn.ConnectionString = sConn
cn.Open
'For Each File in folder.Files
'Set sqlScript = fso.OpenTextFile(path, ForReading)
Set sqlScript = fso.OpenTextFile(path, ForReading)
'Next
Response.write path
While sqlScript.AtEndOfStream = False
strLine = sqlScript.ReadLine()
If StrComp(Trim(strLine),"go",1) = 0 Then
If Len(Trim(strToGo)) <> 0 Then
cn.Execute strToGo, , adCmdText
strToGo = ""
End If
Else
While InStr(1, strLine, "/*", 1) <> 0
intCommentBegin = InStr(1, strLine, "/*", 1)
strBeforeComment = Trim(Left(strLine, intCommentBegin - 1))
If Len(strBeforeComment) <> 0 Then
strToGo = strToGo & Chr(13) & strBeforeComment
End If
While InStr(intCommentBegin, strLine, "*/", 1) = 0
strLine = strLine & Chr(13) & sqlScript.ReadLine()
Wend
intCommentEnd = InStr(intCommentBegin, strLine, "*/", 1) + 1
strLine = Trim(Right(strLine, Len(strLine) - intCommentEnd))
Wend
strToGo = strToGo & Chr(13) & strLine
End If
Wend
If Len(Trim(strToGo)) <> 0 Then
cn.Execute strToGo, , adCmdText
strToGo = ""
End If

cn.Close

Set cn = Nothing
Set fso = Nothing
jayme9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrGreenCommented:
give this a try, might want to test the lines that get your paths first:

especially this line: Set sqlScript = fso.OpenTextFile(path & objFile.Name, ForReading)

do a reponse.write the end to see what   path & objFile.Name returns



'Use Common.asp include file to get connection string
Dim DBTRACS
Set DBTRACS = new clsDBTRACS
DBTRACS.Open
sConn = DBTRACS.ConnectionString
DBTRACS.Close

Const ForReading = 1, ForWriting = 2
Dim cn, path, folder, File, fso, sConn, intCommentBegin, intCommentEnd, sqlScript, strAfterComment, strBeforeComment, strLine, strToGo, wshShell

'Execute sql script file(s) in directory that contain batches with "GO"
Set fso = CreateObject("Scripting.FileSystemObject")
Set cn = CreateObject("ADODB.Connection")

'test this line
'path = "C:\Inetpub\wwwroot\INBOX\JASP1-TRACS-dbo-tbl_contactPOC.sql"
path = Server.Mappath("/INBOX/") & "\"
Set folder = fso.GetFolder(path)
cn.ConnectionString = sConn
cn.Open

For Each objFile in folder.Files

      Set sqlScript = fso.OpenTextFile(path & objFile.Name, ForReading)      
      
      While sqlScript.AtEndOfStream = False
            strLine = sqlScript.ReadLine()
            If StrComp(Trim(strLine),"go",1) = 0 Then
                  If Len(Trim(strToGo)) <> 0 Then
                  cn.Execute strToGo, , adCmdText
                  strToGo = ""
                  End If
            Else
                        While InStr(1, strLine, "/*", 1) <> 0
                        intCommentBegin = InStr(1, strLine, "/*", 1)
                        strBeforeComment = Trim(Left(strLine, intCommentBegin - 1))
                        If Len(strBeforeComment) <> 0 Then
                        strToGo = strToGo & Chr(13) & strBeforeComment
                        End If
                        While InStr(intCommentBegin, strLine, "*/", 1) = 0
                        strLine = strLine & Chr(13) & sqlScript.ReadLine()
                        Wend
                  intCommentEnd = InStr(intCommentBegin, strLine, "*/", 1) + 1
                  strLine = Trim(Right(strLine, Len(strLine) - intCommentEnd))
            Wend
            strToGo = strToGo & Chr(13) & strLine
            End If
      Wend
      If Len(Trim(strToGo)) <> 0 Then
      cn.Execute strToGo, , adCmdText
      strToGo = ""
      End If

Next

cn.Close

Set cn = Nothing
Set fso = Nothing

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.