0

SOLUTION on the bottom .

I find my self how to use

($openfiledialog1.ShowDialog() -eq 'OK')
{
    $textboxFile.Text = $openfiledialog1.FileName
}

I can extract and change the text inside of this file and save at the same path.

I would like to save in another place and I tried some thing

(Get-Content $textboxFile.Text).Replace('$Name', 'test') |
    Out-File $HOME\$openfiledialog1.SafeFileNames

I tried with Set-Content same issue, the script can't save and error appear.

ERROR: Set-Content : Could not open the alternate data stream ' Title: , FileName: C:\Users\Administrator.SERVERAD\Documents\test.txt' of the file
ERROR: 'C:\Users\Administrator.SERVERADSystem.Windows.Forms.OpenFileDialog'. MainForm.psf (24, 59):
ERROR: At Line: 24 char: 59
ERROR: + ... xt).replace('$Name', 'test')|Set-Content -Path $HOME\$openfiledialog1

The final usage is change some data inside of HTML file.

Ansgar I don't understand your solution but you keep me on the good way thank you so much. After 2 hours and some break I find a SOLUTION, but I don't why I can't use directly $openfiledialog1.SafeFileName.

$buttonBrowse_Click = {
    if ($openfiledialog1.ShowDialog() -eq 'OK') {
        $textboxFile.Text = $openfiledialog1.SafeFileName
    }
}

$button1_Click = {
    $b = $textboxFile.Text 
    # also this things work too $b=$openfiledialog1.SafeFileName
    # TODO: Place custom script here
    (Get-Content $openfiledialog1.FileName).replace('$Name', "text") |
        Out-File $HOME\$b
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
lol lol
  • 71
  • 6
  • Split your action into 3 individual steps (read, replace, write). Which of them is throwing the error, and what does the error say exactly? – Ansgar Wiechers Jul 14 '18 at 22:25
  • This error ,I thing the script try to change the tittle . ERROR: Set-Content : Could not open the alternate data stream ' Title: , FileName: C:\Users\Administrator.SERVERAD\Documents\test.txt' of the file ERROR: 'C:\Users\Administrator.SERVERAD\System.Windows.Forms.OpenFileDialog'. MainForm.psf (24, 59): ERROR: At Line: 24 char: 59 ERROR: + ... xt).replace('$Name', 'test')|Set-Content -Path $HOME\$openfiledialog1 – lol lol Jul 14 '18 at 22:44
  • this ligne work `(Get-Content $openfiledialog1.filename).replace('$Name','test')|Set-Content $openfiledialog1.filename` but is the same path . – lol lol Jul 14 '18 at 22:46
  • Ansgar if i understood ,I extract the text in variable and then I can do use `replace` and then I can save ? i will try . – lol lol Jul 14 '18 at 22:48
  • Please [edit] your question. Do not bury this information in comments. Also, please remove the custom error formatting from your script, so that we can see the actual error message. – Ansgar Wiechers Jul 14 '18 at 22:49
  • 1
    Something about `$HOME` or `$openfiledialog1.SafeFileNames` is not right. Check both values. Also try something like `$filename = $openfiledialog1.SafeFileNames; ... | Set-Content "$HOME\$filename"`. – Ansgar Wiechers Jul 14 '18 at 22:53
  • if i use `(Get-Content $openfiledialog1.filename).replace('$Name', 'test')|Out-File $HOME\text.txt` that's perflectly fine .My issue is not totally solved because If I want to change lot of file .I am limited .I will try your option . – lol lol Jul 14 '18 at 23:11
  • Thank ansgar for your help .I find a solution . – lol lol Jul 15 '18 at 00:21

1 Answers1

1

tl;dr

In order to reference property value $openfiledialog1.SafeFileName as part of the file-path argument, you must enclose it in $(...):

... | Out-File "$HOME\$($openfiledialog1.SafeFileName)" # Note the $(...)

You could omit the enclosing ", but I suggest using them for conceptual clarity and general robustness.


As for what you tried:

$HOME\$openfiledialog1.SafeFileNames isn't expanded the way you expect:

.SafeFileNames is treated as a literal, meaning that $openfiledialog1 by itself is expanded, which is not your intent.

Here's a simple example:

PS> Write-Output $HOME\$PSVersionTable.PSVersion
C:\Users\jdoe\System.Management.Automation.PSVersionHashTable.PSVersion

You may have expected something like C:\Users\jdoe\5.1.16299.492, but as you can see, .PSVersion was retained as a literal, and $PSVersionTable by itself was stringified, which resulted in the (unhelpful) System.Management.Automation.PSVersionHashTable representation.

The reason for this behavior is that an unquoted token such as $HOME\$openfiledialog1.SafeFileName is treated much like an expandable string, i.e. like a token enclosed in "...".

Inside "...", only mere variable references can be embedded as-is (e.g., "Value: $var"); by contrast, accessing a variable's property is an expression that must be enclosed in $(...), i.e. a call to the subexpression operator (e.g., "Value: $($var.Foo)")

For an overview of string-expansion rules, see this answer of mine.

For an overview of how unquoted tokens are parsed as command arguments, see this answer of mine.

mklement0
  • 382,024
  • 64
  • 607
  • 775