Script to make content entry into a text file
Asked Answered
P

6

23

How do I include some "text" into a .txt format file without opening the same via a script on Windows?

Panslavism answered 25/2, 2011 at 15:35 Comment(1)
echo This is My Text >> myText.txt? - Really though, this seems more suited to StackOverflow - voting to close/move...Gastroenterology
S
37

I will give you an all PowerShell answer. You can use Add-Content or Set-Content cmdlets.

Set-Content overwrites the target file and Add-Content appends to the file.

Set-Content -Value "Test1" -Path C:\Scripts\Scratch\test.txt
Add-Content -Value "Test" -Path C:\Scripts\Scratch\test.txt

Or, you can also use Out-File.

"Test" | Out-File -FilePath C:\Scripts\Scratch\test.txt -Append
Stace answered 25/2, 2011 at 17:25 Comment(0)
M
5

The command you need is echo (alias of Write-Output - use Get-Alias to get the list):

 echo Text >> textFile.txt

This link should prove helpful in learning Windows commands.

Mimicry answered 25/2, 2011 at 17:24 Comment(0)
S
5

Here is the sample code to create and add content into a text file:

$text = Hello World

# This is to create file:
$text | Set-Content MyFile.txt

# Or
$text | Out-File MyFile.txt

# Or
$text > MyFile.txt


# This is to write into a file or append to the text file created:
$text | Add-Content MyFile.txt

# Or
$text | Out-File MyFile.txt -Append

# Or
$text >> MyFile.txt
Schadenfreude answered 16/10, 2016 at 16:14 Comment(0)
L
0

If you want to do it interactively from a standard Windows command prompt (typing the content in at the keyboard), you can use the following:

copy con c:\temp\file.txt

Then you can just start typing. To finish, just hit Ctrl+Z and ENTER, like so:

Hello world!
Goodbye...^Z
        1 file(s) copied.

To view the file, use:

type c:\temp\file.txt

You should see the following output:

Hello world!
Goodbye...
Liverwurst answered 26/2, 2011 at 4:46 Comment(0)
L
0

The Get-Content cmdlet should work fine for you.

Lexeme answered 27/2, 2011 at 1:58 Comment(0)
E
-4
$com1 = New-Object PSobject  # Task 1
$com2 = New-Object PSobject  # Task 1
$com3 = New-Object PSobject  # Task 1

$com1 | Add-Member noteproperty -name user -value jindpal                      # Task 2
$com1 | Add-Member noteproperty -name code -value IT01                         # Task 2
$com1 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version}  # Task 3

$com2 | Add-Member noteproperty -name user -value singh                        # Task 2
$com2 | Add-Member noteproperty -name code -value IT02                         # Task 2
$com2 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version}  # Task 3

$com3 | Add-Member noteproperty -name user -value dhanoa                       # Task 2
$com3 | Add-Member noteproperty -name code -value IT03                         # Task 2
$com3 | Add-Member scriptmethod ver {[system.Environment]::oSVersion.Version}  # Task 3

$arr += $com1, $com2, $com3  # Task4

Write-Host  "Windows version of computer 1 is: "$com1.ver()  # Task 3
Write-Host  "User name of computer 1 is: "$com1.user         # Task 6
Write-Host  "Code of computer 1 is: "$com1,code              # Task 5

Write-Host  "Windows version of computer 2 is: "$com2.ver()  # Task 3
Write-Host  "User name of computer 2 is: "$com2.user         # Task 6
Write-Host  "Windows version of computer 3 is: "$com3.ver()  # Task 3

Write-Host  "User name of computer 3 is: "$com1.user         # Task 6
Write-Host  "Code of computer 3 is: "$com3,code              # Task 5

Read-Host
Edifice answered 13/10, 2016 at 0:27 Comment(3)
$arr =@("jind",12, "singh") write-host $arr[1] read-host $arr += "reza" write-host $arr[3] read-host write-host $arr[$arr.length-1] read-host $arr = $arr -ne $arr[1] write-host $arr read-host foreach ($i in $arr) {write-host $i}Edifice
$room = Read-Host “Enter a room number:” get-content computers.txt | where {$_ -match "B108"} $newcontents = get-content computers.txt | where {$_ -match "B108"}Edifice
An explanation would be in order.Hejira

© 2022 - 2024 — McMap. All rights reserved.