設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 224|回復: 0
打印 上一主題 下一主題

A PowerShell script to monitor a folder and executing Cypher when a new file

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2023-12-16 04:22:41 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
$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{
        
        }
    }
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-17 01:40 , Processed in 0.200964 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表