regex
  • powershell
  • folder
  • watch
  • 2016-07-24 71 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,这样,当新的文件来文件夹,然后这一点。正则表达式开始工作。在目前只有文件夹的手表工作,正则表达式没做什么

    对不起,我英文不好

     相关问题

    • 暂无相关问题^_^