You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackingDataImport.vbs 4.6KB

7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Option Explicit
  2. Dim dev:dev = True
  3. Dim DataDirectory
  4. Dim ConnectionString
  5. Dim glob:set glob = CreateObject("Chilkat_9_5_0.Global")
  6. Dim success:success = glob.UnlockBundle("KENTCM.CB1022025_RGzBPM5J655e")
  7. If (success <> 1) Then
  8. WriteLine(glob.LastErrorText)
  9. WScript.Quit
  10. End If
  11. Dim objCSV:Set objCsv = CreateObject("Chilkat_9_5_0.Csv")
  12. Dim WorkingDirectory:WorkingDirectory = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
  13. If dev Then
  14. DataDirectory = WorkingDirectory & "test"
  15. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=F:\Development\Tracking_Kits\Data\webdata - Copy.mdb;"
  16. Else
  17. DataDirectory = "\\kci-syn-cl01\PC Transfer\TrackingDataImport\"
  18. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=C:\inetpub\Data\webdata - Copy.mdb;"
  19. End If
  20. CheckForFiles
  21. Function CheckForFiles()
  22. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  23. If objFSO.FolderExists(DataDirectory) Then
  24. Dim objFolder:Set objFolder = objFSO.GetFolder(DataDirectory)
  25. If objFolder.Files.Count > 0 Then
  26. WScript.Echo "Files found in directory: " & DataDirectory
  27. Dim objFile
  28. For Each objFile In objFolder.Files
  29. Dim CsvString:CsvString = ConvertCsvToString(objFile.Path)
  30. If ValidImportCSV(CsvString) Then
  31. SetupKit CsvString,objFile.Name
  32. End If
  33. Next
  34. Else
  35. WScript.Echo "No files found in directory: " & DataDirectory
  36. End If
  37. End If
  38. End Function
  39. Function ValidJcode(jcode)
  40. Dim oConn
  41. Set oConn = WScript.CreateObject("ADODB.Connection")
  42. oConn.ConnectionString = ConnectionString
  43. oConn.Open
  44. Dim oRs
  45. set oRs = oConn.Execute("Select * from Jurisdiction Where [JCode] = '" & jcode & "';")
  46. If oRs.EOF Then
  47. ValidJcode = 0
  48. Else
  49. ValidJcode = 1
  50. End If
  51. oRs.Close
  52. oConn.Close
  53. End Function
  54. Function SetupKit(CsvString,FileName)
  55. Dim JobNumber:JobNumber = Mid(FileName,9,6)
  56. Dim JCode:JCode = Left(Filename,5)
  57. If IsNumeric(JobNumber) Then
  58. If ValidJcode(JCode) Then
  59. WScript.Echo FileName & " Is a Valid CSV for Importing"
  60. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  61. oConn.ConnectionString = ConnectionString
  62. oConn.Open
  63. oConn.Execute("Insert Into Kit ([JobNumber], [Jcode], [CreatedOn], [JobType],[Filename],[Status]) VALUES ('" & JobNumber & "','" & JCode & "',#" & Now() & "#,'Purple Envelopes','" & FileName & "','Importing');")
  64. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit ORDER BY ID DESC")
  65. Dim kitId : kitId = rs("ID").value
  66. rs.close
  67. Dim i
  68. For i = 1 To objCSV.NumRows -1
  69. oConn.Execute("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  70. ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  71. ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & kitId & _
  72. ",'" & objCsv.GetCell(i,0) & _
  73. "','" & objCsv.GetCell(i,1) & _
  74. "','" & objCsv.GetCell(i,2) & _
  75. "','" & objCsv.GetCell(i,3) & _
  76. "','" & objCsv.GetCell(i,4) & _
  77. "','" & objCsv.GetCell(i,5) & _
  78. "','" & objCsv.GetCell(i,6) & _
  79. "','" & objCsv.GetCell(i,7) & _
  80. "','" & objCsv.GetCell(i,8) & _
  81. "','" & objCsv.GetCell(i,9) & _
  82. "','" & objCsv.GetCell(i,10) & _
  83. "','" & objCsv.GetCell(i,11) & _
  84. "','" & objCsv.GetCell(i,12) & _
  85. "','" & objCsv.GetCell(i,13) & _
  86. "','" & objCsv.GetCell(i,14) & _
  87. "','" & objCsv.GetCell(i,15) & _
  88. "','" & objCsv.GetCell(i,16) & _
  89. "','" & objCsv.GetCell(i,17) & _
  90. "','" & objCsv.GetCell(i,18) & _
  91. "','" & objCsv.GetCell(i,19) & _
  92. "')")
  93. Next
  94. oConn.Execute("Update Kit SET [Status] = 'Ready to Cass' Where ID = " & kitId &";")
  95. oConn.Close
  96. End If
  97. End If
  98. End Function
  99. Function ConvertCsvToString(FilePath)
  100. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  101. Dim objCsvFile:set objCsvFile = objFSO.OpenTextFile(FilePath)
  102. Dim strContent:strContent = ""
  103. Dim intLineCount:intLineCount = 0
  104. Do Until objCsvFile.AtEndOfStream Or intLineCount >= 3
  105. objCsvFile.SkipLine
  106. intLineCount = intLineCount + 1
  107. Loop
  108. ' Read the remaining content into a string
  109. Do Until objCsvFile.AtEndOfStream
  110. Dim strLine:strLine = objCsvFile.ReadLine
  111. strContent = strContent & strLine & vbCrLf
  112. Loop
  113. ConvertCsvToString = strContent
  114. End Function
  115. Function ValidImportCSV(CsvFileAsString)
  116. objCSV.LoadFromString(CsvFileAsString)
  117. If objCSV.NumColumns = 20 Then
  118. debug.WriteLine objCsv.GetCell(1,1)
  119. ValidImportCSV = True
  120. Else
  121. ValidImportCSV = False
  122. End If
  123. End Function

Powered by TurnKey Linux.