Page 1 of 2 12>
Topic Options
#111474 - 2004-01-08 05:48 PM Need Script to replace strings in a text file
SEANS Offline
Lurker

Registered: 2004-01-08
Posts: 4
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

Top
#111475 - 2004-01-08 06:16 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
Will you be replacing an entire line or just a part of a line? What will be the text you will be replacing?
Top
#111476 - 2004-01-08 06:18 PM Re: Need Script to replace strings in a text file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11161
Loc: Boston, MA, USA
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


Edited by sealeopard (2004-01-08 06:22 PM)
_________________________
There are two types of vessels, submarines and targets.

Top
#111477 - 2004-01-08 06:19 PM Re: Need Script to replace strings in a text file
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8609
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.
Top
#111478 - 2004-01-08 06:29 PM Re: Need Script to replace strings in a text file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
There are also external utilities like Howard's QReplace.
http://home.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
#111479 - 2004-01-08 06:47 PM Re: Need Script to replace strings in a text file
SEANS Offline
Lurker

Registered: 2004-01-08
Posts: 4
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

Top
#111480 - 2004-01-08 06:50 PM Re: Need Script to replace strings in a text file
SEANS Offline
Lurker

Registered: 2004-01-08
Posts: 4
I will just be replacing part of the line.
Top
#111481 - 2004-01-08 06:50 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
What part? I dont see anything in the lines about userIDs...Unless they are either integra or destiny? Are you changing the server name?
Top
#111482 - 2004-01-08 06:56 PM Re: Need Script to replace strings in a text file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11161
Loc: Boston, MA, USA
Please explain why for example InserLine() does not work for you?
_________________________
There are two types of vessels, submarines and targets.

Top
#111483 - 2004-01-08 07:19 PM Re: Need Script to replace strings in a text file
SEANS Offline
Lurker

Registered: 2004-01-08
Posts: 4
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.

Top
#111484 - 2004-01-08 09:04 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
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.

Top
#111485 - 2004-01-08 09:04 PM Re: Need Script to replace strings in a text file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#111486 - 2004-01-08 09:08 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
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?

Top
#111487 - 2004-01-08 09:09 PM Re: Need Script to replace strings in a text file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Not being a KiX purist, I'm all for "short work"

Someone should write a FAQ on this... seems to come up often enough.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#111488 - 2004-01-08 09:14 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
I would much rather stick with purely KiX if possible. Maybe someone just needs to write a UDF for it that works...
Top
#111489 - 2004-01-08 09:19 PM Re: Need Script to replace strings in a text file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#111490 - 2004-01-08 09:20 PM Re: Need Script to replace strings in a text file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#111491 - 2004-01-08 10:49 PM Re: Need Script to replace strings in a text file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11161
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#111492 - 2004-01-08 11:23 PM Re: Need Script to replace strings in a text file
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2041
Loc: Tulsa, OK
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


Top
#111493 - 2004-01-08 11:32 PM Re: Need Script to replace strings in a text file
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
have you looked at ReadFile() WriteFile() and a simple for each with a join(split($line,"old string"),"new string")
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Chris S., Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
4 registered (Jochen, AndreLuiz, Flavien, Arend_) and 206 anonymous users online.
Newest Members
Sjaak, Biybucket, Markus1961, Ian231, kixnewbie12
17480 Registered Users

Generated in 0.088 seconds in which 0.03 seconds were spent on a total of 13 queries. Zlib compression enabled.

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