How to read a file in UTF-8 and remove a BOM in batch script?

Microsoft Windows
Post Reply
PiotrMP006
Posts: 8
Joined: 2021-Sep-01, 10:57 am

How to read a file in UTF-8 and remove a BOM in batch script?

Post by PiotrMP006 »

Hi

How to read a file in UTF-8 and remove a BOM in batch script?
User avatar
Simon Sheppard
Posts: 93
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: How to read a file in UTF-8 and remove a BOM in batch script?

Post by Simon Sheppard »

Some answers to this here

If you're using PowerShell [Core] version 6 or higher, you get BOM-less UTF-8 files by default
https://stackoverflow.com/questions/400 ... 4#40098904

PowerShell is much better suited to the task of dealing with encodings than batch:

To remove embedded UTF-8 BOM characters from source files:

Code: Select all

$files=get-childitem -Path . -Include @("*.h","*.cpp") -Recurse
foreach ($f in $files){
 (Get-Content $f.PSPath) | 
 Foreach-Object {$_ -replace "\xEF\xBB\xBF", ""} | 
 Set-Content $f.PSPath
}
via SO
Post Reply