Powershell 中的 Out-File -append 不会产生新行并将字符串分解为字符 [英] Out-File -append in Powershell does not produce a new line and breaks string into characters

查看:25
本文介绍了Powershell 中的 Out-File -append 不会产生新行并将字符串分解为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过这个 cmdlet 来理解一些奇怪的行为.

I'm trying to understand some weird behaviour with this cmdlet.

如果我在我创建的文本文件上使用Out-File -append Filename.txt"并通过 Windows 上下文菜单输入文本,该字符串将作为一系列空格分隔附加到该文件的最后一行字符.

If I use "Out-File -append Filename.txt" on a text file that I created and entered text into via the windows context menu, the string will append to the last line in that file as a series of space separated characters.

所以:

"This is a test" | out-file -append textfile.txt

将产生:这很重要

如果 out-file 创建了文件,或者文本文件在追加之前没有文本,则不会发生这种情况.为什么会发生这种情况?

This wont happen if out-file creates the file, or if the text file has no text in it prior to appending. Why does this happen?

我还要注意,重复该命令只会以相同的方式附加到同一行.我猜它无法识别换行符或换行符终止符或由于编码更改而导致的某些内容?

I will also note that repeating the command will just append in the same way to the same line. I guess it doesn't recognise newline or line break terminator or something due to changed encoding?

推荐答案

Out-File 默认为 unicode 编码,这就是您看到的行为的原因.使用 -Encoding Ascii 来改变这种行为.在你的情况

Out-File defaults to unicode encoding which is why you are seeing the behavior you are. Use -Encoding Ascii to change this behavior. In your case

Out-File -Encoding Ascii -append textfile.txt. 

Add-Content 使用 Ascii 并默认附加.

Add-Content uses Ascii and also appends by default.

"This is a test" | Add-Content textfile.txt.

至于缺少换行符:您没有发送换行符,因此它不会向文件写入一个.

As for the lack of newline: You did not send a newline so it will not write one to file.

这篇关于Powershell 中的 Out-File -append 不会产生新行并将字符串分解为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