good day, I have several .txt files written in Russian language., in UTF-8. I need to convert all this files to UTF-8-BOM. So, I find this line on internet. But there is only one file. How can I convert all my 1000 .txt files at once?
Note that with Set-Content in Windows PowerShell (5.1) -Encoding utf8 encodes in UTF-8 with BOM but with PowerShell Core (7.1) -Encoding utf8 encodes in UTF-8 with no BOM.
You may find that Get-Content and Set-Content on the same file gives an error because the file is in use so write the new files to a different folder.
For processing multiple files, just use a foreach loop.
# Assumes Windows PowerShell, use -Encoding utf8BOM with PowerShell Core.
$files = Get-ChildItem E:\Temp\Source\ -Filter *.txt
foreach ($file in $files) {
Get-Content $file.FullName | Set-Content "E:\Temp\Destination\$($file.Name)" -Encoding utf8
}
I test the -Encoding with utf8 and utf8BOM. I got this error
Set-Content : Cannot bind parameter 'Encoding'. Cannot convert value "utf8BOM" to type
"Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding". Error: "Unable to match the identifier name utf8BOM
to a valid enumerator name. Specify one of the following enumerator names and try again:
Unknown, String, Unicode, Byte, BigEndianUnicode, UTF8, UTF7, UTF32, Ascii, Default, Oem, BigEndianUTF32"
At line:3 char:92
+ ... e | Set-Content "c:\Folder1\$($file.Name)" -Encoding utf8BOM
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Content], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SetContentCommand
Please do not post images of code opr error messages. Instead post the text of error messages and format it as code as well. You can go back and edit your already existing post. You don’t have to create a new one.
I find this solution, it is a Python Script. I test it, and WORKS only in Notepad++.
Run this code on Menu → Plugins → Python Scripts . After running the code, a new pop-up window appears, and you must write there the path of your files, like c:\Folder1\ Click OK. That’s all !
# -*- coding: utf-8 -*-
from __future__ import print_function
from Npp import notepad
import os
uft8_bom = bytearray(b'\xEF\xBB\xBF')
top_level_dir = notepad.prompt('Paste path to top-level folder to process:', '', '')
if top_level_dir != None and len(top_level_dir) > 0:
if not os.path.isdir(top_level_dir):
print('bad input for top-level folder')
else:
for (root, dirs, files) in os.walk(top_level_dir):
for file in files:
full_path = os.path.join(root, file)
print(full_path)
with open(full_path, 'rb') as f: data = f.read()
if len(data) > 0:
if ord(data[0]) != uft8_bom[0]:
try:
with open(full_path, 'wb') as f: f.write(uft8_bom + data)
print('added BOM:', full_path)
except IOError:
print("can't change - probably read-only?:", full_path)
else:
print('already has BOM:', full_path)