tags:

views:

993

answers:

6

How can we read and write some string into a text file using VBScript? I mean I have a text file which is already present so when I use this code below:-

Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\New\maddy.txt",1,1) 

This opens the file only for reading but I am unable to write anything and when I use this code:-

Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\New\maddy.txt",2,1)

I can just use this file for writing but unable to read anything. Is there anyway by which we can open the file for reading and writing by just calling the OpenTextFile method only once.

I am really new to VBScript. I am only familiar with C concepts. Is there any link to really get me started with VBScript?

I guess I need to have a good knowledge of the objects and properties concepts.

A: 

Don't think so...you can only use openTextFile for reading (1), writing (2), or appending (8). Reference here.

If you were using VB6 instead of VBScript, you could do:

Open "Filename" [For Mode] [AccessRestriction] [LockType] As #FileNumber

Using the Random mode. For example:

Open "C:\New\maddy.txt" For Random As #1
pianoman
I am really new to vbscripting.I am just familiar with my C concepts. Can anyone please suggest me any link to get really get my vbscripting started. I guess i need to have a good knowledge on the objects and properties concepts.. Any suggestions??
Here is a nice tutorial from W3Schools: http://www.w3schools.com/Vbscript/vbscript_intro.asp
pianoman
pianoman,Thanks a lot..I had gone through the link already and just tells abt the basic operations.I want to get into a bit more deep like the usage of objects,classes.I am really new to these terms as such.
Here a fairly-detailed explanation of those terms: http://www.4guysfromrolla.com/webtech/092399-1.shtml
pianoman
Pianoman,Thanks a lot
+1  A: 

You can create a temp file, then rename it back to original file:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.GetFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Set ts = objFile.OpenAsTextStream(1,-2)
Do Until ts.AtEndOfStream
    strLine = ts.ReadLine
    ' do something with strLine 
    objOutFile.Write(strLine)
Loop
objOutFile.Close
ts.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile 

Usage is almost the same using OpenTextFile:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)    
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    ' do something with strLine 
    objOutFile.Write(strLine & "kndfffffff")
Loop
objOutFile.Close
objFile.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile 
ghostdog74
Nice! Now let's see that with one `OpenTextFile` call :P
pianoman
A: 

For a complete reference to VBScript have a look at Windows Script 5.6 Documentation. This of course contains a full reference for the Scripting.FileSystemObject.

mdresser
Thanks a lot mdresser
+1  A: 

You could open two textstreams, one for reading

Set filestreamIn = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,1)

and one for appending

Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,8,true)

The filestreamIN can read from the begining of the file, and the filestreamOUT can write to the end of the file.

Tester101
A: 

See VBScript User's Guide and VBScript Language Reference.

aphoria
+1  A: 

You could also read the entire file in, and store it in an array

Set filestreamIN = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",1)
file = Split(filestreamIN.ReadAll(), vbCrLf)
filestreamIN.Close()
Set filestreamIN = Nothing

Manipulate the array in any way you choose, and then write the array back to the file.

Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",2,true)

for i = LBound(file) to UBound(file)
    filestreamOUT.WriteLine(file(i))
Next

filestreamOUT.Close()
Set filestreamOUT = Nothing
Tester101
that's one of the way, provided the file size is not huge.
ghostdog74