SEANS
(Lurker)
2004-01-08 05:48 PM
Need Script to replace strings in a text file

I am looking for a script to accomplish the follwoing.

Open a text file on the users desktop c:\\documents and setting\username\desktop\filename.
Search the file for a string and replace it with a new string. Then save the new file.
I have been told to look at READLINE, WRITELINE AND INSTR, but I am a newbie to Kixtart and scripting so I am am at a loss. Any help will be greatly apriciated.

Sean


ShaneEP
(MM club member)
2004-01-08 06:16 PM
Re: Need Script to replace strings in a text file

Will you be replacing an entire line or just a part of a line? What will be the text you will be replacing?

Sealeopard
(KiX Master)
2004-01-08 06:18 PM
Re: Need Script to replace strings in a text file

Did you see the examples, too? They show how to read/write text files. and did you search this BBS or even take a look at the UDF Forum, which also contains some UDFs for this?

See for example InsertLine() - insert, replace, or delete any line in a text file , ReadFile() - Read a file into an array , and WriteFile() - Write an array to a file


ShawnAdministrator
(KiX Supporter)
2004-01-08 06:19 PM
Re: Need Script to replace strings in a text file

Is there any way that you could just simply host a new version of this file on your NETLOGON shares and then copy it down upon user login ? Obviously, if everyones version is different, this isn't an option - but just had to ask.

Les
(KiX Master)
2004-01-08 06:29 PM
Re: Need Script to replace strings in a text file

There are also external utilities like Howard's QReplace.
http://home.comcast.net/~habullock/Perlutilities.htm


SEANS
(Lurker)
2004-01-08 06:47 PM
Re: Need Script to replace strings in a text file

I This is a sample of the file I need to edit. I need to change lines 18 and 19( alternate directory S: and shell working directory:s:) i I cannot just replace the whole file because each one has a different username. I have been serching the board for examples but like I said I am new to this so I am trying to make sense of it all. Thanks for the help so far.

screen mode id:i:2
desktopwidth:i:1024
desktopheight:i:768
session bpp:i:8
winposstr:s:0,1,0,0,878,716
auto connect:i:0
full address:s:termserv1.ghmc.net
compression:i:1
keyboardhook:i:2
audiomode:i:0
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:1
displayconnectionbar:i:1
username:s:username
domain:s:domain
alternate shell:s:\\servername\c\integra\destiny\filename.exe -d "First One Database"
shell working directory:s:\\servername\c\integra\destiny
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000D0D61E03189DD1488EFBD3B5A34D4BCA0000000008000000700073007700000003660000A8000000100000002B311A643CC3435C37FD822138D71DF70000000004800000A000000010000000EA4B93DAA6E81DCA6F98B27EC294FC2E0802000073D3267340AE74B3897F4543AD60E139E2F59559081A3C3B3D8E989F5ABC67949AD9F4AE7C092AD3DE5C15381F3373552BC983D81D33B8F85360FC417FA8764A452ACAC61DD3D618F245D0B7553382DA3A1E608A88FE588FE9B5E6A92C9AC7B97376E9E83384A5D35B98FCC326FECE095ACE37548DA1922367E22F57157EFECF326365798D8A78C58EF51CAF46342B23C847332C9A8FC703A1DA70B9D089BBC8780C2CF24E07EAEC165295EE3320214AC3C88996F568B6B1F265445D58FB6F93AA427171DF7DF6B14171E85A2A96A86BC71870A69271DC964544F9969994555BB652F4F4CAB3792A369FC55CC5B5B591E61FCA93CF6077B2343318328D192E40DB3B451FB3D8A9FB5D5F602D7BDD9C47D2E9378234CFA4E0DCF2E764273FAEBE38CE5123033BDA283117C267B090BAD11960BB7907A28EE30EFECF69277E7A2D47D91EFEC14D26CCBCE9E1520CFA4808FB414DF8B7E0C4FE924CD765F39F7BCC45A708120727F8EDABF1008B634C27E6D232FA4B1BD49B962D382A2ED255900BCE6A52DBEA79CB375945FD2A12C99701CB0E3E63294FE4EC0082AADF28F64BEA85E468CD807C7579101638FAA475D94BB9AB7D1BEF1C89B7887BEB01B9565A5DCFFDAF82C7B8DC92F7980050FB0D6044784EFE59DA0F8BB0C53D944764847FB3198A5BD146485FE6251B6F04721D61F42D07D117DEF2E9F0D3479250B3B53C288A1E9EE3F61D4D9014000000DD893FEBC278867030AB9DB367103EF8EDC4C907
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
bitmapcachepersistenable:i:1


