Skip to content

Instantly share code, notes, and snippets.

@gavxin
Created November 24, 2015 03:49
Show Gist options
  • Save gavxin/4b6edc6af3f33ad085aa to your computer and use it in GitHub Desktop.
Save gavxin/4b6edc6af3f33ad085aa to your computer and use it in GitHub Desktop.
notepad++ python script plugin script for convert files to UTF8 encoding and Unix EOL.
#-*- encoding:gb2312 -*-
#
# Notepad++ Python script plugin script.
# For converting file encoding & lineending.
#
import os;
import sys;
filter_exts = [".h", ".cpp", ".c"]
SC_CP_UTF8 = 65001
SC_EOL_LF = 2
def convert(dir):
for root, dirs, files in os.walk(dir):
for f in files:
file_name, file_ext = os.path.splitext(f);
if file_ext in filter_exts:
path = root + "\\" + f
notepad.open(path)
console.write(path)
if editor.getEOLMode() != SC_EOL_LF:
answer = notepad.messageBox("文件" + path + " EOL将被转换成Unix格式(lf),是否确定?", "Confirm", MESSAGEBOXFLAGS.YESNOCANCEL)
if answer == MESSAGEBOXFLAGS.RESULTYES:
editor.convertEOLs(SC_EOL_LF)
notepad.save()
if answer == MESSAGEBOXFLAGS.RESULTCANCEL:
notepad.close()
return
if editor.getCodePage() != SC_CP_UTF8:
console.write(" code page:" + repr(editor.getCodePage()))
answer = notepad.messageBox("文件" + path + "将要被转换成utf8格式,是否确定?", "Confirm", MESSAGEBOXFLAGS.YESNOCANCEL)
if answer == MESSAGEBOXFLAGS.RESULTYES:
notepad.runMenuCommand("Encoding", "Convert to UTF-8")
notepad.save()
if answer == MESSAGEBOXFLAGS.RESULTCANCEL:
notepad.close()
return
notepad.close()
console.write("\r\n")
p = notepad.prompt("请输入目录路径", "输入目录路径")
if p:
convert(p)
@Limour-dev
Copy link

#-*- encoding:utf-8 -*-
import os
import sys

filePathSrc = notepad.prompt("请输入目录路径".decode('utf-8').encode('gbk'), "输入目录路径".decode('utf-8').encode('gbk'))

for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
        print fn.decode('gbk').encode('utf-8')
        if fn[-4:]=='.txt':# Specify type of the files
            print fn[-4:]
            notepad.open((root + "\\" + fn).decode('gbk').encode('utf-8'))
            print notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_AS_UTF_8)
            notepad.save()
            notepad.close()
print 'finish'

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