Guest User

Untitled

a guest
Feb 15th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. $global:folder = 'c:\temp\imports' # The root folder being monitored.
  2. $filter = '*.csv' # File types we're looking for.
  3. $global:archiveFolder = 'c:\temp\imports\archive' # The archive root folder
  4. $global:cypherShellLocation = 'd:\databases\neo4j\enterprise\neo4j-enterprise-3.3.0\bin\cypher-shell.bat' # The location of the cypher-shell.bat file
  5. $global:user = "neo4j"
  6. $global:password = "neo"
  7.  
  8. # A watcher to watch
  9. $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  10.  
  11. # Useful if continually running this in the same session
  12. Get-EventSubscriber -SourceIdentifier "filecreated" | Unregister-Event
  13.  
  14. $global:parsed = New-Object 'System.Collections.Generic.List[String]'
  15.  
  16. Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
  17. # Setup
  18. $fullPath = $Event.SourceEventArgs.FullPath
  19. $name = $Event.SourceEventArgs.Name
  20. if ($global:parsed.Contains($name)){
  21. return
  22. }
  23.  
  24. $global:parsed.Add($name)
  25. $changedPath = $fullPath.Replace("\", "/")
  26. $changeType = $Event.SourceEventArgs.ChangeType
  27. $timeStamp = $Event.TimeGenerated
  28. $log = "$folder\log.txt"
  29. $date = Get-Date
  30. Write-Host "'$fullPath' was $changeType at $timeStamp" -fore green
  31. Out-File -FilePath $log -Append -InputObject "[$date] Parsing '$fullPath' ($changeType at $timeStamp)"
  32.  
  33. # Generate the command
  34. $command = $cypherShellLocation + ' -u ' + $global:user + ' -p ' + $global:password + ' "USING PERIODIC COMMIT 5000 LOAD CSV WITH HEADERS FROM ''file:///' + $changedPath + ''' AS line CREATE (i:Item {Id: line[''Id''], Name: line[''Name'']})"'
  35. $date = Get-Date
  36. Write-Host "Will Execute: $command" -ForegroundColor Cyan
  37. Out-File -FilePath $log -Append -InputObject "[$date] Executing: $command"
  38.  
  39. # Actually invoke!
  40. Invoke-Expression $command
  41.  
  42. # Log completion
  43. $date = Get-Date
  44. Out-File -FilePath $log -Append -InputObject "[$date] '$fullPath' completed import."
  45. $archivedPath = Join-Path $global:archiveFolder $name
  46.  
  47. # Archive the file
  48. $date = Get-Date
  49. Copy-Item $fullPath $archivedPath -Force
  50. Write-Host "Archived $fullPath to $archivedPath" -ForegroundColor Cyan
  51. Out-File -FilePath $log -Append -InputObject "[$date] Archived $fullPath to $archivedPath"
  52. # Because neo holds onto files - we can try to delete the file, but it's unlikely to work :(
  53. try
  54. {
  55. Remove-Item $fullPath -ErrorAction Stop
  56. $global:parsed.Remove($name)
  57. }
  58. catch {
  59. Write-Host "Couldn't delete $fullPath - probably locked by Neo4j LOAD CSV handler - it is safe to delete." -ForegroundColor Red
  60. }
  61. Foreach($file in $global:parsed){
  62. try{
  63. $tempPath = Join-Path $global:folder $file
  64. Write-Host " Attempting to remove $tempPath"
  65. Remove-Item $tempPath -ErrorAction Stop
  66. }
  67. catch{
  68.  
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment