Notifications
Clear all

[Closed] Replace string in a text file

I have trouble to raplace a string. I can’t use SetIniSettings.
I make two functions for this propose but is not working very well:(

Mabe someone know beter way to do this:


fn readFile fPath =
(
	local f = openFile fPath mode:"r"
	local data = ""
	while not eof f do
	(
		data += (readline f)+"
"
	)
	flush f
	close f
	return data
)

fn setReplaceText str newStr fPath =
(
	local f = openFile fPath mode:"a+"
	if (skipToString f str  ) == undefined do return false
	if (local strPos = filePos f) != undefined do local strLen = (readLine f).count
	flush f
	close f

	if strLen == 0 do return false
	
	local txt = readFile fPath
	local newTxt = replace txt (strPos-12) strLen newStr 
	
	f = openFile fPath mode:"w"
	format "%" newTxt to:f
	flush f
	close f
)

2 Replies

this maybe helpful .
rules:

  1. first get file with first button .
  2. input the words to replace , format : a,b,c – here i use the “,” for the separater , if u dont like it , open the script and change the separater define at top of the script . after change , u should write input here in ur rule .
  3. input the new words , here , the count must be same as the oldwords line . for i replace them one to one . format: aaa,bbb,ccc
  4. if u want create a new file for the result, here type in the fileName . default is :“temp” . so if ur filename is “xxxx.txt” , then new file will at same folder , name is “temp.txt”
  5. if u chose “overwrite old file” , it will overwrite the old file . the newfile edittext will not work .

ok , have fun .

rollout replaceWords "replace words: [email="http://forums.cgsociety.org/haibo.lan@gmail.com"]haibo.lan@gmail.com[/email]" 
(
-- var define -- 
local thetargetfile = undefined 
local oldwordsArray = undefined
local newwordsArray = undefined 
local separater = ","
-- interface define -- 
button gettxtfile "press here to get txt file" width:280
edittext oldwords "old   words:"   
edittext newwords "new words:" 
edittext createNewFile "create new file:" text:"temp"
checkbutton overwriteoldfile "overwrite oldfile" width:80 align:#left checked:on 
button doreplace "do replace" width:100 pos:(overwriteoldfile.pos+[100,0])
label nothing1 "-- --" pos:(doreplace.pos+[140,0])
-- function define -- 
fn replacefunction thefile old new = 
(
escapeenable = on 
local strs = #() 
if old.count == new.count do 
 ( 
 local theFileStream = openfile thefile mode:"r" 
 while not eof thefilestream do 
  (
  append strs (readline thefilestream) 
  ) -- end while . 
  close theFilestream  
  -- loop strs for replace  . 
  for i in 1 to strs.count do 
   (
  for j in 1 to old.count do 
   (
   while (findstring strs[i] old[j]) != undefined do 
	(
	strs[i] = replace strs[i]  (findstring strs[i] old[j]) old[j].count new[j]
	) -- end while .
   ) -- end j.
  ) -- end i .
 ) -- end if .
return strs
) -- end fn . 
-- action define -- 
on replaceWords open do clearlistener() 
-----------------------
on gettxtfile pressed do 
(
thetargetfile = getopenfilename caption:"select a txt file for work with: " \
   filename:"" types:"ms files|*.ms|txt files|*.txt|all files|*.*" 
if thetargetfile != undefined do gettxtfile.caption = "current file: " + (filenamefrompath thetargetfile)
)
-----------------------
on doreplace pressed do 
(
nothing1.text = "working..."
-- collect words . -- 
if theTargetFile != undefined then 
 (
 oldwordsArray = if oldwords.text.count==0 then  undefined else (filterstring oldwords.text separater )
 newwordsArray = if newwords.text.count==0 then undefined else (filterstring newwords.text separater ) 
 
 if oldwordsArray != undefined and newwordsArray != undefined do 
  ( 
  local newstrs = replacefunction theTargetFile oldwordsarray newwordsarray  
  local newfilename = if overwriteoldfile.checked then theTargetFile else \
	  ((getfilenamepath theTargetFile)+createNewFile.text+(getfilenametype theTargetFile))
  createfile newfilename
  local newfilestream = openfile newfilename mode:"w"  
  for i in newstrs do 
   (
   print i to:newfilestream 
   )
  close newfilestream 
  
  )
 )
nothing1.text = "-- --"
)
----------------------- 
) createdialog replaceWords 300 130 

Good! This is it. Thanks for you help Lan:thumbsup: