Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Write something at a specific place in a text file

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3255
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Write something at a specific place in a text file
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Write something at a specific place in a text file - 6/6/2005 4:14:30 AM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Hi,
I am trying to edit a text file to find a specific expression (with regular expression) = expression to replace(A).
Then, if the expression is find i replace it and if it's not find i m writting in a new line (no overwritte) my expression ( = my new expression (B)). The problem is that I don't want to write at the end of my file (not Appending) but after a specific line :
example : php.ini

[PHP]
A
Z
O
...

after changes :
[PHP]
B
Z
O
...

or

[PHP]
Z
O

after changes
[PHP]
B
Z
O

I don't know how to do...
 
 
Post #: 1
 
 Re: Write something at a specific place in a text file - 6/7/2005 2:07:08 AM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
Up [:0]
Any idea ?

(in reply to littlebouda)
 
 
Post #: 2
 
 Re: Write something at a specific place in a text file - 6/7/2005 5:37:42 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
You can read the text file as one string.
Example :

Function ReadFile
' Read whole file conLog as one string (max. possible length about 2 biljoen characters)
Dim objFile, ts
Set objFile = objFSO.GetFile(conLog)
If objFile.Size > 0 Then
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile = ts.ReadAll
ts.Close
End If
End Function

Then you can process the string ReadFile as needed.

Good luck !

< Message edited by didorno -- 7/20/2005 6:16:03 AM >

(in reply to littlebouda)
 
 
Post #: 3
 
 Re: Write something at a specific place in a text file - 6/9/2005 12:38:07 AM   
  littlebouda

 

Posts: 32
Score: 0
Joined: 5/13/2005
From: France
Status: offline
My problem isn't to read my file as a string but with this : i have to find [PHP] and replace the next line;
Do you think (for the example) that my pattern should be :
regexp.pattern = "[PHP]" &VbCrLf &"A" &VbCrLf
and the replace string :
"[PHP]" &VbCrLf &"B" &VbCrLf
But if there is not B at the next line...

(in reply to littlebouda)
 
 
Post #: 4
 
 Re: Write something at a specific place in a text file - 6/9/2005 7:31:16 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
littlebouda, can you do the next :
1. Insert B after each [PHP], no matter A
2. Remove all A (after B)

Do I understand you right that this gives your required change ?

(in reply to littlebouda)
 
 
Post #: 5
 
 Re: Write something at a specific place in a text file - 6/11/2005 12:01:25 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
littlebouda, this works for me for your example.
In writesomething.txt :

[PHP]
A
Z
O
...
...
[PHP]
Z
O
...

The vbscript file, running with cscript.exe :

conLog = "writesomething.txt"
conForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(conLog)
If objFile.Size > 0 Then
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile= ts.ReadAll
ts.Close
End If

WScript.Echo ReadFile

set oRE = New RegExp
oRE.Global = True
oRE.Pattern = "\[PHP\]" & "(\r\nA)?"
WScript.Echo oRE.Replace(ReadFile, "[PHP]" & vbCrLf & "B")


Then you obtain your desired result.
If you write the string to your file you are done.
Good luck.

< Message edited by didorno -- 7/20/2005 6:17:09 AM >

(in reply to littlebouda)
 
 
Post #: 6
 
 Re: Write something at a specific place in a text file - 6/27/2005 8:17:36 AM   
  diablo996

 

Posts: 3
Score: 0
Joined: 6/27/2005
From:
Status: offline
quote:
Then you obtain your desired result.
If you write the string to your file you are done.
Good luck.


Im a noob but how would you write the string to the file in this example. Thanks in advance!

(in reply to littlebouda)
 
 
Post #: 7
 
 Re: Write something at a specific place in a text file - 6/28/2005 6:25:41 AM   
  diablo996

 

Posts: 3
Score: 0
Joined: 6/27/2005
From:
Status: offline
Anyone? I just want to change the second echo to a write somehow.

(in reply to littlebouda)
 
 
Post #: 8
 
 Re: Write something at a specific place in a text file - 6/28/2005 8:18:42 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
The next piece of code writes the whole new string to a file conLog.
Add it to the given code.

TekstIn = oRE.Replace(ReadFile, "[PHP]" & vbCrLf & "B")
conLog = "Your new file name"
OpenTextFileTest(TekstIn)


Sub OpenTextFileTest(TekstIn)
' Write text in logfile : conLog, if file not exist, make it
Dim f
conForWriting = 2
conCreateFile = True
Set f = objFSO.OpenTextFile(conLog, conForWriting, conCreateFile)
f.Write TekstIn
f.Close
End Sub

(I have not tested the above part.)

Good luck.

< Message edited by didorno -- 7/20/2005 6:17:57 AM >

(in reply to littlebouda)
 
 
Post #: 9
 
 Re: Write something at a specific place in a text file - 6/28/2005 8:38:48 AM   
  diablo996

 

Posts: 3
Score: 0
Joined: 6/27/2005
From:
Status: offline
That worked great! Thanks for the help!

(in reply to littlebouda)
 
 
Post #: 10
 
 Re: Write something at a specific place in a text file - 6/30/2005 5:49:15 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
You are welcome !

(in reply to littlebouda)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Write something at a specific place in a text file Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts