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 5.5KB

пре 7 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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]) VALUES ('" & JobNumber & "','" & JCode & "',#" & Now() & "#,'Purple Envelopes');")
  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. 'objCsv.LoadFromString(CsvString)
  68. Dim i
  69. For i = 1 To objCSV.NumRows
  70. 'debug.WriteLine objCsv.GetCell(i,0)
  71. ' debug.WriteLine("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  72. ' ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  73. ' ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & CInt(kitId) & ",'" & objCsv.GetCell(i,0) & _
  74. ' "','" & objCSV.GetCell(i,1) & _
  75. ' "','" & objCsv.GetCell(i,2) & _
  76. ' "','" & objCsv.GetCell(i,3) & _
  77. ' "','" & objCsv.GetCell(i,4) & _
  78. ' "','" & objCsv.GetCell(i,5) & _
  79. ' "','" & objCsv.GetCell(i,6) & _
  80. ' "','" & objCsv.GetCell(i,7) & _
  81. ' "','" & objCsv.GetCell(i,8) & _
  82. ' "','" & objCsv.GetCell(i,9) & _
  83. ' "','" & objCsv.GetCell(i,10) & _
  84. ' "','" & objCsv.GetCell(i,11) & _
  85. ' "','" & objCsv.GetCell(i,12) & _
  86. ' "','" & objCsv.GetCell(i,13) & _
  87. ' "','" & objCsv.GetCell(i,14) & _
  88. ' "','" & objCsv.GetCell(i,15) & _
  89. ' "','" & objCsv.GetCell(i,16) & _
  90. ' "','" & objCsv.GetCell(i,17) & _
  91. ' "','" & objCsv.GetCell(i,18) & _
  92. ' "','" & objCsv.GetCell(i,19) & _
  93. ' "')")
  94. oConn.Execute("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  95. ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  96. ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & kitId & _
  97. ",'" & objCsv.GetCell(i,0) & _
  98. "','" & objCsv.GetCell(i,1) & _
  99. "','" & objCsv.GetCell(i,2) & _
  100. "','" & objCsv.GetCell(i,3) & _
  101. "','" & objCsv.GetCell(i,4) & _
  102. "','" & objCsv.GetCell(i,5) & _
  103. "','" & objCsv.GetCell(i,6) & _
  104. "','" & objCsv.GetCell(i,7) & _
  105. "','" & objCsv.GetCell(i,8) & _
  106. "','" & objCsv.GetCell(i,9) & _
  107. "','" & objCsv.GetCell(i,10) & _
  108. "','" & objCsv.GetCell(i,11) & _
  109. "','" & objCsv.GetCell(i,12) & _
  110. "','" & objCsv.GetCell(i,13) & _
  111. "','" & objCsv.GetCell(i,14) & _
  112. "','" & objCsv.GetCell(i,15) & _
  113. "','" & objCsv.GetCell(i,16) & _
  114. "','" & objCsv.GetCell(i,17) & _
  115. "','" & objCsv.GetCell(i,18) & _
  116. "','" & objCsv.GetCell(i,19) & _
  117. "')")
  118. Next
  119. oConn.Close
  120. End If
  121. End If
  122. End Function
  123. Function ConvertCsvToString(FilePath)
  124. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  125. Dim objCsvFile:set objCsvFile = objFSO.OpenTextFile(FilePath)
  126. Dim strContent:strContent = ""
  127. Dim intLineCount:intLineCount = 0
  128. Do Until objCsvFile.AtEndOfStream Or intLineCount >= 3
  129. objCsvFile.SkipLine
  130. intLineCount = intLineCount + 1
  131. Loop
  132. ' Read the remaining content into a string
  133. Do Until objCsvFile.AtEndOfStream
  134. Dim strLine:strLine = objCsvFile.ReadLine
  135. strContent = strContent & strLine & vbCrLf
  136. Loop
  137. ConvertCsvToString = strContent
  138. End Function
  139. Function ValidImportCSV(CsvFileAsString)
  140. objCSV.LoadFromString(CsvFileAsString)
  141. If objCSV.NumColumns = 20 Then
  142. debug.WriteLine objCsv.GetCell(1,1)
  143. ValidImportCSV = True
  144. Else
  145. ValidImportCSV = False
  146. End If
  147. End Function

Powered by TurnKey Linux.