观看 bat 文件中的特定文件夹。自动提交和推送代码

Watch Specific folder in bat file. commit and push code automatically

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

这是我的代码:

    ### 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}

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

Regex:

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

String.Contains:

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

Wildcard:

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