Computing.Net > Forums > Programming > text replaceing via *.bat

text replaceing via *.bat

Reply to Message Icon

Original Message
Name: renis
Date: July 2, 2007 at 23:27:29 Pacific
Subject: text replaceing via *.bat
OS: XP
CPU/Ram: 1GB
Model/Manufacturer: pentium 4
Comment:

I need to change/replace a word in a row in batch file. for example "localhost" to "AAA". Supose there are no diference to the same in *.txt however can someone help me? thanks!


Report Offensive Message For Removal


Response Number 1
Name: renis
Date: July 3, 2007 at 00:42:35 Pacific
Subject: text replaceing via *.bat
Reply: (edit)

I found one way to do this but there is one litte problem again. I don`t want to change all "Jim" to "James" but in one row only. who can help me with this .vbs script? thanks!

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: July 3, 2007 at 02:22:17 Pacific
Subject: text replaceing via *.bat
Reply: (edit)

the replace function has an optional "start" and "count" argument. you can use that. Put count = 1, eg
[code]
Replace(strText, "Jim ", "James ",1,1)
[/code]
it will replace just 1 instance of Jim.


Report Offensive Follow Up For Removal

Response Number 3
Name: renis
Date: July 4, 2007 at 01:08:44 Pacific
Subject: text replaceing via *.bat
Reply: (edit)

thanks! it works. and if I want to change it in two different rows? :)


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: July 4, 2007 at 01:38:58 Pacific
Subject: text replaceing via *.bat
Reply: (edit)

then you can switch from using ReadAll, to Readline, ie go through the files line by line. That way you can determine which lines you want to stop replacing. this is just one suggestion.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: text replaceing via *.bat

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge