#46584 - 2003-10-11 01:59 PM
Edit Text File
|
smc12
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
|
|
|
|
#46587 - 2003-10-11 03:01 PM
Re: Edit Text File
|
smc12
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
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
|
|
|
|
#46591 - 2003-10-12 04:28 AM
Re: Edit Text File
|
Radimus
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 ]
|
|
Top
|
|
|
|
#46593 - 2003-10-12 03:43 PM
Re: Edit Text File
|
Radimus
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 ]
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 811 anonymous users online.
|
|
|