SEANS
(Lurker)
2004-01-08 06:50 PM
Re: Need Script to replace strings in a text file

I will just be replacing part of the line.

ShaneEP
(MM club member)
2004-01-08 06:50 PM
Re: Need Script to replace strings in a text file

What part? I dont see anything in the lines about userIDs...Unless they are either integra or destiny? Are you changing the server name?

Sealeopard
(KiX Master)
2004-01-08 06:56 PM
Re: Need Script to replace strings in a text file

Please explain why for example InserLine() does not work for you?

SEANS
(Lurker)
2004-01-08 07:19 PM
Re: Need Script to replace strings in a text file

I want to change alternate shell:s:\\servername\c\integra\destiny\filename.exe -d "First One Database" to alternate shell:s:d\integra\destiny\filename.exe -d "First One Database" and shell working directory:s:d\integra\destiny toshell working directory:s:\\servername\c\integra\destiny
Essentualy removing the UNC path and changing the drive letter.


ShaneEP
(MM club member)
2004-01-08 09:04 PM
Re: Need Script to replace strings in a text file

SEANS...

Try this code and see if it does the trick.

Code:
Break On
$null = SetOption ('NoVarsInStrings','On')
$null = SetOption ('Explicit','On')
Dim $null,$texttoreplace,$textreplacement,$inputfile,$inputhandle,$outputfile,$outputhandle,$line,$beforetext,$aftertext

$texttoreplace = '\\servername\c\'
$textreplacement = 'd\'

$inputfile = 'file.txt'
$inputhandle = FreeFileHandle()

If Open ($inputhandle, $inputfile) = 0
$outputfile = '%temp%\kxoutputfile.tmp'
$outputhandle = FreeFileHandle()
If Open ($outputhandle, $outputfile, 5) = 0
$line = ReadLine ($inputhandle)
While @Error = 0
If InStr ($line, $texttoreplace)
$beforetext = SubStr ($line, 1, InStr ($line,$texttoreplace)-1)
$aftertext = SubStr ($line, InStr ($line,$texttoreplace)+Len ($texttoreplace))
$line = $beforetext+$textreplacement+$aftertext
Endif
$null = WriteLine ($outputhandle, $line+@CRLF)
$line = ReadLine ($inputhandle)
Loop
$null = Close ($outputhandle)
Else
$null = MessageBox ('Unable to open the Output file','Error',0)
Endif
$null = Close ($inputhandle)
If Exist ($outputfile) and GetFileSize ($outputfile) > 0
Copy $outputfile $inputfile
Del $outputfile
Endif
Else
$null = MessageBox ('Unable to open the Input file','Error',0)
Endif



It resulted in the following file when run on my laptop...

Quote:

