Re: I need help FAST!!
- Posted by Robert Craig <rds at RAPIDEUPHORIA.COM> Jan 12, 2001
- 400 views
Mike The Spike writes: > I need code that accepts a sequence of files like > this; {"test.txt","crap.ex", ...} > It should also accept a sequence of keywords like > this; > {"key1","crapkey2","shitkeyword3", ...} > and that finally will replace every keyword it finds in > every file... Junko just handed me the following program that she wrote a couple of years ago, just before we created safe.e. She used it a couple of times for debugging purposes. It hasn't been thoroughly tested, so be careful. -- Utility to replace selected strings with new strings in -- selected files. -- Before running it, edit three constants: files, strings, direction. -- Run this program in a directory where all the selected files exist. -- Each time you run this program, it creates a new backup -- sub-directory under current directory where original selected -- files are copied and saved. All the selected files in the current -- directory will be replaced with new files containing the -- new strings. -- -- Backups are made, but be careful! -- -- Written by Junko C. Miura of Rapid Deployment Software include file.e constant files = { -- list of all the files to be converted "yourfile.ex", -- DO NOT put "safe.e" here "file2.ex" -- etc. } constant strings = { -- list of string pairs, {"fromString", "toString"} {"peek(", "safe_peek("}, {"poke(", "safe_poke("}, {"mem_copy(", "safe_mem_copy("}, {"mem_set(", "safe_mem_set("}, {"allocate(", "safe_allocate("}, {"allocate_low(", "safe_allocate_low("}, {"free(", "safe_free("}, {"free_low(", "safe_free_low("} } constant direction = 1 -- 1 = from "fromString" to "toString" direction -- 0 = reverse direction, ie, from "toString" to -- "fromString" direction constant TRUE = 1 constant ERROR = 2 function replaceStrInOneLine(sequence line, sequence fromStr, sequence toStr) -- Replace all occurrences of fromStr with toStr in a line of text integer prevIndex, index prevIndex = 0 index = match(fromStr, line) while index != 0 do index = index + prevIndex line = line[1 .. (index - 1)] & toStr & line[(index + length(fromStr)) .. length(line)] prevIndex = index + length(toStr) - 1 index = match(fromStr, line[(prevIndex + 1) .. length(line)]) end while return line end function procedure main() integer fn1, fn2 object line sequence fromStr, toStr sequence backup_dir -- find a new name for backup directory for i = 0 to 99 do backup_dir = sprintf("backup%d", i) if atom(dir(backup_dir)) then exit end if end for -- make a subdirectory called backup.. system("mkdir " & backup_dir & " > NUL", 2) puts(1, "saving original files in " & backup_dir & " directory\n") for i = 1 to length(files) do system("copy " & files[i] & " " & backup_dir, 2) fn1 = open(backup_dir & "\\" & files[i], "r") if fn1 = -1 then puts(ERROR, "couldn't open " & backup_dir & "\\" & files[i] & '\n') abort(1) end if fn2 = open(files[i], "w") if fn2 = -1 then puts(ERROR, "couldn't open " & files[i] & '\n') abort(1) end if while TRUE do line = gets(fn1) if atom(line) then exit end if for j = 1 to length(strings) do if direction = 1 then fromStr = strings[j][1] toStr = strings[j][2] else fromStr = strings[j][2] toStr = strings[j][1] end if line = replaceStrInOneLine(line, fromStr, toStr) end for puts(fn2, line) end while close(fn1) close(fn2) end for end procedure main() Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com