I am trying to make this script work (having some vba knowledge and no idea about vbs differences. The solution I am trying to adapt is here: VB Script to dump an SQL Server table to CSV and https://msdn.microsoft.com/en-us/library/ms974559.aspx
It is supposed to read csv file, run ACE SQL query on it and return the result to another CSV
It generates only an empty Output.csv file and it is locked for editing. Can you help me out:
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\Databases\"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
rs.Open "SELECT * FROM PhoneList.csv", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile("c:\Databases\output.csv",2,TRUE)
line=""
For Each tmp In objRecordset.Fields
line=line & tmp.Name & ","
Next
ts.WriteLine Left(line,Len(line)-1)
While Not rs.EOF
line=""
For Each tmp In rs.Fields
If IsNull(tmp.Value) Then
line=line & """" & Replace(tmp.Value,"""","""""") & ""","
Else
line=line & """" & tmp.Value & ""","
End If
Next
ts.WriteLine Left(line,Len(line)-1)
rs.MoveNext
Wend
Set rs = Nothing
ts.close
rs.close
fso.close
cn.Close
I understand some VBA but am not able to find my way out in this VBS. Can you help me find out what I am doing wrong?
Secondly... is it possible to make all the paths to data relative to the script itself, so that the solution can be distributed? I know how to do it in vba but have no idea if it is possible with vbscript?
I am sure such a template can be very helpful to all the community. If my question is ill formulated I will humbly accept the critique.