1

I have a PowerShell script that sets flags based on various conditions of the file. I'll abbreviate for brevity.

$path = "c:\path"
$srcfiles = Get-ChildItem $path -filter *.htm*

ForEach ($doc in $srcfiles) {

$s = $doc.Fullname

Write-Host "Processing :" $doc.FullName

if (stuff) {flag 1 = 1} else {flag 1 = 0}
if (stuff) {flag 1 = 1} else {flag 1 = 0}
if (stuff) {flag 1 = 1} else {flag 1 = 0}

$t = "$s;$flag1;$flag2;$flag2"

Write-Host "Output: " $t

This all works great. My file processes, the flags are set properly, and a neat semicolon delimited line is generated as $t. However, if I slap these two lines at the end of the function,

$stream = [System.IO.StreamWriter] "flags.txt"
$stream.WriteLine $t    

I get this error.

Unexpected token 't' in expression or statement.
At C:\CGC003a.ps1:53 char:25
+     $stream.WriteLine $t <<<<
    + CategoryInfo          : ParserError: (t:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

If I'm reading this write, it appears that write-host flushed my variable $t before it got to the WriteLine. Before I try out-file, though, I want to understand what's happening to $t after Write-Host that prevents Write Line from seeing it as valid. Or is there something else I'm missing?

2 Answers 2

6

try:

$stream.WriteLine($t)   

writeline is a method of streamwriter .net object. To pass in value you need to enclose it in ( )

-if you need to append to a streamwriter you need to create it like this:

$a = new-object 'System.IO.StreamWriter' -ArgumentList "c:\path\to\flags.txt",$true

Where the boolean arguments can be true to append data to the file orfalse to overwrite the file.

I suggest to pass full path for:

$stream = [System.IO.StreamWriter] "c:\path\to\flags.txt"

otherwise you create the file in .net current folder ( probably c:\windows\system32 if run as administrator your current powershell console, to know it type [System.IO.Directory]::GetCurrentDirectory())

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

8 Comments

Awesome! That at least gets me a different error...my first record writes to the file, then I get runtime errors that it's in use by another process...which makes sense. Is there a -force switch for the streamwriter method that'll override that file lock? It doesn't like -append.
Um... streamwriter object needs to be .closed() and eventually .disposed() after used to unlock file. If you had overwrite the $stream variable you need to close powershell and re-open to unlock file. And not oper your stramwriter in the loop, just before the loop, and close it at the and of your script
Yes, I understand about .closing the file...but as it iterates through the foreach ($doc), would I need to close it at the end of each iteration? I would think I'd only need to close the file at the END of all my iterations...what am I missing?
Have your read my previous comment? You have to close the streamwriter out of the loop when you not need write in more data. follow?
Why are you using StreamWriter instead of Out-File anyways?
|
0

you could try

$t > c:\path\to\flags.txt

1 Comment

Whilst this definitely works, it's much less efficient (and thus slower) than using a StreamWriter - especially over a network/remote connection. --- See: blogs.technet.com/b/gbordier/archive/2009/05/05/…

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.