8

I need a piece of powershell-code to search and replace a certain string inside a text-file. In my example, I want to replace 23-06-2016' with '24-06-2016'. The script below does this job:

$original_file  = 'file.old'
$destination_file   = 'file.new'

(Get-Content $original_file) | Foreach-Object {
$_ -replace '23-06-2016', '24-06-2016' `
} | Out-File -encoding default $destination_file

As the search / replace string change I want to loop over an array of dates which might look like this:

$dates = @("23-06-2016","24-06-2016","27-06-2016")

I tried use the

$original_file  = 'file.old'
$destination_file   = 'file.new'

foreach ($date in $dates) {
  (Get-Content $original_file) | Foreach-Object {
  $_ -replace 'date', 'date++' `
  } | Out-File -encoding default $destination_file
}

In a first step, the date '23-06-2016' should be replaced by '24-06-2016' and in a second step, the date '24-06-2016' should be replaced by '27-06-2016'.

As my script is not working I am seeking for some advice.

0

1 Answer 1

9

You are using $date as your instance variable in your foreach loop but then referencing it as 'date', which is just a string. Even if you used '$date' it would not work because single-quoted strings do not expand variables.

Further, $date is not a number, so date++ would not do anything even it were referenced as a variable $date++. Further still, $var++ returns the original value before incrementing, so you would be referencing the same date (as opposed to the prefix version ++$var).

In a foreach loop, it's not very practical to refer to other elements, in most cases.

Instead, you could use a for loop:

for ($i = 0; $i -lt $dates.Count ; $i++) {
    $find = $dates[$i]
    $rep = $dates[$i+1]
}

This isn't necessarily the most clear way to do it.

You might be better off with a [hashtable] that uses the date to find as a key, and the replacement date as the value. Sure, you'd be duplicating some dates as value and key, but I think I'd rather have the clarity:

$dates = @{
    "23-06-2016" = "24-06-2016"
    "24-06-2016" = "27-06-2016"
}

foreach ($pair in $dates.GetEnumerator()) {
    (Get-Content $original_file) | Foreach-Object {
      $_ -replace $pair.Key, $pair.Value
    } | Out-File -encoding default $destination_file
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you @braintist! There is only some minor error in your definiton of $dates, I guess a semicolon instead of a comma should be used to seperate the pairs from each other: $dates = @{ "23-06-2016" = "24-06-2016"; "24-06-2016" = "27-06-2016" }
@PhilU. whoops, yeah, I updated. In fact when you put them on separate lines as I have, no other separator is needed. If you put key/value pairs on the same line as others, then a semi-colon is used. Good catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.