Hi
How to read a file in UTF-8 and remove a BOM in batch script?
How to read a file in UTF-8 and remove a BOM in batch script?
Moderator: DosItHelp
-
- Posts: 31
- Joined: 08 Sep 2017 06:10
Re: How to read a file in UTF-8 and remove a BOM in batch script?
The BOM is just one multibyte-character. You can read it away using PAUSE commands for the 3 bytes.
Steffen
Code: Select all
@echo off
set "rmvBomRedir=|((pause&pause&pause)>nul&findstr "^^")"
set "rmvBomFor=^|((pause^&pause^&pause^)^>nul^&findstr "^^"^)"
>nul chcp 65001
:: redirect to console
type "utf8.txt" %rmvBomRedir%
:: redirect to a pipe
(type "utf8.txt" %rmvBomRedir%)|find /v ""
:: redirect to a file
>"utf8noBom.txt" (type "utf8.txt" %rmvBomRedir%)
:: process in a FOR /F loop
for /f "delims=" %%i in ('type "utf8.txt" %rmvBomFor%') do echo %%i
pause
Last edited by aGerman on 05 Oct 2021 01:54, edited 1 time in total.
Reason: updated code
Reason: updated code
Re: How to read a file in UTF-8 and remove a BOM in batch script?
Forget about what I told you about one PAUSE is enough. Updated the code above.