screen mode id:i:2
desktopwidth:i:1024
desktopheight:i:768
session bpp:i:8
winposstr:s:0,1,0,0,878,716
auto connect:i:0
full address:s:termserv1.ghmc.net
compression:i:1
keyboardhook:i:2
audiomode:i:0
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:1
displayconnectionbar:i:1
username:s:username
domain:s:domain
alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"
shell working directory:s:d\integra\destiny
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000D0D61E03189DD1488EFBD3B5A34D4BCA0000000008000000700073007700000003660000A8000000100000002B311A643CC3435C37FD822138D71DF70000000004800000A000000010000000EA4B93DAA6E81DCA6F98B27EC294FC2E0802000073D3267340AE74B3897F4543AD60E139E2F59559081A3C3B3D8E989F5ABC67949AD9F4AE7C092AD3DE5C15381F3373552BC983D81D33B8F85360FC417FA8764A452ACAC61DD3D618F245D0B7553382DA3A1E608A88FE588FE9B5E6A92C9AC7B97376E9E83384A5D35B98FCC326FECE095ACE37548DA1922367E22F57157EFECF326365798D8A78C58EF51CAF46342B23C847332C9A8FC703A1DA70B9D089BBC8780C2CF24E07EAEC165295EE3320214AC3C88996F568B6B1F265445D58FB6F93AA427171DF7DF6B14171E85A2A96A86BC71870A69271DC964544F9969994555BB652F4F4CAB3792A369FC55CC5B5B591E61FCA93CF6077B2343318328D192E40DB3B451FB3D8A9FB5D5F602D7BDD9C47D2E9378234CFA4E0DCF2E764273FAEBE38CE5123033BDA283117C267B090BAD11960BB7907A28EE30EFECF69277E7A2D47D91EFEC14D26CCBCE9E1520CFA4808FB414DF8B7E0C4FE924CD765F39F7BCC45A708120727F8EDABF1008B634C27E6D232FA4B1BD49B962D382A2ED255900BCE6A52DBEA79CB375945FD2A12C99701CB0E3E63294FE4EC0082AADF28F64BEA85E468CD807C7579101638FAA475D94BB9AB7D1BEF1C89B7887BEB01B9565A5DCFFDAF82C7B8DC92F7980050FB0D6044784EFE59DA0F8BB0C53D944764847FB3198A5BD146485FE6251B6F04721D61F42D07D117DEF2E9F0D3479250B3B53C288A1E9EE3F61D4D9014000000DD893FEBC278867030AB9DB367103EF8EDC4C907
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
bitmapcachepersistenable:i:1





If this look right...You should be good to go. Just change the $inputfile = 'file.txt' line in the script to point to the file you are trying to change. And be sure to make a backup of the file first...I dont want you blaming me if it does something wrong.


Howard Bullock
(KiX Supporter)
2004-01-08 09:04 PM
Re: Need Script to replace strings in a text file

My Qreplace.exe can make short work of this.

Code:
c:\data\scripts>qreplace junk.txt "\\\\servername\\c" "d"

Utility: QREPLACE.EXE
Written by: Howard A. Bullock (habullock@comcast.net)

Copyright 2002

Replaces text in one or more files.
Processing: junk.txt (2 replacements)



Result:
alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"
shell working directory:s:d\integra\destiny


ShaneEP
(MM club member)
2004-01-08 09:08 PM
Re: Need Script to replace strings in a text file

Quote:

Please explain why for example InserLine() does not work for you?




Jens...

I tried the InsertLine() function and it didnt work properly. I ran this...

Code:
$null = insertline('file.txt',17,'alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"',1)
$null = insertline('file.txt',18,'shell working directory:s:d\integra\destiny',1)

Function insertline($a, $b, optional $c, optional $d)
Dim $e,$f,$h,$x,$y
If $b<0 Exit -3 EndIf
If $c="" $d=1 EndIf
$f="%temp%\~kixil00.tmp" ; temporary file to use
Del $f
If $d<>1 $d=0 EndIf
$e=FreeFileHandle
$insertline=-2
If $e=0 Exit -2 EndIf
$insertline=Open($e,$a)
If $insertline<>0 Exit @Error EndIf
$h=FreeFileHandle
If $h=0
$insertline=-2
$x=Close($e)
Exit -2
EndIf
$insertline=Open($h,$f,5)
If $insertline<>0
$x=Close($e)
Exit @Error
EndIf
If $b<>0
For $x=0 To $b-1
$y=ReadLine($e)
If @Error<>0
$x=Close($e)
$x=Close($h)
Del $f
$insertline=-3
Exit -3
Else
$insertline=WriteLine($h,$y+@Crlf)
EndIf
Next
EndIf
$insertline=WriteLine($h,$c)
$y=ReadLine($e)
If @Error<>0
$x=Close($e)
$x=Close($h)
Del $f
$insertline=-3
Exit -3
EndIf
If $d=0
$x=WriteLine($h,$y+@Crlf)
EndIf
$y=ReadLine($e)
While @Error=0
$insertline=Writeline($h,$y+@Crlf)
$y=ReadLine($e)
Loop
$x=Close($e)
$x=Close($h)
Copy $f $a
Del $f
$insertline=0
Exit 0
EndFunction



