首页 > 解决方案 > 获取内容中的Powershell多重匹配

问题描述

我想看看是否有任何行包含单词“Newer”和/或“New File”。我试过像下面这样的脚本,但没有运气。任何见解将不胜感激。

RoboCopy.exe  "C:\Folder" "C:\Folder2" *.* /S /E /MIR /MT:8 /X /V /TS /FP /BYTES /ETA /LOG:C:\Output\log\sync.txt

$SourceDir = "C:\Output\log\"
#$GCI_Fiter = '*.txt'
$Include=@("*.txt")

$FileList = Get-ChildItem -LiteralPath $SourceDir -Include "$Include" -File
foreach ($FL_Item in $FileList)   {

$results = Get-Content -Path $FL_Item.FullName | Select-String -pattern '(newer.New File)'

}

Robocopy 日志:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Wednesday, December 25, 2019 8:16:28 PM
   Source : C:\Folder\
     Dest : C:\Folder2\

    Files : *.*

  Options : *.* /V /X /TS /FP /BYTES /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /ETA /MT:8 /R:1000000 /W:30 

------------------------------------------------------------------------------


        New File               1 2019/12/25 17:16:04    C:\Folder\2.txt
              same             0 2019/12/25 11:38:23    C:\Folder\2\1.txt
              same            24 2019/09/25 08:42:17    C:\Folder\5.csv
        Newer                  1 2019/12/25 17:16:04    C:\Folder\4.txt
100%  
              same             0 2019/12/11 08:06:39    C:\Folder\1.txt
              same             0 2019/12/11 08:06:39    C:\Folder\2.txt


------------------------------------------------------------------------------

标签: powershellrobocopy

解决方案


如果将该文件加载到$InStuffwithGet-Content中,则可以使用-match适用于集合的方式。这将为您提供与正则表达式模式匹配的行。像这样 ...

$InStuff -match 'newer|new file'

输出 ...

New File               1 2019/12/25 17:16:04    C:\Folder\2.txt
Newer                  1 2019/12/25 17:16:04    C:\Folder\4.txt

推荐阅读