regex
  • powershell
  • folder
  • watch
  • 2016-07-24 67 views 0 likes 
    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:無法將參數綁定到參數'Path',因爲它爲空。 在線:1 char:21 + select-string -Path $ input_path -Pattern $ regex -List | %{$ _。匹配} | %{$ _ ... + ~~~~~~~~~~~ + CategoryInfo:InvalidData:(:) [選擇字符串],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SelectStrin gCommand – Elx

    回答

    0

    你的正則表達式腳本中有單引號('),它正在打破你的scriptblock。 。用雙引號(「)替換他們

    +0

    您是否正在用PowerShell ISE等ISE編輯您的PowerShell?我將你的代碼粘貼到ISE中,並且錯誤立即顯示出來。 –

    +0

    我在試用Power-Shell 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,這樣,當新的文件來文件夾,然後這一點。正則表達式開始工作。在目前只有文件夾的手錶工作,正則表達式沒做什麼

    對不起,我英文不好

    相關問題