#186595 - 2008-04-01 04:05 PM
Re: Open all files in dir, find phrase, insert text, write file
[Re: Glenn Barnas]
|
Curious
Getting the hang of it
Registered: 2004-02-16
Posts: 61
|
As for basic search/replace, here's the basic idea...
$SearchData = 'xyzzy' ; insert when line containing this is found
$InsertData = 'AbraCadabra' ; data line to insert
$RootPath = 'D:\folder...' ; root path where files live
$Files = DirList($RootPath, 6) ; return a list of all files with complete paths
For Each $File in $Files
Move $File 'WorkFile.tmp' ; rename the file
If Open(2, 'workfile.tmp')
$ = RedirectOutput($File) ; output to original file
$Line = ReadLine(2)
While Not @ERROR
If InStr($Line, $SearchData)
$InsertData ? ; output extra (inserted) line
EndIf
$Line ? ; output original line
$Line = ReadLine(2)
Loop
$ = Close(2)
$ = RedirectOutput('') ; close output file
Del 'workfile.tmp'
EndIf
Next
This is UNTESTED, and presented to illustrate the logic flow. This could easily be modified to REPLACE a line or APPEND a line rather than INSERT a line. A nice mod might be to define a var $MODE, and use a Case statement to Insert, Replace, or Append depending on the value of $MODE. Glenn
I've started to work on this more intensively today, and took this code to start with. I understand that this also uses dirlist.udf. The basic works fine, but what's strange, is that the script doesn't do anything with file #1, it just cuts and paste file#1 into file#2(pastes into the beginning of file). File #2 is being processed, and the insertion is ok, but as you see.. the file#1 is then a part of this file. After this file#1 gets deleted, that is, this isn't totally true, the file is being put where the kix-script is started from, and has been renamed to 'workfile.tmp'
Haven't tried with more then 2 files yet.
Jon
Edited by Curious (2008-04-01 04:11 PM)
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
|
|
Top
|
|
|
|
Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-03-25 03:48 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-25 04:50 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
NTDOC
|
2008-03-25 06:29 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-25 08:24 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-03-26 11:06 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-26 01:35 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-03-26 03:36 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 04:05 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 08:57 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 11:27 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-02 12:32 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-03 01:20 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-03 03:59 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-04 05:03 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-04 08:17 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-05 12:11 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-05 01:45 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-05 04:21 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Les
|
2008-04-03 05:06 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 09:32 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
NTDOC
|
2008-03-26 08:10 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 09:57 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-01 01:05 PM
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 884 anonymous users online.
|
|
|