And got this in the text file...

Quote:

screen mode id:i:2
desktopwidth:i:1024
desktopheight:i:768
session bpp:i:8
winposstr:s:0,1,0,0,878,716
auto connect:i:0
full address:s:termserv1.ghmc.net
compression:i:1
keyboardhook:i:2
audiomode:i:0
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:1
displayconnectionbar:i:1
username:s:username
domain:s:domain
alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000D0D61E03189DD1488EFBD3B5A34D4BCA0000000008000000700073007700000003660000A8000000100000002B311A643CC3435C37FD822138D71DF70000000004800000A000000010000000EA4B93DAA6E81DCA6F98B27EC294FC2E0802000073D3267340AE74B3897F4543AD60E139E2F59559081A3C3B3D8E989F5ABC67949AD9F4AE7C092AD3DE5C15381F3373552BC983D81D33B8F85360FC417FA8764A452ACAC61DD3D618F245D0B7553382DA3A1E608A88FE588FE9B5E6A92C9AC7B97376E9E83384A5D35B98FCC326FECE095ACE37548DA1922367E22F57157EFECF326365798D8A78C58EF51CAF46342B23C847332C9A8FC703A1DA70B9D089BBC8780C2CF24E07EAEC165295EE3320214AC3C88996F568B6B1F265445D58FB6F93AA427171DF7DF6B14171E85A2A96A86BC71870A69271DC964544F9969994555BB652F4F4CAB3792A369FC55CC5B5B591E61FCA93CF6077B2343318328D192E40DB3B451FB3D8A9FB5D5F602D7BDD9C47D2E9378234CFA4E0DCF2E764273FAEBE38CE5123033BDA283117C267B090BAD11960BB7907A28EE30EFECF69277E7A2D47D91EFEC14D26CCBCE9E1520CFA4808FB414DF8B7E0C4FE924CD765F39F7BCC45A708120727F8EDABF1008B634C27E6D232FA4B1BD49B962D382A2ED255900BCE6A52DBEA79CB375945FD2A12C99701CB0E3E63294FE4EC0082AADF28F64BEA85E468CD807C7579101638FAA475D94BB9AB7D1BEF1C89B7887BEB01B9565A5DCFFDAF82C7B8DC92F7980050FB0D6044784EFE59DA0F8BB0C53D944764847FB3198A5BD146485FE6251B6F04721D61F42D07D117DEF2E9F0D3479250B3B53C288A1E9EE3F61D4D9014000000DD893FEBC278867030AB9DB367103EF8EDC4C907
shell working directory:s:d\integra\destinydisable menu anims:i:1
disable themes:i:0
bitmapcachepersistenable:i:1





Looks like something is off in this UDF...Could someone else try it and confirm its not just something I did?


Les
(KiX Master)
2004-01-08 09:09 PM
Re: Need Script to replace strings in a text file

Not being a KiX purist, I'm all for "short work"

Someone should write a FAQ on this... seems to come up often enough.


ShaneEP
(MM club member)
2004-01-08 09:14 PM
Re: Need Script to replace strings in a text file

I would much rather stick with purely KiX if possible. Maybe someone just needs to write a UDF for it that works...

Howard Bullock
(KiX Supporter)
2004-01-08 09:19 PM
Re: Need Script to replace strings in a text file

Given some free time I could provide this functionality as a UDF but it would not be pure KiX. What am I saying...


I have already written it as KiX COM code.
See: RFC: Regular Expression UDFs


Les
(KiX Master)
2004-01-08 09:20 PM
Re: Need Script to replace strings in a text file

See my sig? Usually I only write code if the effort to code is less than the effort to do without the code.

Well, sometimes I get bored and may rise to an interesting challenge. Maybe not.


Sealeopard
(KiX Master)
2004-01-08 10:49 PM
Re: Need Script to replace strings in a text file

InsertLine() works as advertised. However, you should read the function header beforehand as it specifically states
Quote:


; Remarks: InsertLine does not automatically append a <Carriage Return>, so if you want to write a
; <Carriage Return>, you should add it to the string (as in : $LineToWrite + @CRLF).




