2015-06-23 29 views
0

我試圖過濾掉所有不以.html結尾的文件。最重要的是,它們都以不同的日期開始,所以只有那些符合日期模式的日期。排除以.html結尾的所有結果

文件名的例子:

2015-06-23 1003 (Tuesday) - Inconsistencies found.html 
2015-06-23 1003 (Tuesday) - Groups without place holder account.txt 
2015-06-23 1003 (Tuesday) - Inactive users.csv 
2015-06-23 0948 (Tuesday) - Inconsistencies found.html 
2015-06-23 0948 (Tuesday) - Description not following the standard.csv 
2015-06-23 0948 (Tuesday) - Groups without place holder account.txt 

預期結果:

# when $d = 2015-06-23 1003 
2015-06-23 1003 (Tuesday) - Groups without place holder account.txt 
2015-06-23 1003 (Tuesday) - Inactive users.csv 

當前正則表達式:

$RegExAttachments = "^{0:00}-{1:00}-{2:00}\s{3:00}{4:00}\s\({5}\).*\." -f $d.Year,$d.Month,$d.Day,$d.Hour,$d.Minute,$d.DayOfWeek 
$Attachments = Get-ChildItem $LogFolder | where {$_.Name -match $RegExAttachments} | foreach {$_.FullName} 
$Attachments 

當前的正則表達式提供了正確的結果,但是,它還包含以HTML結尾的文件,我希望在結果中避免使用該文件。我也嘗試過"^{0:00}-{1:00}-{2:00}\s{3:00}{4:00}\s\({5}\).*\.[^html]",但是它也沒有調出txt files

回答

2

不要使用正則表達式比你必須的更多。 Powershell有足夠的內置功能來過濾諸如文件擴展名之類的微不足道的東西。

用途:

$Attachments = Get-ChildItem $LogFolder | where {($_.extension -ne '.html') -and ($_.Name -match $RegExAttachments)} 

[]字符在正則表達式是不是文字字符串 - [^html]手段不匹配html,無論什麼順序。所以,因爲這個角色類包含一個t

0

你也可以使用下面

Get-ChildItem $LogFolder|where {$_.extension -ne ".html"} |where {$_.Name -match $RegExAttachments} 
不匹配 txt