Pregunta

Okay someone suggested that I start a new question because my original question was solved but I have another question which belongs to my problem from before.

My first problem was that I wanted to write text in a txt file using double quotes. That is solved. My next problem / question is how can I work with more than one parameter in Add-Content -Value?

Here is an example:

Add-Content -Value '"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe" "-i C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt" "-o" "C:\Users\Administrator\Desktop\output_dcp\$title"'

In this case parameter $title stands for the title of the video clip I am working on and I do not know the title when I am working on it, that is why I am using this parameter. But when I am running my script, power-shell totally ignores my parameter. So I tried it again with single quotes around the parameter for example:

... "-i C:\Users\Administrator\Desktop\dcp_bearbeitet\'$title'\'$title'.txt" ...

And then power-shell does not even perform my script. So maybe somebody knows how I can work with parameters in Add-Content -Value?

¿Fue útil?

Solución

You have two competing requirements:

  • you want to substitute a variable into the command you are building so you need to use double quotes
  • you want to include double quotes in the output, so you either have to escape them or use single quotes round the string.

One way is to use a here-string, i.e. @"..."@ to delimit the string instead of just using ordinary quotes:

$value = @"
"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe" -i "C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt" -o "C:\Users\Administrator\Desktop\output_dcp\$title"
"@
Add-Content -Value $value

That should work for you. I separated it out from Add-Content not because it wouldn't work in-place, but because there is less scope for confusion to do one thing at a time. Also if you build the value up separately you can add a temporary Write-Host $value to quickly check you have the string exactly as you want it.

Another way is simply to escape the enclosed quotes:

$value = "`"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe`" -i `"C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt`" -o `"C:\Users\Administrator\Desktop\output_dcp\$title`""
Add-Content -Value $value

but that does get messy.

Other options would be to build up the string in small chunks, or to use a different character instead of the double quotes inside the string and then do a replace to turn them into the quotes, but either of these is messy. I would go for the "here-string" solution.

Otros consejos

You can pass arrays into Add-Content.

$string_array = @(
    "`n",
    "line1",
    "line2",
    "line3",
    '"line4"',
    "li`"ne`"5"
)

$string_array | Add-Content file.ext

The parameter -Value can also take an array directly:

Add-Content -path cake.txt -value @('some "stuff"',"more `"backticked`" stuff","hello world")
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top