Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

232 wiersze
8.3KB

  1. Option Explicit
  2. Dim dev:dev = True
  3. Dim outFile
  4. Dim WaitTime:WaitTime = 15000
  5. Dim DataDirectory
  6. Dim WshShell:Set WshShell = WScript.CreateObject("Wscript.Shell")
  7. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  8. Dim ConnectionString
  9. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  10. Dim glob:set glob = CreateObject("Chilkat_9_5_0.Global")
  11. Dim success:success = glob.UnlockBundle("KENTCM.CB1022025_RGzBPM5J655e")
  12. If (success <> 1) Then
  13. WriteLine(glob.LastErrorText)
  14. WScript.Quit
  15. End If
  16. Dim objCSV:Set objCsv = CreateObject("Chilkat_9_5_0.Csv")
  17. Dim WorkingDirectory:WorkingDirectory = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
  18. If dev Then
  19. DataDirectory = WorkingDirectory & "test"
  20. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=F:\Development\Tracking_Kits\Data\webdata - Copy.mdb;"
  21. set outFile = objFSO.CreateTextFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\temp.csv",True)
  22. Else
  23. DataDirectory = "\\kci-syn-cl01\PC Transfer\TrackingDataImport"
  24. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=C:\inetpub\Data\webdata - Copy.mdb;"
  25. set outFile = objFSO.CreateTextFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\temp.csv",True)
  26. End If
  27. CheckForFiles
  28. Dim KitID:KitID = CheckForJobsToCass()
  29. If KitID > 0 Then
  30. ExportMMCsv(KitID)
  31. RunMailManager
  32. ImportCass
  33. End If
  34. WScript.Quit
  35. Function CheckForFiles()
  36. If objFSO.FolderExists(DataDirectory) Then
  37. Dim objFolder:Set objFolder = objFSO.GetFolder(DataDirectory)
  38. If objFolder.Files.Count > 0 Then
  39. 'WScript.Echo "Files found in directory: " & DataDirectory
  40. Dim objFile
  41. For Each objFile In objFolder.Files
  42. Dim CsvString:CsvString = ConvertCsvToString(objFile.Path)
  43. If ValidImportCSV(CsvString) Then
  44. SetupKit CsvString,objFile.Name
  45. objFSO.MoveFile objFile.Path, DataDirectory & "\import\" & objFile.Name
  46. End If
  47. Next
  48. Else
  49. 'WScript.Echo "No files found in directory: " & DataDirectory
  50. End If
  51. End If
  52. End Function
  53. Function ValidJcode(jcode)
  54. Dim oConn
  55. Set oConn = WScript.CreateObject("ADODB.Connection")
  56. oConn.ConnectionString = ConnectionString
  57. oConn.Open
  58. Dim oRs
  59. set oRs = oConn.Execute("Select * from Jurisdiction Where [JCode] = '" & jcode & "';")
  60. If oRs.EOF Then
  61. ValidJcode = 0
  62. Else
  63. ValidJcode = 1
  64. End If
  65. oRs.Close
  66. oConn.Close
  67. End Function
  68. Function SetupKit(CsvString,FileName)
  69. Dim JobNumber:JobNumber = Mid(FileName,9,6)
  70. Dim JCode:JCode = Left(Filename,5)
  71. If IsNumeric(JobNumber) Then
  72. If ValidJcode(JCode) Then
  73. WScript.Echo FileName & " Is a Valid CSV for Importing"
  74. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  75. oConn.ConnectionString = ConnectionString
  76. oConn.Open
  77. oConn.Execute("Insert Into Kit ([JobNumber], [Jcode], [CreatedOn], [JobType],[Filename],[Status]) VALUES ('" & JobNumber & "','" & JCode & "',#" & Now() & "#,'Purple Envelopes','" & FileName & "','Importing');")
  78. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit ORDER BY ID DESC")
  79. Dim kitId : kitId = rs("ID").value
  80. rs.close
  81. Dim i
  82. For i = 1 To objCSV.NumRows -1
  83. oConn.Execute("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  84. ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  85. ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & kitId & _
  86. ",'" & Replace(objCSV.GetCell(i,0),"'","''") & _
  87. "','" & objCsv.GetCell(i,1) & _
  88. "','" & Replace(objCSV.GetCell(i,2),"'","''") & _
  89. "','" & Replace(objCSV.GetCell(i,3),"'","''") & _
  90. "','" & Replace(objCSV.GetCell(i,4),"'","''") & _
  91. "','" & Replace(objCSV.GetCell(i,5),"'","''") & _
  92. "','" & Replace(objCSV.GetCell(i,6),"'","''") & _
  93. "','" & Replace(objCSV.GetCell(i,7),"'","''") & _
  94. "','" & Replace(objCSV.GetCell(i,8),"'","''") & _
  95. "','" & Replace(objCSV.GetCell(i,9),"'","''") & _
  96. "','" & Replace(objCSV.GetCell(i,10),"'","''") & _
  97. "','" & Replace(objCSV.GetCell(i,11),"'","''") & _
  98. "','" & Replace(objCSV.GetCell(i,12),"'","''") & _
  99. "','" & Replace(objCsv.GetCell(i,13),"'","''") & _
  100. "','" & objCsv.GetCell(i,14) & _
  101. "','" & objCsv.GetCell(i,15) & _
  102. "','" & objCsv.GetCell(i,16) & _
  103. "','" & objCsv.GetCell(i,17) & _
  104. "','" & objCsv.GetCell(i,18) & _
  105. "','" & objCsv.GetCell(i,19) & _
  106. "')")
  107. Next
  108. oConn.Execute("Update Kit SET [Status] = 'Ready to Cass' Where ID = " & kitId &";")
  109. 'oConn.Close
  110. End If
  111. End If
  112. End Function
  113. Function ConvertCsvToString(FilePath)
  114. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  115. Dim objCsvFile:set objCsvFile = objFSO.OpenTextFile(FilePath)
  116. Dim strContent:strContent = ""
  117. Dim intLineCount:intLineCount = 0
  118. Do Until objCsvFile.AtEndOfStream Or intLineCount >= 3
  119. objCsvFile.SkipLine
  120. intLineCount = intLineCount + 1
  121. Loop
  122. ' Read the remaining content into a string
  123. Do Until objCsvFile.AtEndOfStream
  124. Dim strLine:strLine = objCsvFile.ReadLine
  125. strContent = strContent & strLine & vbCrLf
  126. Loop
  127. ConvertCsvToString = strContent
  128. End Function
  129. Function ValidImportCSV(CsvFileAsString)
  130. objCSV.LoadFromString(CsvFileAsString)
  131. If objCSV.NumColumns = 20 Then
  132. debug.WriteLine objCsv.GetCell(1,1)
  133. ValidImportCSV = True
  134. Else
  135. ValidImportCSV = False
  136. End If
  137. End Function
  138. Sub ImportCass
  139. Dim currentRow
  140. objCsv.LoadFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\FROM_MM.CSV")
  141. For currentRow = 0 To objCsv.NumRows -1
  142. oConn.Execute("UPDATE InkJetRecords SET CassADDRESS1 = '" & Replace(objCsv.GetCell(currentRow,1),"'","''") & "', " &_
  143. "CassADDRESS2 = '" & Replace(objCSV.GetCell(currentRow,3),"'","''") & "', " &_
  144. "CassADDRESS3 = '" & Replace(objCSV.GetCell(currentRow,4),"'","''") & "', " &_
  145. "CassADDRESS4 = '" & Replace(objCSV.GetCell(currentRow,5),"'","''") & "', " &_
  146. "CassADDRESS5 = '" & Replace(objCsv.GetCell(currentRow,6) & ", " & objCsv.GetCell(currentRow,7) & " " & objCsv.GetCell(currentRow,8),"'","''") & "'" &_
  147. " WHERE ID = " & objCSV.GetCell(currentRow,0) & ";")
  148. Next
  149. oConn.Execute("UPDATE Kit SET Status ='Ready To Assign STIDS' WHERE ID =" & KitID & ";")
  150. End Sub
  151. Sub RunMailManager
  152. 'WshShell.Run "\\MM2012\APPS\BCC\MM2010\mailman.exe -p -j MMJOB.mjb -u DAN",1,True
  153. WshShell.Run "PsExec64.exe -i -e -u ntp\danielc -p SunBrightShine! \\MarkH2 ""\\MM2012\APPS\BCC\MM2010\mailman.exe"" -p -j MMJOB.mjb -u DAN",1,True
  154. End Sub
  155. Sub ExportMMCsv(KitId)
  156. Dim success
  157. objCsv.HasColumnNames = 1
  158. objCSV.EnableQuotes = 1
  159. success = objCsv.SetColumnName(0,"ID")
  160. success = objCsv.SetColumnName(1,"NAME")
  161. success = objCsv.SetColumnName(2,"ADDRESS1")
  162. success = objCsv.SetColumnName(3,"ADDRESS2")
  163. success = objCsv.SetColumnName(4,"ADDRESS3")
  164. success = objCsv.SetColumnName(5,"ADDRESS4")
  165. success = objCsv.SetColumnName(6,"ADDRESS5")
  166. Dim rs : Set rs = oConn.Execute("Select ID," & _
  167. "IIF(FIRSTNAME IS NULL,'',FIRSTNAME & ' ') & " & _
  168. "IIF(MIDDLENAME IS NULL,'',MIDDLENAME & ' ') & " & _
  169. "IIF(LASTNAME IS NULL,'',LASTNAME & ' ') & " & _
  170. "IIF(SUFFIX IS NULL,'',SUFFIX & ' ') " & _
  171. "AS NAME, ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5 FROM InkjetRecords WHERE KitID =" & KitID & " ORDER By ID;")
  172. Dim CurrentRow:CurrentRow = 0
  173. While Not rs.EOF
  174. objCSV.SetCell CurrentRow,0,rs("ID").value
  175. objCSV.SetCell CurrentRow,1,rs("NAME").value
  176. objCSV.SetCell CurrentRow,2,rs("ADDRESS1").value
  177. objCSV.SetCell CurrentRow,3,rs("ADDRESS2").value
  178. objCSV.SetCell CurrentRow,4,rs("ADDRESS3").value
  179. objCSV.SetCell CurrentRow,5,rs("ADDRESS4").value
  180. objCSV.SetCell CurrentRow,6,rs("ADDRESS5").value
  181. rs.MoveNext
  182. CurrentRow = CurrentRow +1
  183. Wend
  184. Dim CsvString:CsvString = objCSV.SaveToString()
  185. outFile.WriteLine CsvString
  186. outFile.Close
  187. End Sub
  188. Function CheckForJobsToCass()
  189. oConn.ConnectionString = ConnectionString
  190. oConn.Open
  191. 'oConn.Open
  192. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit Where Status ='Ready to Cass' ORDER BY ID DESC;")
  193. If Not rs.EOF Then
  194. Dim kitId : kitId = rs("ID").value
  195. rs.Close
  196. CheckForJobsToCass = KitID
  197. oConn.Execute("UPDATE Kit SET Status ='Cassing' WHERE ID =" & KitID & ";")
  198. Else
  199. CheckForJobsToCass = 0
  200. End If
  201. 'oConn.Close
  202. End Function

Powered by TurnKey Linux.