在bat文件中监视特定文件夹。自动提交和推送代码

时间:2018-04-10 16:10:28

标签: git powershell windows-10 file-watcher

我正在使用powershell代码观看特定文件夹。但是我怎样才能逃脱像.git这样的文件夹和像log.txt这样的文件?就像我正在观看所有更改并在此文件夹名称中创建事件' test'。但每当我保存更改事件时,它会提交并将其推送到bitbucket。同时.git文件夹也会因提交和推送而触发更改事件。这就形成了循环。

这是我的代码:

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "path of test folder"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

    ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { 
    $path = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $logline = "$(Get-Date), $changeType, $path"
    Write-Host $path
    if ($path -contains '\.git\') { 

    }
    else{
    Add-content "\log.txt" -value $logline
cd test
    git add .
    git commit -am "made auto changes"
    git push origin master
    }
    }    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED

    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

1 个答案:

答案 0 :(得分:1)

根据您的示例,您有几个选项可以过滤字符串(所有这些都假设$path[System.String]类型,而不是[System.IO.FileInfo][System.IO.DirectoryInfo]):< / p>

Regex

if ($path -notmatch '\\\.git\\') {

String.Contains

if (-not $path.Contains('\.git\') {

Wildcard

if ($path -notlike '*\.git\*') {