2011-02-25 72 views
15

如何通過Windows上的腳本將某些「文本」包含到.txt格式文件中而不通過腳本打開該文件?將內容輸入到文本文件中的腳本

+1

echo這是我的文本>> myText.txt? - 真的,雖然這似乎更適合StackOverflow - 投票關閉/移動... – techie007 2011-02-25 15:51:24

回答

31

我會給你一個PowerShell的全部答案。 您可以使用Add-Content或Set-Content cmdlet。

Set-Content覆蓋目標文件,Add-Content附加到文件。

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

或者,您也可以使用Out-File。

"Test" | Out-File -FilePath C:\Scripts\Scratch\test.txt -Append 
5

你需要的命令是回聲:

echo Text>>textFile.txt 

This link應該證明在學習Windows命令的幫助。

0

如果你想從一個標準的Windows命令提示符下以交互方式做到這一點(打字在鍵盤上的內容),您可以使用以下命令:

copy con c:\temp\file.txt 

然後,你可以開始輸入。要完成,只需按下Ctrl + Z和Enter鍵,就像這樣:

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

要查看該文件,使用:

type c:\temp\file.txt 

你應該看到下面的輸出:

Hello world! 
Goodbye... 
0

GET -content cmdlet應該可以正常工作。

-2

$ COM1 =新對象PSobject#任務1 $ COM2 =新對象PSobject#任務1 $ COM3 =新對象PSobject#任務1

$ COM1 | add-member noteproperty -name用戶-value jindpal#任務2 $ com1 | add-member noteproperty -name code -value IT01#Task2 $ com1 | add-member scriptmethod ver {[system.Environment] :: oSVersion.Version}#任務3

$ com2 | add-member noteproperty -name用戶-value singh#任務2 $ com2 | add-member noteproperty -name code -value IT02#Task2 $ com2 | add-member scriptmethod ver {[system.Environment] :: oSVersion.Version}#任務3

$ com3 | add-member noteproperty -name用戶-value dhanoa#任務2 $ com3 | add-member noteproperty -name code -value IT03#Task2 $ com3 |附加構件scriptmethod版本{[系統環境] :: oSVersion.Version}#任務3

$ ARR + = $ COM1,COM2 $,$ COM3#Task4

寫主機「窗口電腦1的版本是:「$ com1.ver()#Task3 write-host」computer1的用戶名是:「$ com1.user#Task6 computer-1的write-host」代碼是:「$ com1,code#Task5 write-host」 computer2的windows版本是:「$ com2。ver()#Task3 write-host「computer2的用戶名是:」$ com2.user#Task6 write-host「computer3的windows版本是:」$ com3.ver()#Task3 write-host「用戶名COMPUTER3的是:「$#com1.user Task6中 寫 - 主機」 COMPUTER3的代碼是:「$ COM3,代碼#任務5

讀主機

+0

$ 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] 寫主機$ ARR 讀主機 的foreach($ I $中ARR) {寫主機$ I} – mad 2016-10-13 00:31:35

+0

$房=讀 - 主機「中輸入房間號:」 獲得內容computers.txt |其中{$ _ -match「B108」} $ newcontents = get-content computers.txt |其中{$ _ -match「B108」} – mad 2016-10-13 00:40:22

3

這裏是示例代碼來創建和添加內容到文本文件

$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 
相關問題