批量替换VS项目文件设置

一个 Windows 项目包含接近 30 个 VS 项目文件,需要从静态链接运行库修改为动态链接运行库。如果手动修改,每个项目需要修改 Release 和 Debug 下编译选项,需要修改 60 种配置。最终写了一个脚本遍历源码目录下项目文件,自动替换编译选项。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os

def replace_md(proj):
print proj
projtmp = proj + '.tmp'

os.rename(proj, projtmp)

srcfile = open(projtmp, 'r')
newfile = open(proj, 'w')
for line in srcfile:
if 'RuntimeLibrary=' in line:
line = 'RuntimeLibrary="2"'
if 'release_static' in line:
line = line.replace('release_static', 'release')
if 'debug_static' in line:
line = line.replace('debug_static', 'debug')
if '_MT.lib' in line:
line = line.replace('_MT.lib', '_MD.lib')
if '_MTd.lib' in line:
line = line.replace('_MTd.lib', '_MDd.lib')
newfile.write(line)
srcfile.close()
newfile.close()

os.remove(projtmp)

def walk_srcdir(dir):
for dirpath,dirnames,filenames in os.walk('.'):
for file in filenames:
if '.vcproj' in file and 'pc.user' not in file:

replace_md(dir + os.path.join(dirpath[1:], file))

walk_srcdir(os.getcwd())