Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ convert to UTF-8 multiple files

The function "Convert to UTF-8 without BOM" of Notepad++ is really nice. But I have 200 files and all of them need to be coverted. Therefor I found this little python script:

import os;
import sys;
filePathSrc="C:\\Temp\\UTF8"
for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
      if fn[-4:] != '.jar' and fn[-5:] != '.ear' and fn[-4:] != '.gif' and fn[-4:] != '.jpg' and fn[-5:] != '.jpeg' and fn[-4:] != '.xls' and fn[-4:] != '.GIF' and fn[-4:] != '.JPG' and fn[-5:] != '.JPEG' and fn[-4:] != '.XLS' and fn[-4:] != '.PNG' and fn[-4:] != '.png' and fn[-4:] != '.cab' and fn[-4:] != '.CAB' and fn[-4:] != '.ico':
        notepad.open(root + "\\" + fn)
        console.write(root + "\\" + fn + "\r\n")
        notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
        notepad.save()
        notepad.close()

It goes through every file -> I can see this. But after it finished, the charset is stil ANSI in my case :/

Can anyone help me?

like image 881
Phil Avatar asked Feb 20 '16 17:02

Phil


People also ask

How do I save a Notepad file as UTF-8?

Open the file you want to verify/fix in Notepad++ In the top menu select Encoding > Convert to UTF-8 (option without BOM) Save the file.


1 Answers

Here is what worked for me:

Go to Notepad++ -> Plugins -> Plugins Admin.

Find and install Python Script plugin.

Create new python script with Plugins -> Python Script -> New script.

Insert this code into your script:

import os;
import sys;
filePathSrc="C:\\Users\\YourUsername\\Desktop\\txtFolder"
for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
      if fn[-4:] == '.txt' or fn[-4:] == '.csv':
        notepad.open(root + "\\" + fn)
        console.write(root + "\\" + fn + "\r\n")
        notepad.runMenuCommand("Encoding", "Convert to UTF-8")
        notepad.save()
        notepad.close()

Replace C:\\Users\\YourUsername\\Desktop\\txtFolder with path to your Windows folder where your files are.

Script works with .txt and .csv files and ignores all other files in folder.

Run script with Plugins -> Python Scripts -> Scripts -> name of your script

like image 129
Hrvoje Avatar answered Sep 22 '22 18:09

Hrvoje