Page 1 of 1 1
Topic Options
#46584 - 2003-10-11 01:59 PM Edit Text File
smc12 Offline
Lurker

Registered: 2003-10-11
Posts: 3
I have a confuguration file that has 6 lines of text in it. It is not an INI file. I need to replace the 3rd line from "MYID , IDNAME" to "MYID , %COMPUTERNAME%". Not sure how to do this. Thanks in advance.
Top
#46585 - 2003-10-11 02:05 PM Re: Edit Text File
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I think it is explained in one of my FAQs. Basically, you open it for read, open a new file for write, readline, count lines, string compare, replace, writeline, close.

...or use Howard's QReplace.
http://mywebpages.comcast.net/habullock/Perlutilities.htm
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#46586 - 2003-10-11 02:12 PM Re: Edit Text File
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'm guessing that if you posted the 6 lines, someone just might just write the code for you.

Hopefully, the lines are short or you would get ticketted by the long-line police.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#46587 - 2003-10-11 03:01 PM Re: Edit Text File
smc12 Offline
Lurker

Registered: 2003-10-11
Posts: 3
This is the file I need to edit:
[STARTUP SECTION]
COM3, ON, 19200, DTR
MYID , ID
Mode , AUTO, , , Delete
STATUS, C:\Program Files\Axiohm\DLStatus.txt
PATH, C:\Program Files\Axiohm
DATA_FILE, C:\Program Files\axiohm\Counts.txt

This is the script that isn't working...

If Exist ("C:\Program Files\Axiohm\cfgDL01.txt") = 0
Quit
EndIf

$file="C:\Program Files\Axiohm\cfgDL01.txt"
$file2="C:\Program Files\Axiohm\cfgDL01.new"

If Open(1,$file)
EndIf

If Open(2,$file2,5)
EndIf

$line=ReadLine(1)
While (@error = 0)
Select
Case
$line="MYID , ID"
If (WriteLine(2,"MYID , " + %COMPUTERNAME%) <> 0)
EndIf
Case
$line="$line"
If (WriteLine(2, $line <>0)
EndIf
EndSelect
$line=ReadLine(1)
Loop
Close(1)
Close(2)

Top
#46588 - 2003-10-11 03:26 PM Re: Edit Text File
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you should use the [Code][/code] tags when posting code. to keep the layout and readability.

then looking at your code:
Case $line="$line"
If (WriteLine(2, $line <>0)

this is obviously what you have intended as for other cases. well, it isn't.
kixtart needs to process your "$line", that is insert the line inside text.
after that, it needs to compare that text with the initial $line. it may or may not be the same.
check from manual what to do in this type of case.

the writeline has an syntax error, missing quote.
a quick re-write of your script would lead me to:

$file="C:\Program Files\Axiohm\cfgDL01.txt" 
$file2="C:\Program Files\Axiohm\cfgDL01.new"

if not Exist ($file)
"no file to edit, quitting..."
sleep 2
Quit
EndIf

if Exist ($file2) ;we want the new to be clean.
del $file2
EndIf

If Open(1,$file)
"can't open file for reading, quitting..."
sleep 2
Quit
EndIf

If Open(2,$file2,5)
"can't open file for writing, quitting..."
sleep 2
Quit
EndIf

$line=ReadLine(1)
While @error = 0
Select
Case $line="MYID , ID"
$rc=WriteLine(2,"MYID , " + @wksta + @crlf)
Case "others"
$rc=WriteLine(2,$line + @crlf)
EndSelect
$line=ReadLine(1)
Loop
$rc=Close(1)
$rc=Close(2)
_________________________
!

download KiXnet

Top
#46589 - 2003-10-11 03:33 PM Re: Edit Text File
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
A simple IF ELSE ENDIF would work too in place of the SELECT CASE.

Did you want the % chars in the file for %COMPUTERNAME% to be expanded later or did you want to expand %COMPUTERNAME% before? If you don't want the % in the file, Jooel's use of the @WKSTA macro is cleaner.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#46590 - 2003-10-12 02:28 AM Re: Edit Text File
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The filestructure you've posted can easily be converted to an .INI file structure. This would make manipulating the file a one-line KiXtart code fragment.
_________________________
There are two types of vessels, submarines and targets.

Top
#46591 - 2003-10-12 04:28 AM Re: Edit Text File
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$file=" "
$arr_text = readfile($file)
for $line = 0 to ubound($arr_text)
if $arr_text[$line] = "whatever"
$arr_text[$line] = "something else"
endif
next
$=writefile($file,$arr_text)

BTW.. readfile() and writefile() are UDFs

[ 12. October 2003, 04:42: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#46592 - 2003-10-12 02:39 PM Re: Edit Text File
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Hey, Rad, WriteFile() doesn't seem to be posted in the UDF Forum. Maybe include a link to the thread dealing with WriteFile()?
_________________________
There are two types of vessels, submarines and targets.

Top
#46593 - 2003-10-12 03:43 PM Re: Edit Text File
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$file is filename... returns an array of each line in $file
code:
 
Function ReadFile($file)
Dim $lf, $f
$lf=chr(10)
$f=freefilehandle
$=open($f,$file)
if @error exit @error endif
do $t=$t+$lf+readline($f) until @error
$=close($f)
$ReadFile=split(substr($t,2),$lf)
EndFunction

$file is filename
$array is array of text to be written to file. CRLF will be automatically appended to each line
code:
 
Function WriteFile($file,$array)
Dim $f, $line
If Not VarType($Array) & 8192 Exit(1) EndIf
$f=freefilehandle
$=open($f,$file,5)
if @error exit @error endif
for each $line in $array
$=writeline($f,$line+@crlf)
if @error exit @error endif
endif
$=close($f)
EndFunction

$array is array of text
$find is string of text to match
$inclusive is whether to return lines with the string or without the string
code:
 
Function FilterArray($array,$find, optional $inclusive)
Dim $lf, $t, $l, $sp
$lf=chr(10) $sp=chr(32)
If Not VarType($Array) & 8192 Exit(1) EndIf
for each $l in $array
if (instr($l,$find) and $inclusive and trim($l)> $sp)
or (not instr($l,$find) and not $inclusive and trim($l)> $sp)
$t=$t+$lf+$l
endif
next
$FilterArray=split(substr($t,2),$lf)
EndFunction



[ 12. October 2003, 15:48: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1376 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.158 seconds in which 0.091 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org