Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read and write to same file??? 1

Status
Not open for further replies.

qajussi

Programmer
Mar 22, 2004
236
US
Hi!

I am trying to read a file and when found a certain word I would like to add a line or two from that point on.
How do I that ??
When I tried to do this, it reads and write nothing to the file..

Do I use??
Set fso = CreateObject("Scripting.FileSystemObject")
Set objRF = fso_OpenTextFile("C:\ReadWrite.xml",8,True)


Example)

ReadWrite.xml

<Name>
<Last>
Smith
</Last>
<First>
John
</First>
</Name>


to
find </First> and add <Middle>James</Middle>

<Name>
<Last>
Smith
</Last>
<First>
John
</First>
<Middle>James</Middle>
</Name>

How do I read and write to same file??

Thanks...
 
If you are working with XML, then there is no reason whatsoever to explicitly read and write to the file. The XML Document Object Model (DOM) is a much better way to handle this.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thank you TomThumbKP.

I guess I could use VBS and DOM to accomplish my task.
YES I agree with you about DOM being be a better choice to insert XML tags.

Acutally I started out doing this with DOM.
It works fine with one XML file but when I ran my VBS with DOM on my folder containing 20K XML files, it failed to run.

It told me that it doesn't recognize the DOM object even though it was same code which ran on one xml file.

I played with it for one week and I decided to use VBS string manipulation to finish the task.

That task was done and I am doing another task with similar ideas.

I just find the file name and when found, i just have to update that xml file by inserting one or two line of tags.

I am trying to open one file and read and write to it but I am having a little difficulty doing it with only object.

Can I do it with only one object like above??
Set fso = CreateObject("Scripting.FileSystemObject")
Set objRF = fso_OpenTextFile("C:\ReadWrite.xml",8,True)

Do I need to make another create object?
Set objRF = fso.CreateTextFile("C:\ReadWrite.xml",2,True)
Problem is, it won't let me create same file name...
Any idea?
Thank you very much for your time..
 
Hello gajussi,

You cannot read and write at the same time (ie insert something in the middle of file while reading) in vbs. You can only [1] read, find, modified, continue to read everything to the memory. Then [2] close the file. [3] reopen it again, and [4] write back everything modified.

If you want to modify only at that one place, you can do this.
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set objRF = fso.OpenTextFile("C:\ReadWrite.xml",1,True)
s = objRF.readall
objRF.close
sig = "<First>" & vbcrlf & "John" & vbcrlf & "</First>" & vbcrlf
sig2 = "<Middle>" & vbcrlf & "James" & vbcrlf & "</Middle>" & vbcrlf
s = replace(s, sig, sig & sig2)
set objRF = fso.OpenTextFile("C:\ReadWrite.xml",2,True)
objRF.write s
objRF.close
set objRF = nothing
set fso = nothing
This approach is not very robust in the sense that any variation, such as line breaker not vbcrlf, a blank space after <First> or upper/lower case change, though equivalent in xml, would make the replace failed. But if you have good control over the original construction, this way of doing it is convenient.

regards - tsuji

Do I need to make another create object?
Set objRF = fso.CreateTextFile("C:\ReadWrite.xml",2,True)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top