0

In the code below, I am searching through an .ini file and trying to replace certain lines with updated configurations. I've seen it done for single lines, but I am trying to do it for multiple & different lines. When trying to use a backtick, I get Unexpected token '^' in expression or statement..

$layout_choice = "TEST"
$store_pics_by_date_choice = "TEST"
$orientation_choice = "TEST"
$copy_strips_choice = $backup_path
$copy_pics_choice = "no"
$copy_gifs_choice = $backup_path
$copy_videos_choice = "no"
$viewer_monitor_choice = "TEST"

get-content $file | ForEach-Object{$_ -replace "^layout =.+$", "layout = $layout_choice"` 
-replace "^store_pics_by_date =.+$", "store_pics_by_date = $store_pics_by_date_choice"
-replace "^orientation =.+$", "orientation = $orientation_choice"
-replace "^copy_strips =.+$", "copy_strips = $copy_strips_choice"
-replace "^copy_gifs =.+$", "copy_gifs = $copy_gifs_choice"
-replace "^copy_pics =.+$", "copy_pics = $copy_pics_choice"
-replace "^copy_videos =.+$", "copy_videos = $copy_videos_choice"
-replace "viewer_monitor =.+", "viewer_monitor = $viewer_monitor_choice"
-replace "viewer_monitor =.+", "viewer_monitor = $viewer_monitor_choice"} | Set-Content ($newfile)
1
  • Is there a space after the backtick? Commented Mar 17, 2014 at 0:30

1 Answer 1

4

The backtick is simply an escape character. It works for line continuation by escaping the carriage return.

I would recommend looking for an alternative approach: A single line with all the code, breaking the code into multiple lines, or using another solution.

An example of an alternate solution. Slightly more code, but less delicate than using backticks:

#gather up your replace pairs in an array, loop through them
$line = "abcdefg"
$replaceHash = @{
    "^store_pics_by_date =.+$" = "store_pics_by_date = blah"
    "b" = "blah"
    "c" = "cat"
}
foreach($key in $replaceHash.keys)
{
    $line = $line -replace $key, $replaceHash[$key]
}

Using a backtick to continue on the next line is rarely if ever the correct answer - it creates delicate code. Even the answer from Frode F. failed, as there was a character after the first backtick.

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

+1 I agree that it's was not a good solution. It was only an attempt to fix the solution provided by OP. :) The space behind the backtick must have appeared when I copy/pasted the code.
Thanks for helping all. I like the idea of the hash but I ran into some issues. Let me see what I can work out and I'll reply back if I hit another roadblock.

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.