随笔 - 64, 文章 - 11, 评论 - 12, 引用 - 0
数据加载中……

由正则表达式"\."引出的问题,而谈及DOS文件格式

      这段时间,公司要我整理一个有点历史的产品代码。由于项目历史久,长期又缺少管理,产品中的项目有点乱,以至于一个DLL模块配置的输出路径竟然有输出成exe可执行模块。存在这样问题的项目有30个左右,而且每个项目要改4个地方,因为有4种生成版本。如果手工去改的话实在很枯燥,但我们是程序员。所以就用代码了。
 1' 更改所有资源的输出路径
 2
 3' get folder path
 4scriptName = wscript.scriptfullname
 5scriptPath = Left(scriptName, instrRev(scriptName, "\"))
 6scriptPath = Left(scriptPath, Len(scriptPath) -1)
 7scriptPath = Left(scriptPath, instrRev(scriptPath,"\"))
 8folderPath = scriptPath & "Loc\"
 9
10' create filesystemobject activex object
11Set fso = CreateObject("Scripting.FileSystemObject")
12Set fol = fso.GetFolder(folderPath)
13
14' visit each file which in the folder
15fileshortName = ""
16Set fileArr = fol.Files
17For each fil in fileArr
18    fileName = fil.name
19    If LCase(Right(fileName, 7)) = ".vcproj" Then
20        fileshortName = Left(fileName,Len(fileName) - 7)
21        If LCase(Right(fileshortName, 4)) <> "_enu" Then
22            modifyResource fil.path,status
23            If status =False Then
24                errlist = errlist &  fil.Path & vbCr
25            End If
26        End If
27    End If
28Next
29
30' Tip when complete the work
31If Len(errlist) > 0 Then
32    MsgBox errlist & "Can not modify"
33Else
34    MsgBox "modify successfully"
35End If
36
37' modify the resource project setting.
38Function modifyResource(filePath,status)
39On Error Resume Next
40
41    resLanguage = UCase(Right(fileshortName, 3))
42    
43    ' Read Content
44    Set fRead = fso.opentextfile(filePath, 1)
45    fContent = fRead.readAll
46    fRead.close
47    
48    ' Replace each output file
49    changed = False
50    Set regEx = New RegExp
51    regEx.pattern = "\bOutputFile=.+"
52    regEx.Global = True
53    Set matches = regEx.Execute(fContent)
54    Set childReg = New RegExp
55    For Each match in matches    
56        If LCase(Right(match.Value, 9)) <> "\loc.dll""" Then            
57            tmpValue = match.Value
58            childReg.pattern = tmpValue
59            If instrRev(tmpValue, "\")>0 Then            
60                tmpValue = Left(tmpValue, instrRev(tmpvalue, "\"))
61            ElseIf instrRev(tmpValue,"/"> 0 Then
62                tmpValue = Left(tmpValue,instrRev(tmpValue,"/"))
63            End If
64            tmpValue = tmpValue & resLanguage & "\loc.dll"""
65            childMatches = childReg.Execute(fContent)
66            ' fContent = childReg.Replace(fContent, tmpValue)
67            fContent = Replace(fContent, match.Value, tmpValue)
68            changed = True
69        End If
70    Next    
71
72    ' write back
73    If changed=True Then
74        Set fWrite = fso.opentextfile(filePath, 2, False)
75        fWrite.Write fContent
76        fWrite.close
77    End If
78    ' clear error
79    If Not err.number = 0 Then
80        err.clear
81        status = False
82    Else
83        status = True
84    End If
85End Function

运行结果符合我们的要求。
   用ultraEdit打开文件时,会提示是否要转换成DOS文件格式,但在转换前用ultraEdit打开不会出现这种问题。问题出在哪儿呢?这还得从DOS文件与非DOS文件格式的区别分析,这两种文件的差别就是一些控制符不同,如DOS是用\r\n来换行,而Unix是用\n来换行。有了这个分析,就知道问题出在哪了。原因是正则表达式
regEx.pattern = "\bOutputFile=.+"这个表达式把后面的"\r"也匹配进去了,而后来随着替换字符串时而消失了。所以上面的正则应写成regEx.pattern = "\bOutputFile=.+"""。改好之后运行,再用ultraEdit打开不会出现这个提示了。

posted on 2009-04-28 13:48 Robertxiao 阅读(325) 评论(0)  编辑 收藏 引用 所属分类: 天马行空


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理