import os
import shutil
#-*- coding:cp936 -*-
import codecs
from sys import argv


def replace_all_files(path):
cnt = 0
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(".cpp"):
f = codecs.open(os.path.join(root, name), "rb+")
text = f.read()
text1 = text
if text.find("\"stdafx.h\"")<0:
key = "#include \"stdafx.h\"\r\n"
if text[:3]==codecs.BOM_UTF8:
text1 = "%s%s%s" % (codecs.BOM_UTF8, key, text[3:])
else:
text1 = "%s%s" % (key, text)
f.seek(0)
f.truncate()
f.write(text1)
cnt = cnt+1
print "[%d] relpace File: " %cnt + os.path.join(root, name)
f.close()


if __name__ == "__main__":
try:
replace_all_files(argv[1])
except Exception, e:
print e.message

posted on 2018-07-27 16:50  叶城宇  阅读(267)  评论(0编辑  收藏  举报