Skip to content

Instantly share code, notes, and snippets.

@bjverde
Forked from smccutchen/ConvertUTF-8.py
Last active July 1, 2023 15:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjverde/88bbc418e79f016a57539c2d5043c445 to your computer and use it in GitHub Desktop.
Save bjverde/88bbc418e79f016a57539c2d5043c445 to your computer and use it in GitHub Desktop.
Convert multiple files to UTF-8 encoding with Notepad++
# 2016-2017 Soverance Studios.
# Scott McCutchen
# This file will search all files and folders within a given directory, and use Notepad++ to convert their encoding to UTF-8 without Byte Order Marks
#
# This file must be run using the PythonScript plugin from within Notepad++, which is available through the Notepad++ Plugin Manager
#
# You must have Python 2.7 installed
#
# Additionally, this script can only exist and be run from within the Notepad++ user's working directory, the default of which is here:
# Note that selecting "New Script" from within the PythonScript plugin will automatically default to this save location
#
# .. USER DIRECTORY\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts
#
# WARNING !! This script will work if Notepad ++ is in ANY Lang. To use in your native language use the terms of the translation
import os;
import sys;
from Npp import notepad
filePathSrc="D:\\wamp\www\\formDin\\base" # Path to the folder with files to convert
for root, dirs, files in os.walk(filePathSrc):
for fn in files:
if fn[-4:] == '.php' or fn[-4:] == '.inc' or fn[-4:] == '.css' or fn[-4:] == '.js' or fn[-4:] == '.htm' or fn[-4:] == '.html': # Specify file types, taking care to change the fn[number] to correspond to length of the file's extension including the .
notepad.open(root + "\\" + fn)
console.write(root + "\\" + fn + "\r\n")
#notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_AS_UTF_8)
notepad.save()
notepad.close()
@brainexcerpts
Copy link

brainexcerpts commented Feb 14, 2021

A version to convert every currently opened tabs:

## reference manual to 'notepad' (save, open, close, get current tabs, activate tab, execute notpad menu etc.)
## file:///C:/Program%20Files%20(x86)/Notepad++/plugins/PythonScript/doc/notepad.html
## reference manual to 'editor' (edit text etc)
## file:///C:/Program%20Files%20(x86)/Notepad++/plugins/PythonScript/doc/scintilla.html
## reference manual to 'console' (write messages in py console, execute windows commands or scripts)
## file:///C:/Program%20Files%20(x86)/Notepad++/plugins/PythonScript/doc/console.html
for filename, bufferID, index, view in notepad.getFiles():
    console.write( filename + "\r\n")        
    notepad.activateIndex(view, index)       
    # UTF8 (without BOM)
    notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_AS_UTF_8)
    notepad.save()    
    notepad.reloadCurrentDocument()

#Note: pay attention to the 'AS' and 'CONV2' in the enum names!!
#notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_AS_UTF_8) #to UTF8 without BOM
#notepad.menuCommand(MENUCOMMAND.FORMAT_AS_UTF_8) #to UTF8 without BOM
#notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_UTF_8) #to UTF8-BOM

@masterwishx
Copy link

How to add win 1251 file names reading for script ?

@masterwishx
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment