2016-07-24 7 views
0

Сначала я сделал новый сценарий, сделать регулярное выражение:Объединяя два сценария

$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" 

Поэтому в основном я работаю, чтобы узнать, как я могу добавить первый сценарий для второй. я пытался что-то это:

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" 

     $input_path = $name 
     $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 
    ') 

    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

Извините за это. Код ошибки: Select-String: Невозможно связать аргумент с параметром «Путь», поскольку он равен нулю. В строке: 1 char: 21 + select-string -Path $ input_path -Pattern $ regex -List | % {$ _. Матчи} | % {$ _ ... + ~~~~~~~~~~~ + CategoryInfo: InvalidData: (:) [Select-String], ParameterBindingValidationException + FullyQualifiedErrorId: ParameterArgumentValidationErrorNullNotAllowed, Microsoft.PowerShell.Commands.SelectStrin gCommand – Elx

ответ

0

В вашем сценарии регулярных выражений есть одинарные кавычки ('), которые нарушают ваш скриптовый блок. . Заменить их в двойные кавычки (")

+0

Также вы редактируете PowerShell с помощью ISE, такого как PowerShell ISE? Я вставил ваш код в ISE, и ошибка появилась мгновенно. –

+0

Я пробовал силовой корпус ISE. – Elx

+0

Удаленные одинарные кавычки ('), но все же не могу заставить его работать, как я хочу. – Elx

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 

часы Папка работает хорошо, но где я должен двигаться функции регулярных выражений-InOut так, что, когда новые файлы приходят в папку, то это. Regex начать работу на данный момент только папки часов работают и регулярное выражение не делало ничего

Извините за мой плохой английский

Смежные вопросы