Powershell script to run a .bat file when a file is added to a folder

前端 未结 2 1735
谎友^
谎友^ 2021-01-20 20:10

I\'d like to monitor a Windows 7 folder and have a .bat file run when any new files are added to the folder. It seems like I should be able to do this using powershell, whic

相关标签:
2条回答
  • 2021-01-20 20:46

    you can also use this code to run ps1 file in background instead of invoke-item

    Invoke-Expression -Command "C:....\somescript.ps1"

    0 讨论(0)
  • 2021-01-20 20:56

    Here you go:

    $folder = 'f:\test' # Enter the root path you want to monitor. 
    $filter = '*.html'  # You can enter a wildcard filter here. 
    
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
    write-host "test"
    Invoke-Item 'f:\test\test.bat'
    }
    
    0 讨论(0)
提交回复
热议问题