2011-02-25 105 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 
相关问题