You omitted the @CRLF at the end of your strings to be inserted.


ShaneEP
(MM club member)
2004-01-08 11:23 PM
Re: Need Script to replace strings in a text file

You are correct Jens...My bad...Thats why I wanted someone else to try it.

SEANS...

This may be a better alternative to the script I posted earlier since this can more easily be modified for future needs. Just change the 'file.txt' variable in the first two lines, to the path and filename you need to modify.

Code:
$null = insertline('file.txt',17,'alternate shell:s:d\integra\destiny\filename.exe -d "First One Database"'+@CRLF,1)
$null = insertline('file.txt',18,'shell working directory:s:d\integra\destiny'+@CRLF,1)

Function insertline($a, $b, optional $c, optional $d)
Dim $e,$f,$h,$x,$y
If $b<0 Exit -3 EndIf
If $c="" $d=1 EndIf
$f="%temp%\~kixil00.tmp" ; temporary file to use
Del $f
If $d<>1 $d=0 EndIf
$e=FreeFileHandle
$insertline=-2
If $e=0 Exit -2 EndIf
$insertline=Open($e,$a)
If $insertline<>0 Exit @Error EndIf
$h=FreeFileHandle
If $h=0
$insertline=-2
$x=Close($e)
Exit -2
EndIf
$insertline=Open($h,$f,5)
If $insertline<>0
$x=Close($e)
Exit @Error
EndIf
If $b<>0
For $x=0 To $b-1
$y=ReadLine($e)
If @Error<>0
$x=Close($e)
$x=Close($h)
Del $f
$insertline=-3
Exit -3
Else
$insertline=WriteLine($h,$y+@Crlf)
EndIf
Next
EndIf
$insertline=WriteLine($h,$c)
$y=ReadLine($e)
If @Error<>0
$x=Close($e)
$x=Close($h)
Del $f
$insertline=-3
Exit -3
EndIf
If $d=0
$x=WriteLine($h,$y+@Crlf)
EndIf
$y=ReadLine($e)
While @Error=0
$insertline=Writeline($h,$y+@Crlf)
$y=ReadLine($e)
Loop
$x=Close($e)
$x=Close($h)
Copy $f $a
Del $f
$insertline=0
Exit 0
EndFunction



Radimus
(KiX Supporter)
2004-01-08 11:32 PM
Re: Need Script to replace strings in a text file

have you looked at ReadFile() WriteFile() and a simple for each with a join(split($line,"old string"),"new string")

Kdyer
(KiX Supporter)
2004-01-09 05:14 AM
Re: Need Script to replace strings in a text file

Wow! This is starting to look a lot better than the simple HOSTS change script:
Update/change HOSTS file

Kent


@lejo @rias
(Starting to like KiXtart)
2004-01-09 03:37 PM
Re: Need Script to replace strings in a text file

Try This...
;to read file and put in array
Function ReadFile($file)
Dim $lf, $t
$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

;func to replace string a to b
Function ReplaceSTR($string, $from, $to)
If InStr($string, $from)
$string=$to
$cont=$cont+1
Exit 0
Else
$ReplaceSTR=$string
Exit 1
EndIf
EndFunction

;func to write array in a new file
Function WriteFile($array,$file)
Dim $f
$f=FreeFileHandle
$=Open($f,$file,5)
If @error Exit @error EndIf
For Each $line In $array
$=WriteLine($f,$line + @crlf)
Next
$=Close($f)
Exit @error
EndFunction

;cod for replace string in the file using funcs
$Usuario_Anterior="xxxx"
$Usuario_Actual="yyyy"

$file='%allusersprofile%\Datos de programa\IBM\Personal Communications\Medellin.acg'
$arrFile=ReadFile($file)
For $line =0 to Ubound($arrFile)
$arrFile[$line]=ReplaceSTR($arrFile[$line],$Usuario_Anterior,$Usuario_Actual)
Next
$=WriteFile($arrFile,$file)



Sealeopard
(KiX Master)
2004-01-09 03:40 PM
Re: Need Script to replace strings in a text file

ReplaceSTR() is just a convoluted way of SplitJoining a string as has already been explained in numerous posts.
Code:

$string=join(split($string,$search),$replace)