加入两个脚本

时间:2016-07-24 16:56:23

标签: regex powershell directory watch

首先我创建了一个新的脚本来制作正则表达式:

 public static string Flatten(this HttpHeaders headers)
 {
      var data = headers.ToDictionary(h => h.Key, h => string.Join("; ", h.Value))
                        .Select(kvp => $"{kvp.Key}: {kvp.Value}");

      return string.Join(Environment.NewLine, data)
 }

到目前为止它的工作效果。

我在网上找到的第二个脚本(也很好):

$input_path = 'C:\site-download\input.txt'
$output_file = 'C:\site-download\output.txt'
$regex = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex2 = '(?<month>Winrate<\/span><span class=""right"">\d{1}.\d{1})'
$regex3 = '(?<month>PFR<\/span><span class=""right"">\d{2}.\d{1})'
$regex4 = '(?<month>Winnings<\/span><span class=""right"">\-[0-9]{1,9})'
$regex5 = '(?<month>3Bet<\/span><span class=""right"">\d{1}.\d{1})'

Select-String -Path $input_path -Pattern $regex -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
Select-String -Path $input_path -Pattern $regex2 -List | % { $_.Matches} | % { $_.Value } |Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
Select-String -Path $input_path -Pattern $regex3 -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
Select-String -Path $input_path -Pattern $regex4 -List | % { $_.Matches} | % { $_.Value } |Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
Select-String -Path $input_path -Pattern $regex5 -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file

所以基本上我正在努力找出如何将第一个脚本添加到第二个脚本。 我试过这个:

Function Register-Watcher {
    param ($folder)
    $filter = "*.*" #all files
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }

    $changeAction = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')

    Register-ObjectEvent $Watcher "Changed" -Action $changeAction
}

Register-Watcher "C:\site-download"

但是一直有些错误:

Select-String : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:21
+ select-string -Path $input_path -Pattern $regex -List | % { $_.Matches} | % { $_ ...
+                     ~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.Sel‌​ectStringCommand

有人可以帮我修复最后一个脚本吗?

2 个答案:

答案 0 :(得分:0)

您的正则表达式脚本中包含单引号(&#39;),这会破坏您的scriptblock。用双引号替换它们(&#34;)。

答案 1 :(得分:0)

我现在测试的是:

Function Register-Watcher {
    param ($folder)
    $filter = "*.*" #all files
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }

    $changeAction = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')

    Register-ObjectEvent $Watcher "Created" -Action $changeAction
}


 Register-Watcher "C:\site-download"

 Function regex-inout { $input_path = 'C:\site-download\input.txt'
$output_file = "C:\site-download\output.txt"
$regex = "(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})"
$regex2 = "(?<month>Winrate<\/span><span class=""right"">\d{1}.\d{1})"
$regex3 = "(?<month>PFR<\/span><span class=""right"">\d{2}.\d{1})"
$regex4 = "(?<month>Winnings<\/span><span class=""right"">\-[0-9]{1,9})"
$regex5 = "(?<month>3Bet<\/span><span class=""right"">\d{1}.\d{1})"

select-string -Path $input_path -Pattern $regex -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
select-string -Path $input_path -Pattern $regex2 -List | % { $_.Matches} | % { $_.Value } |Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
select-string -Path $input_path -Pattern $regex3 -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
select-string -Path $input_path -Pattern $regex4 -List | % { $_.Matches} | % { $_.Value } |Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file
select-string -Path $input_path -Pattern $regex5 -List | % { $_.Matches} | % { $_.Value } | Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | Add-Content $output_file

} regex-inout

文件夹手表工作正常。但是我应该在哪里移动函数regex-inout,以便当新文件到达文件夹时,这个正则表达式开始工作。目前只有文件夹监视工作和正则表达式没有做任何事情。

抱歉我的英语不好。