writing output to the text file

write-output to file powershell
how to save command line output in text file linux
powershell output to file -append
powershell write string to file
powershell write-host to file
powershell write variable to file
powershell output to file and console
powershell output to file csv

I need to find the first 20,000 random numbers generated by RND -1. I have written the following code, which allows me to see those numbers in the Immediate Window:

Sub xx()
Dim i As Long
Rnd -1
For i = 1 To 20000
    Debug.Print Rnd
Next i
End Sub

However, only the last 199 lines are stored there for me to copy and paste. How can I write the entire output to a text file instead?

Many thanks

Since your goal seems to be to get a lot of random numbers in the clipboard, you can do the following:

in the VBA editor under tools/references, add a reference to Microsoft Forms 2.0 Object Library and then use:

Sub RandToClip(n As Long)
    Dim clip As New DataObject
    Dim A As Variant
    Dim i As Long

    ReDim A(1 To n)
    Randomize

    For i = 1 To n
        A(i) = Rnd()
    Next i

    clip.SetText Join(A, vbCrLf)
    clip.PutInClipboard
End Sub

If you then e.g. enter RandToClip 20000 in your clipboard available for pasting somewhere else.

Write output to a text file in PowerShell, Use the Out-File cmdlet. Compare-Object | Out-File C:\filename.txt. Optionally, add -Encoding utf8 to Out-File as the default encoding is not  Once you enter the command and press Enter, you are returned to the command prompt. The output is sent to the text file, but not to the screen, so you do not see the output. The network_info.txt file is created in the current directory.

You can easily write to a text file (file stream) by using a FileSystemObject. See example below for working code in a module:

Global fso As New FileSystemObject

Public Function GenRandomNumbers(ByVal n As Long) As String
    Dim i As Long
    Dim res As String
    Rnd -1
    For i = 1 To n
        res = res & CStr(Rnd()) & vbCrLf
    Next i
    GenRandomNumbers = res
End Function

Public Sub WriteRandomNumbers(ByVal n As Long, ByVal filename As String)
    Dim fs As TextStream
    Set fs = fso.CreateTextFile(filename, Overwrite:=True)
    fs.Write GenRandomNumbers(n)
    fs.Close
End Sub

In the immediate window you can write:

WriteRandomNumbers 20000, "Result.txt"

How to write console output to a txt file, You need to do something like this: PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out);. The second statement is the key  When you use a BAT file to pipe a command's output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file. Use Redirection Operators in Batch Files

Answering your question: here is the basic function for that task. Make sure to add checks of whether file exists, not locked, etc. But take a look at the solution provided by John Coleman as it may be a better solution for your task.

Public Function WritetoTXT (Byval Text as String, byval FilePath as String)    
    Dim TextFile As Long
    TextFile = FreeFile
    Open Path For Append As TextFile
    Print #TextFile, Text
    Close TextFile
End Function

In your code:

Sub xx()
Dim i As Long
Rnd -1
For i = 1 To 20000
    WritetoTXT  Rnd, "your file path here"
Next
End Sub

Edit: As pointed out in comments to decrease overhead you can combine your code to the following:

Sub xx()
Dim i As Long
Rnd -1
Dim TextFile As Long
TextFile = FreeFile
Open "your file path here" For Append As TextFile
For i = 1 To 20000
    Print #TextFile,  Rnd
Next
Close TextFile
End Sub

How to save command output to file using Command Prompt or , txt" with the path and file name with the output content. After completing the steps, the PowerShell command will save the result into a text file on  The simplest way is to just redirect the output, like so: > will cause the output file to be overwritten if it already exists. >> will append new text to the end of the output file if it already exists. This is the cmd / dos method.

Out-File, Explains how to redirect output from PowerShell to text files. Copy. &{ Write-​Warning "hello" Write-Error "hello" Write-Output "hi" } 3>&1 2>&1  The following code uses the StreamWriter class to open, to write, and to close the text file. In a similar way to the StreamReader class, you can pass the path of a text file to the StreamWriter constructor to open the file automatically. The WriteLine method writes a complete line of text to the text file.

about_Redirection, The first is to send the command output write to a new file every time you run the command. To do this, open the command prompt and type: dir  If this flag is not set, the initial position is the beginning of the file. ios::app: All output operations are performed at the end of the file, appending the content to the current content of the file. ios::trunc: If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.

Redirect Output from the Windows Command Line to a Text File, Call sink() without any arguments to return output to the terminal. # Start writing to an output file sink('analysis-output.txt') set.seed(12345) x <-rnorm(10,10,1) y  Similar to a previous cmdlet of a day we covered, Set-Content, PowerShell also has another cmdlet called Out-File that performs a similar feat which is write output directly to a text file.

Comments
  • or just write it out to a worksheet and copy/paste from there. In your FOR loop: Sheet1.Range("A" & i).value = Rnd Also suggest before that line that you use command Randomize to initialize the seed. Otherwise it won't be so random.
  • stackoverflow.com/q/11503174/1188513
  • Thanks JNevill, that's a good suggestion and what I ended up doing
  • Long strings concatenation has significant impact on performance. Use Join instead.
  • I tested it with 20000 strings from the immediate window with no discernible delay whatsoever. So in theory yes, but practically it doesn't matter for this case.
  • You'll want to open the file once, write everything into it, then close it. Open+Close IO overhead over 20K iterations sounds like a massive performance hit to take, no?