2

I'm pretty new to powershell/programming so bear with me. I have this bug that appends the new renamed path to a new-line without the rest of path. The console output:

/content/pizza/en/ingredients/
helloworld/menu-eng.html

What I want:

/content/pizza/en/ingredients/helloworld/menu-eng.html

What the code below is supposed to do is rename a bunch paths. Right now testName is hard-coded but after I get this to work properly it will be dynamic.

My code:

$testName = "helloworld"
$text = (Get-Content W:\test\Rename\rename.csv) | Out-String
$listOfUri = Import-Csv W:\test\Rename\rename.csv

foreach ($element in $listOfUri) {
    if ($element -match "menu-eng.html") {    
        $elementString = $element.'ColumnTitle' | Out-String
        $elementString = $elementString.Replace('menu-eng.html', '')
        $varPath1 = $elementString

        $elementString = $elementString.Insert('', 'http://www.pizza.com')
        $elementName = ([System.Uri]$elementString).Segments[-1]
        $elementString = $elementString.Replace($elementName, '')
        $elementString = $elementString.Replace('http://www.pizza.com', '')
        $varPath2 = $elementString.Insert($elementString.Length, $testName + '/')

        $text = $text.Replace($varPath1.Trim(), $varPath2)
    }
}
$text 
0

1 Answer 1

3

Assuming your .csv file looks like this:

ColumnTitle,Idk
/content/pizza/en/ingredients/SPAM/menu-eng.html,Stuff

Then:

$testName = 'helloworld'
foreach ($row in Import-CSV d:\rename.csv) {
    $bit = $row.'ColumnTitle'.Split('/')[-2]
    $row.'ColumnTitle'.replace($bit, $testName)
}

I have no real idea what all the rest of your code is for, particularly my earlier comment, your line:

$text = (Get-Content W:\test\Rename\rename.csv) | Out-String

is making $text into an /array/ of all the lines in the file, including the headers. You can still use .Replace() on it in PowerShell, but it's going to do the replace on every line. I can't quite see how that gives you the output you get, but it will give you multiple lines for every line in the input file.

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

2 Comments

My money is definitely on Out-String as the culprit here
You were right, it was the second Out-String causing the problem. If anyone wants to explain why it happened I would love to hear it. Thanks again both of you.

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.