VBS script attempting to write to .rc file returns

2019-03-05 08:38发布

I am trying to find a process by which to edit and write to a resource .rc file; I attempted to use the sample code listed at How to increment values in resourse file by using vbscript but the last line in both samples returned the same error ( fso.OpenTextFile(rcfile, 2).Write rctext ) :

Error: Invalid procedure call or argument 
Code: 800A0005 
Source: Microsoft VBScript runtime error

I modified the script to write out to a .txt file and that worked fine, but I'm baffled as to what may be causing the problem writing out to a .rc file.

3条回答
倾城 Initia
2楼-- · 2019-03-05 09:01

It seems that the answer to my question required both of your answers(@MC ND and @Ekkehard.Horner); also, once I changed the vbs script to open and write to the .rc file in Unicode, which I'm not sure why I have to, the script was able to execute without error.

Here is the vbs script in it's final form:

Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Const DoNotCreate = false

rcFile  = "C:\Path\To\RC\File.rc"
major   = 4
minor   = 3
maint   = 2
build   = 1
version = major & "," & minor & "," & maint & "," & build

Set fso     = CreateObject("Scripting.FileSystemObject")
Set fileObj = fso.OpenTextFile(rcFile, ForReading, DoNotCreate, TristateTrue)
rcText      = fileObj.ReadAll
fileObj.Close

Set regex     = New RegExp
regex.Global  = True
regex.Pattern = "(PRODUCTVERSION|FILEVERSION) \d+,\d+,\d+,\d+"
rcText        = regex.Replace(rcText, "$1 " & version)

regex.Pattern = "(""(ProductVersion|FileVersion)"",) ""\d+, \d+, \d+, \d+"""
rcText        = regex.Replace(rcText, "$1 """ & Replace(version, ",", ", ") & """")

Set fileObj = fso.GetFile(rcFile)
Set textStream = fileObj.OpenAsTextStream(ForWriting, TristateTrue)
textStream.Write rcText
textStream.Close

The only thing that does not seem to work is the regex for replacing the ProduceVersion|FileVersion values, but hopefully I can hammer that out within a reasonable time.

查看更多
Fickle 薄情
3楼-- · 2019-03-05 09:03

From the linked sample (simplified)

rctext = fso.OpenTextFile(rcfile).ReadAll
rctext = ....
fso.OpenTextFile(rcfile, 2).Write rctext

The idea is read all the file, and as far as there is no variable holding a reference to the opened file, it is closed, then change what needs to be changed and open again the file, now for writing, and write the changed content to file

And, usually, it works. But sometimes the file opened for reading is not closed fast enough to later open it for writing.

To ensure the file is closed and then can be opened for writing, change the reading code to

set f = fso.OpenTextFile(rcfile)
rctext = f.ReadAll
f.Close
查看更多
别忘想泡老子
4楼-- · 2019-03-05 09:08

As your line

fso.OpenTextFile(rcfile, 2).Write rctext 

does three things (access fso, open file, write to it), there are many things that could go wrong. Please see this answer for ideas wrt to problems concerning the first two actions. Another answer concerns the write.

In your case, the evidence - works with a.txt, but not with b.rc - makes it highly improbable that the file's opening is to blame (so .Close won't save you). I suspect that the .rc contains Unicode (UTF-8/UTF-16) data that the textstream can't encode.

So either use the unicode parameter to read/write open the file with UTF-16 encoding or an ADODB.Stream for UTF-8.

查看更多
登录 后发表回答