合肥to outfile linux脚本,powershell模块Out-FileUtf8NoBom, 输出没有Bom的UTF8文件

本文介绍了一个PowerShell脚本功能,该功能能够将数据输出到UTF-8编码的文件中且不包含字节顺序标记(BOM),解决了PowerShell原生Out-File命令的一个不足。通过此功能,用户可以灵活地指定换行符类型、宽度,并选择是否追加到现有文件或覆盖只读文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源, 由周蒙牛修改.

.SYNOPSIS

Outputs to a UTF-8-encoded file *without a BOM* (byte-order mark).

.DESCRIPTION

Mimics the most important aspects of Out-File:

* Input objects are sent to Out-String first.

* -Append allows you to append to an existing file, -NoClobber prevents

overwriting of an existing file.

-Append dones't seem to work. 周蒙牛(zhoujin7) comments.

* -Width allows you to specify the line width for the text representations

of input objects that aren't strings.

However, it is not a complete implementation of all Out-String parameters:

* Only a literal output path is supported, and only as a parameter.

* -Force is supported. It write by 周蒙牛(zhoujin7).

Indicates that the cmdlet overwrites an existing read-only file.

* -NewLineType allows you to specify the type of newline. It write by 周蒙牛(zhoujin7).

The optional types are 'windows','unix','mac'.

Default type is 'unix'.

Caveat: *All* pipeline input is buffered before writing output starts,

but the string representations are generated and written to the target

file one by one.

.NOTES

The raison d'être for this advanced function is that, as of PowerShell v5,

Out-File still lacks the ability to write UTF-8 files without a BOM:

using -Encoding UTF8 invariably prepends a BOM.

Copyright (c) 2017 Michael Klement (http://same2u.net),

released under the [MIT license](https://spdx.org/licenses/MIT#licenseText).

#>

function Out-FileUtf8NoBom

{

[CmdletBinding()]

param (

[Parameter(Mandatory, Position = 0)]

[string]$LiteralPath,

[switch]$Append,

[switch]$NoClobber,

[AllowNull()]

[int]$Width,

[Parameter(ValueFromPipeline)]

$InputObject,

[switch]$Force,

[ValidateSet('windows', 'unix', 'mac')]

[String]$NewLineType = 'unix'

)

#requires -version 3

# Make sure that the .NET framework sees the same working dir. as PS

# and resolve the input path to a full path.

[System.IO.Directory]::SetCurrentDirectory($PWD) # Caveat: .NET Core doesn't support [Environment]::CurrentDirectory

$LiteralPath = [IO.Path]::GetFullPath($LiteralPath)

#It write by 周蒙牛(zhoujin7)

if (Test-Path -LiteralPath $LiteralPath)

{

$file = Get-Item -LiteralPath $LiteralPath -Force

if (!($file.IsReadOnly))

{

Remove-Item -LiteralPath $LiteralPath -Force

}

else

{

Throw [IO.IOException] "The file '$LiteralPath' is read-only!"

}

}

#It write by 周蒙牛(zhoujin7)

if ($Force -and (Test-Path -LiteralPath $LiteralPath))

{

Remove-Item -LiteralPath $LiteralPath -Force

}

# If -NoClobber was specified, throw an exception if the target file already

# exists.

if ($NoClobber -and (Test-Path $LiteralPath))

{

Throw [IO.IOException] "The file '$LiteralPath' already exists."

}

# Create a StreamWriter object.

# Note that we take advantage of the fact that the StreamWriter class by default:

# - uses UTF-8 encoding

# - without a BOM.

$sw = New-Object IO.StreamWriter $LiteralPath, $Append

$htOutStringArgs = @{ }

if ($Width)

{

$htOutStringArgs += @{ Width = $Width }

}

# Note: By not using begin / process / end blocks, we're effectively running

# in the end block, which means that all pipeline input has already

# been collected in automatic variable $Input.

# We must use this approach, because using | Out-String individually

# in each iteration of a process block would format each input object

# with an indvidual header.

try

{

#It write by 周蒙牛(zhoujin7)

switch ($NewLineType)

{

'windows' {

$NewLine = "`r`n"

break

}

'unix' {

$NewLine = "`n"

break

}

'mac' {

$NewLine = "`r"

break

}

}

$Input | Out-String -Stream @htOutStringArgs | ForEach-Object { $sw.Write("$_$NewLine") }

}

finally

{

$sw.Dispose()

}

}

×用微信扫描并分享

暂时没有相关文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值