Powershell ForEach-Object lines in a text file Split compare and delete
Im working on a Powershell script that can put lines from a text file into a list box, but only lines that start with a date ../../....
The add list box part is working. But id like to delete older lines from the text file first.
I tried this but it not correct. Can anyone help me code this correctly.
I will rephrase the question to hopefully make it clearer.
The text file look like this:
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
If I try this code in Powershell assuming the current date is 04/01/2019 (4th January 2019) it works fine
$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"
}
};
When I try to build a Listbox with this line it works fine
#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)
When I tried to combine the two it just comes up blank
#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)
The idea is that if the date is greater than or equal to the current date display the line in the list box, else don't. But then I will try to delete the line by adding in a replace line.
#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)
powershell foreach text-files
add a comment |
Im working on a Powershell script that can put lines from a text file into a list box, but only lines that start with a date ../../....
The add list box part is working. But id like to delete older lines from the text file first.
I tried this but it not correct. Can anyone help me code this correctly.
I will rephrase the question to hopefully make it clearer.
The text file look like this:
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
If I try this code in Powershell assuming the current date is 04/01/2019 (4th January 2019) it works fine
$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"
}
};
When I try to build a Listbox with this line it works fine
#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)
When I tried to combine the two it just comes up blank
#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)
The idea is that if the date is greater than or equal to the current date display the line in the list box, else don't. But then I will try to delete the line by adding in a replace line.
#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)
powershell foreach text-files
add a comment |
Im working on a Powershell script that can put lines from a text file into a list box, but only lines that start with a date ../../....
The add list box part is working. But id like to delete older lines from the text file first.
I tried this but it not correct. Can anyone help me code this correctly.
I will rephrase the question to hopefully make it clearer.
The text file look like this:
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
If I try this code in Powershell assuming the current date is 04/01/2019 (4th January 2019) it works fine
$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"
}
};
When I try to build a Listbox with this line it works fine
#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)
When I tried to combine the two it just comes up blank
#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)
The idea is that if the date is greater than or equal to the current date display the line in the list box, else don't. But then I will try to delete the line by adding in a replace line.
#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)
powershell foreach text-files
Im working on a Powershell script that can put lines from a text file into a list box, but only lines that start with a date ../../....
The add list box part is working. But id like to delete older lines from the text file first.
I tried this but it not correct. Can anyone help me code this correctly.
I will rephrase the question to hopefully make it clearer.
The text file look like this:
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
If I try this code in Powershell assuming the current date is 04/01/2019 (4th January 2019) it works fine
$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"
}
};
When I try to build a Listbox with this line it works fine
#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)
When I tried to combine the two it just comes up blank
#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)
The idea is that if the date is greater than or equal to the current date display the line in the list box, else don't. But then I will try to delete the line by adding in a replace line.
#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)
powershell foreach text-files
powershell foreach text-files
edited Jan 2 at 22:26
Kurt Lane
asked Dec 31 '18 at 22:59
Kurt LaneKurt Lane
72210
72210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Based on new sample file, this script distinguishes
- between lines with a leading date
- a date from today or later(keep)
- a date before today (skip)
- other lines (keep)
## Q:Test201911SO_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
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change$UpdatedContent | Set-Content $RosterPath
intoSet-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992011%2fpowershell-foreach-object-lines-in-a-text-file-split-compare-and-delete%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on new sample file, this script distinguishes
- between lines with a leading date
- a date from today or later(keep)
- a date before today (skip)
- other lines (keep)
## Q:Test201911SO_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
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change$UpdatedContent | Set-Content $RosterPath
intoSet-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
add a comment |
Based on new sample file, this script distinguishes
- between lines with a leading date
- a date from today or later(keep)
- a date before today (skip)
- other lines (keep)
## Q:Test201911SO_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
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change$UpdatedContent | Set-Content $RosterPath
intoSet-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
add a comment |
Based on new sample file, this script distinguishes
- between lines with a leading date
- a date from today or later(keep)
- a date before today (skip)
- other lines (keep)
## Q:Test201911SO_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
Based on new sample file, this script distinguishes
- between lines with a leading date
- a date from today or later(keep)
- a date before today (skip)
- other lines (keep)
## Q:Test201911SO_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
edited Jan 2 at 22:55
answered Jan 1 at 0:02
LotPingsLotPings
19.1k61532
19.1k61532
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change$UpdatedContent | Set-Content $RosterPath
intoSet-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
add a comment |
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change$UpdatedContent | Set-Content $RosterPath
intoSet-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
Please don't. I suggest to start over with a new question with a sanitized real world input file, the description what you expect and a Minimal, Complete, and Verifiable example of your script. And delete this Question nobody can/will follow without sieving through all the comments.
– LotPings
Jan 2 at 22:27
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
I'll remove all my comments (and suggest you do the same) and try something different in my answer soon.
– LotPings
Jan 2 at 22:37
1
1
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change
$UpdatedContent | Set-Content $RosterPath
into Set-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
Thank you, I just had a problem if there were not any future shift in the file, If there is only old shifts in the file as above, it doesn't delete them but someone pointed out empty values in a pipeline are skipped over, meaning that nothing is written to the output file, so the output file remains unchanged. You can avoid this issue by passing the variable to the parameter -Value. Change
$UpdatedContent | Set-Content $RosterPath
into Set-Content -Value $UpdatedContent -Path $RosterPath
– Kurt Lane
Jan 29 at 3:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992011%2fpowershell-foreach-object-lines-in-a-text-file-split-compare-and-delete%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown