Skip to content

Instantly share code, notes, and snippets.

@cskardon
Created February 1, 2018 15:12
Show Gist options
  • Save cskardon/c5675bf77c906c8f0b39158551fec4ee to your computer and use it in GitHub Desktop.
Save cskardon/c5675bf77c906c8f0b39158551fec4ee to your computer and use it in GitHub Desktop.
A PowerShell script to monitor a folder and executing Cypher when a new file is added
$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{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment