Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $global:folder = 'c:\temp\imports' # The root folder being monitored.
- $filter = '*.csv' # File types we're looking for.
- $global:archiveFolder = 'c:\temp\imports\archive' # The archive root folder
- $global:cypherShellLocation = 'd:\databases\neo4j\enterprise\neo4j-enterprise-3.3.0\bin\cypher-shell.bat' # The location of the cypher-shell.bat file
- $global:user = "neo4j"
- $global:password = "neo"
- # A watcher to watch
- $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
- # Useful if continually running this in the same session
- Get-EventSubscriber -SourceIdentifier "filecreated" | Unregister-Event
- $global:parsed = New-Object 'System.Collections.Generic.List[String]'
- Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
- # Setup
- $fullPath = $Event.SourceEventArgs.FullPath
- $name = $Event.SourceEventArgs.Name
- if ($global:parsed.Contains($name)){
- return
- }
- $global:parsed.Add($name)
- $changedPath = $fullPath.Replace("\", "/")
- $changeType = $Event.SourceEventArgs.ChangeType
- $timeStamp = $Event.TimeGenerated
- $log = "$folder\log.txt"
- $date = Get-Date
- Write-Host "'$fullPath' was $changeType at $timeStamp" -fore green
- Out-File -FilePath $log -Append -InputObject "[$date] Parsing '$fullPath' ($changeType at $timeStamp)"
- # Generate the command
- $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'']})"'
- $date = Get-Date
- Write-Host "Will Execute: $command" -ForegroundColor Cyan
- Out-File -FilePath $log -Append -InputObject "[$date] Executing: $command"
- # Actually invoke!
- Invoke-Expression $command
- # Log completion
- $date = Get-Date
- Out-File -FilePath $log -Append -InputObject "[$date] '$fullPath' completed import."
- $archivedPath = Join-Path $global:archiveFolder $name
- # Archive the file
- $date = Get-Date
- Copy-Item $fullPath $archivedPath -Force
- Write-Host "Archived $fullPath to $archivedPath" -ForegroundColor Cyan
- Out-File -FilePath $log -Append -InputObject "[$date] Archived $fullPath to $archivedPath"
- # Because neo holds onto files - we can try to delete the file, but it's unlikely to work :(
- try
- {
- Remove-Item $fullPath -ErrorAction Stop
- $global:parsed.Remove($name)
- }
- catch {
- Write-Host "Couldn't delete $fullPath - probably locked by Neo4j LOAD CSV handler - it is safe to delete." -ForegroundColor Red
- }
- Foreach($file in $global:parsed){
- try{
- $tempPath = Join-Path $global:folder $file
- Write-Host " Attempting to remove $tempPath"
- Remove-Item $tempPath -ErrorAction Stop
- }
- catch{
- }
- }
- }
Add Comment
Please, Sign In to add comment