Windows Vista Forums

File I/O
  1. #1


    MKielman Guest

    File I/O

    All,



    What is the best way to write output to a file rather then the terminal?
    I have played around with set-content and add-content but neither of
    them meet my needs. If i use set-content, then each new line that I
    write to the file, overwrites the previous line, if I use add-content,
    then each time I run the script, it appends it to the file if it already
    exists. Is there a way I can create a handle to a file and specify once
    what I plan to do with that file so that each time I run the script I
    can be sure that I am not appending to a file that already exists unless
    that is what I want to do?

    Thanks,

      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: File I/O

    Unless I misunderstand you this should do what your looking for:

    Command | out-file "c:\logfile.txt" -append


    "MKielman" <mkielman@newsgroups.nospam> wrote in message
    news:uGYGBYY7GHA.4064@TK2MSFTNGP03.phx.gbl...
    > All,
    >
    > What is the best way to write output to a file rather then the terminal? I
    > have played around with set-content and add-content but neither of them
    > meet my needs. If i use set-content, then each new line that I write to
    > the file, overwrites the previous line, if I use add-content, then each
    > time I run the script, it appends it to the file if it already exists. Is
    > there a way I can create a handle to a file and specify once what I plan
    > to do with that file so that each time I run the script I can be sure that
    > I am not appending to a file that already exists unless that is what I
    > want to do?
    >
    > Thanks,



      My System SpecsSystem Spec

  3. #3


    Jim Holbach Guest

    RE: File I/O

    I like functions, so here's one possible approach using a function:

    function do-something {
    param ($filepath,[switch]$overwrite, $scriptblock)

    if ($Overwrite -and (test-path $filepath)) {clear-content $filepath}
    &$scriptblock | add-content $filepath
    }

    ---
    Jim Holbach


    "MKielman" wrote:

    >
    > What is the best way to write output to a file rather then the terminal?



      My System SpecsSystem Spec

File I/O problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing small file turns in to huge file in print queue Gorge Vista General 3 17 Aug 2009
Scheduled task (run .bat file) fails every time with 0x80070002 (file not found) Ben Dilts Vista General 2 22 Jul 2008
Create New folder, move file, delete file. Explorer Freeze erod49 General Discussion 10 20 May 2008
coping file from a remote file share - FILE IS NO LONG THERE bogus error message Heith Vista networking & sharing 0 18 Oct 2007
File Open and File Save As Dialog Freeze and Hang All Applications =?Utf-8?B?TGlnaHRmb290ZQ==?= Vista file management 0 12 Jul 2006