簡體   English   中英

文本文件中的Powershell ForEach-Object行拆分比較和刪除

[英]Powershell ForEach-Object lines in a text file Split compare and delete

我正在使用Powershell腳本工作,該腳本可以將文本文件中的行放入列表框中,但僅以日期../../....開頭的行有效。 但是id喜歡先從文本文件中刪除較舊的行。 我試過了,但不正確。 誰能幫我正確編碼。

我將重新表述這個問題,以期使其更加清楚。

文本文件如下所示:

text.txt

//Regular Roster | Only update if making a permanent change.
!The order for the entry must be: Day time,number,name,email(optional)
!With a comma ‘,’ separating the values.

Monday 9AM-2PM,0400499449,BILL
Monday 2PM-6PM,074477464,Terry
Monday 6PM-9PM,040484747,Leanne

Tuesday 9AM-2PM,07123456,BILL
Tuesday 2PM-6PM,054647383,Barbara
Tuesday 6PM-9PM,03937464,Mandy

//Changes to Regular Roster. This section will override the above.
!The date order of the entries below is not important but each line must follow this pattern:
!date(Dayxx/Monthxx/Yearxxxx),time(9AM,2PM or 6PM),number,name,email(optional)
!Date slash must be forward facing '/'.
!The only times that can be entered are 9AM,2PM or 6PM.
!With a comma ‘,’ separating the values.

01/01/2019,6AM,0400012345,Kurt,kurt@outlook.com
02/01/2019,6AM,0412345676,Bill,bill@outlook.com
03/01/2019,6AM,0400012345,Sam,Sam@outlook.com
04/01/2019,6AM,0412345676,Barry,barry@outlook.com
05/01/2019,6AM,0400012345,Kurt,kurt@outlook.com

如果我假設當前日期為04/01/2019(2019年1月4日),則在Powershell中嘗試此代碼,則可以正常工作

$CurrentDate = "04/01/2019"
$Text = "05/01/2019,6AM,0400012345,Kurt,kurt@outlook.com"
$Text | ForEach-Object {
    $Roster1Date,$Roster2Time,$Roster3Number,$Roster4Name,$Roster5Email = $_.split(",")
    $Newdate = "$Roster1Date"
    $CurrentDate = [datetime]::ParseExact("$CurrentDate", 'dd/MM/yyyy', $null)
    $Newdate = [datetime]::ParseExact("$Newdate", 'dd/MM/yyyy', $null)
    if ($Newdate -ge $CurrentDate) {
     write-output "NewDate is bigger than or equal to"
    }
    else {
      write-output "CurrentDate is bigger"
    }
};

當我嘗試使用此行構建列表框時,它可以正常工作

#Add Listbox
$ShiftsListBox = New-Object System.Windows.Forms.ListBox
$ShiftsListBox.Location = New-Object System.Drawing.Size(400,40)
$ShiftsListBox.Size = New-Object System.Drawing.Size(360,20)
$ShiftsListBox.Height = 160
Get-Content $RosterPath | select-string -Pattern '../../...' | ForEach-Object {[void] $ShiftsListBox.Items.Add($_)};
$FormCreateNewShift.Controls.Add($ShiftsListBox)

當我嘗試將兩者結合時,它只是一片空白

#Add Listbox
$TextFile = text.txt
$ShiftsListBox = New-Object System.Windows.Forms.ListBox
$ShiftsListBox.Location = New-Object System.Drawing.Size(400,40)
$ShiftsListBox.Size = New-Object System.Drawing.Size(360,20)
$ShiftsListBox.Height = 160
Get-Content $TextFile | select-string -Pattern '../../...' | ForEach-Object {
    $Roster1Date,$Roster2Time,$Roster3Number,$Roster4Name,$Roster5Email = $_.split(",")
    $Newdate = "$Roster1Date"
    $CurrentDate = [datetime]::ParseExact("$CurrentDate", 'dd/MM/yyyy', $null)
    $Newdate = [datetime]::ParseExact("$Newdate", 'dd/MM/yyyy', $null)
    if ($Newdate -ge $CurrentDate) {
     [void] $ShiftsListBox.Items.Add($_)
    }
    else {
    }
};
$FormCreateNewShift.Controls.Add($ShiftsListBox)

想法是,如果日期大於或等於當前日期,則在列表框中顯示該行,否則不顯示。 但隨后,我將嘗試通過添加替換行來刪除該行。

#Add Listbox
$TextFile = text.txt
$ShiftsListBox = New-Object System.Windows.Forms.ListBox
$ShiftsListBox.Location = New-Object System.Drawing.Size(400,40)
$ShiftsListBox.Size = New-Object System.Drawing.Size(360,20)
$ShiftsListBox.Height = 160
Get-Content $TextFile | select-string -Pattern '../../...' | ForEach-Object {
    $Roster1Date,$Roster2Time,$Roster3Number,$Roster4Name,$Roster5Email = $_.split(",")
    $Newdate = "$Roster1Date"
    $CurrentDate = [datetime]::ParseExact("$CurrentDate", 'dd/MM/yyyy', $null)
    $Newdate = [datetime]::ParseExact("$Newdate", 'dd/MM/yyyy', $null)
    if ($Newdate -ge $CurrentDate) {
     [void] $ShiftsListBox.Items.Add($_)
    }
    else {
          ((Get-Content -path $TextFile) -replace $_,"") | Set-Content -Path $TextFile;
    }
};
$FormCreateNewShift.Controls.Add($ShiftsListBox)

基於新的示例文件,此腳本可以區分

  • 在行首之間的行之間
    • 從今天或更晚的日期開始(保留)
    • 今天之前的日期(跳過)
  • 其他行(保持)

## Q:\Test\2019\01\01\SO_53992011.ps1
$Rosterpath = ".\rosterpath"

$CurrentDate = (Get-Date).Date  # to have a datetime starting today at 00:00:00

$RegEx = [RegEx]"^(?<day>\d{2})\/(?<mon>\d{2})\/(?<year>\d{4})"

$UpdatedContent = Get-Content $RosterPath | ForEach-Object {
    If ($_ -match $RegEx){
        If((Get-Date -Year $Matches.year -Month $Matches.mon -Day $Matches.day) -ge $CurrentDate){
            # yes keep this line
            $_
        } else { 
            #drop this line
        }
    } else {
        # line without a date, keep
        $_
    }
}

$UpdatedContent | Set-Content $RosterPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM