header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Correct usage of IO.StreamWriter?
Last Post 16 Apr 2008 04:36 PM by Jaykul. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
kelvandUser is Offline
New Member
New Member
Posts:2

--
15 Apr 2008 03:46 PM  

First, hello all; (passing out the virtual apple pies and such)

I'd like to use StreamWriter for for some large data output and came across Mr. Schofield's (slightly modified) sample code for an introduction to using StreamWriter:

$sw = new-object system.IO.StreamWriter("temp.txt")
$sw.writeline("Some sample text")
$sw.close()
get-content "temp.txt"

Powershell v1 runs this without error, but no file is created. .NET doc appears to say that the file must already exist; ok, created file first; still nothing written to the existing file.

Can someone point me to what I'm missing in what should be a simple usage?

smurawskiUser is Offline
New Member
New Member
Posts:46

--
15 Apr 2008 03:56 PM  
I ran this example with v1 and it did create a file in the current directory.

Anything unique in the setup for your environment?
RichSUser is Offline
New Member
New Member
Posts:41

--
15 Apr 2008 08:09 PM  
I tried this with the November CTP for V2 and got the following results.

If I used the first line give in the original post
$sw = new-object system.IO.StreamWriter("temp.txt")
then I duplicated results i.e. no file is created and no output shown

If I changed the first line to
$sw = new-object system.IO.StreamWriter("c:\scripts\temp.txt")
i.e. gave it the full path then it worked perfectly
marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:181

--
16 Apr 2008 02:22 AM  
I tried the command, then searched my entire system. There's particular environment variable named HOMEPATH that you can see from DOS (DOS>set). I'm not sure where it comes from/how it is set, but there's where my temp.txt file ended up. It wasn't in the current directory...
RichSUser is Offline
New Member
New Member
Posts:41

--
16 Apr 2008 09:56 AM  
Nice one Marco

Found the file there.
kelvandUser is Offline
New Member
New Member
Posts:2

--
16 Apr 2008 12:59 PM  
Indeed, StreamWriter uses a full path or it's relative to HOMEPATH (at least in PS v1).

And, combine that with constructor StreamWriter(path, append : boolean), handling overwrites are covered too!

Thanks everyone!
JaykulUser is Offline
New Member
New Member
Posts:31

--
16 Apr 2008 04:36 PM  

Actually, all of the FileSystem IO use the CurrentDirectory.  You can check it using:

[IO.Directory]::GetCurrentDirectory()

And you can set it using

[IO.Directory]::SetCurrentDirectory($pwd)

And yes ... you can add that to your prompt, or your 'cd' function, or whatever.

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer