将内容输入到文本文件中的脚本

发布于 2024-10-19 12:10:15 字数 54 浏览 8 评论 0原文

如何将一些“文本”包含到 .txt 格式文件中,而不通过 Windows 上的脚本打开该文件?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

箜明 2024-10-26 12:10:15

我会给你一个完全PowerShell的答案。您可以使用添加内容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

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
我只土不豪 2024-10-26 12:10:15

您需要的命令是 echo写入输出 - 使用 Get-Alias 获取列表):

 echo Text >> textFile.txt

此链接 应该会有所帮助学习 Windows 命令。

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.

少女的英雄梦 2024-10-26 12:10:15

以下是创建内容并将其添加到文本文件中的示例代码:

$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

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
ㄖ落Θ余辉 2024-10-26 12:10:15

如果您想从标准 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...

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...
迷鸟归林 2024-10-26 12:10:15

Get-Content cmdlet 应为你工作得很好。

The Get-Content cmdlet should work fine for you.

白芷 2024-10-26 12:10:15
$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
$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文