Jump to content
Sign in to follow this  
stibbar

finding text in a file and replacing it

Recommended Posts

stibbar

I am searching a file for text in between Http:// and :8080 and replace it in the file with a specific URL. The one thing is I am testing this on a single file to make it work. Just to note I will need to read lines from a file with the location of each file I want to find and replace the URLs. I have code in the script that I can reference to make that part work.

I have been reading over the forums, the help file and I was able to get everything I needed, and thanks to those who posted questions to get me in the right direction BTW, except one thing. How to write it back into the file.

So here is what I have;

#Include <File.Au3>
$string = FileOpen("D:\test\textfile.txt")
$TFind = StringRegExpReplace($string, "(http://)(.*)(:8080)","http://usethisurl:8080")
ConsoleWrite("Output:" & $TFind & @CRLF)

I added the console write so I could test it was working, and it is. Now I am running in to issues with trying to write that back into the file. I have tried _ReplaceStringInFile and FileWrite which leads me to beleive I am just not setting it up the right way. maybe I am putting things in the wrong order I am not sure. A nudge in the right direction would be great folks

The code for _ReplaceStringInFile looked like this

#Include <File.Au3>
$TFind = _ReplaceStringInFile("D:\test\textfile.txt"), "(http://)(.*)(:8080)","http://usethisurl:8080")
ConsoleWrite("Output:" & $TFind & @CRLF)
; or this when trying it with Varibles
$TextFileName = ("D:\test\textfile.txt")
$FText = "(http://)(.*)(:8080)"
$RText = "http://usethisurl:8080"
_ReplaceStringInFile($TextFileName,$FText,$RText)

Thanks for any feedback, direction or slap in the head.

/R

Stibb

Share this post


Link to post
Share on other sites
kylomas

stibbar,

Look at the help file for _replacestringinfile, your syntax is incorrect. There is ONE set of opening and closing parentheses.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

stibbar,

Also, the help file does NOT indicate that you can use regex syntax in the source abd/or target arguments.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

stibbar,

Try something like this (untested)

#Include <File.Au3>
$string = FileOpen("D:testtextfile.txt")
$TFind = StringRegExpReplace($string, "(http://)(.*)(:8080)","http://usethisurl:8080")
ConsoleWrite("Output:" & $TFind & @CRLF)

Local $h_fl = FileOpen("D:testtextfile_new.txt",2) ; write to file erasing previous contents
If $h_fl = -1 Then MsgBox(0,'Open Error','File open for write failed')

FileWrite($h_fl,$TFind)
FileClose($h_fl)
$h_fl = 0

Good Luck,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
BrewManNH

Hasn't anyone noticed that this script isn't putting the contents of the file into the variable string? It's putting the file handle of the file in it. So, I'm wondering how this was working for him at all?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
stibbar

Kylomas,

thanks for hte info and code I tried it and it did write to the file, it just replace everything with a 1 :)

i am going to play around with that some more to see if i can make it work.

i am thinking it has to do with the wildcards, unfortunaelty i have to use them as i have found serveral instance of different URLs in the file. just trying to make them all uniform so they all point to one place.

Stibb

Share this post


Link to post
Share on other sites
kylomas

Stibb,

See BrewmanNH's comments...$string is a handle to the file that you are opening.

Do something like :

$h_input = fileopen("yourfile")
$string = fileread($h_input)
...rest of the code

This is not tested as the anti-virus that I just updated is puking all over Scite4Autoit...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

stibb,

This should get you started...

#Include <File.Au3>
$h_open = FileOpen("D:\test\textfile.txt")
If $h_open = -1 Then MsgBox(0,'Open Error','File failed to open')
While 1
$string = FileReadLine($h_open)
If @error = -1 Then exitloop
$TFind &= StringRegExpReplace($string, "([url="http://)(.*)(:8080)"]http://)(.*)(:8080)",[url="http://usethisurl:8080[/url]"]http://usethisurl:8080[/url][/url]) & @crlf
WEnd
Local $h_fl = FileOpen("D:\test\textfile_new.txt",2) ; open for write erasing contents
If $h_fl = -1 Then MsgBox(0,'Open Error','File open for write failed')
FileWrite($h_fl,$TFind)
FileClose($h_fl)
$h_fl = 0

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
stibbar

So I have played around with this again after I was able to come back to it.

what I find is if I do this;

#include <file.au3>
#Include <string.au3>
#include <array.au3>
Local $filename = "D:testfilepos.txt"
Local $find = "URLToRemove:8080"
Local $replace = "NewURLIWant:8080"

Local $retval = _ReplaceStringInFile($filename, $find, $replace)
If $retval = -1 Then
MsgBox(0, "ERROR", "The pattern could not be replaced in file: " &amp; $filename &amp; " Error: " &amp; @error)
Exit
Else
MsgBox(0, "INFO", "Found " &amp; $retval &amp; " occurances of the pattern: " &amp; $find &amp; " in the file: " &amp; $filename)
EndIf
$msg = FileRead($filename, 1000)
MsgBox(0, "AFTER", $msg)

everything works like a champ

but if I do this;

#include <file.au3>
#Include <string.au3>
#include <array.au3>
Local $filename = "D:testfilepos.txt"
Local $find = StringRegExp($fopen, "(http://)(?.*)(:8080)") ; Change is here
Local $replace = "NewURLIWant:8080"

Local $retval = _ReplaceStringInFile($filename, $find, $replace)
If $retval = -1 Then
MsgBox(0, "ERROR", "The pattern could not be replaced in file: " &amp; $filename &amp; " Error: " &amp; @error)
Exit
Else
MsgBox(0, "INFO", "Found " &amp; $retval &amp; " occurances of the pattern: " &amp; $find &amp; " in the file: " &amp; $filename)
EndIf
$msg = FileRead($filename, 1000)
MsgBox(0, "AFTER", $msg)

Am I even right for thinking I can use the code to search for anything between Http:// and :8080? the reason is I have looked at some of the files and while they should all have the same URL, i found they do not hence the reason this is becoming a PITA.

again thanks in advance.

/R

Stibb</array.au3></string.au3></file.au3></array.au3></string.au3></file.au3>

Edited by stibbar

Share this post


Link to post
Share on other sites
kylomas

<p>Stibb,

Yes...although I am sure that one of the regexp heavy's has additional info.

A script called "SRE TESTER" is excellent for working out regex patterns. Do a search for it as I do not remember the link or the author.

kylomas

edit: additional info - the regex pattern should work...I have no idea whether or not your logic works. You need to play with it a bit. Pay attention to what the regexp returns. Also, the regexp needs a parameter of "3" (this editor is not rendering the code correctly when I post it). This will return an array of matches. All of this is untested as I do not have your input to test against.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

Stibbar,

Perhaps this will more correctly address your problem. From your origional post

#Include <File.Au3>
$string = FileOpen("D:testtextfile.txt")
$TFind = StringRegExpReplace($string, "(http://)(.*)(:8080)","http://usethisurl:8080")
ConsoleWrite("Output:" & $TFind & @CRLF)

If you want to write this to an output file then just add this

$file = fileopen("c:myouputfile.txt",2)
filewrite($file,$TFind)
fileclose($file)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
stibbar

Kylomas,

thanks Sir, i am looking at a few posts now, i hope i can work this out. will report back once this is solved.

/R

Stibb

Share this post


Link to post
Share on other sites
stibbar

ok it looks like I have it working...to a point. I can read a file and replace want I need. Now as I have hundreds of files to change I have tried to do a fileread which has not worked for me. Since I am getting sick of the NDA BS I will just pull out as much as I can with out getting in to much trouble.

The files I am going after are .OSD, which is used for streaming application. So the references are in the script here.

Local $OSD = "D:OSDAUTOIT.OSD.txt"
local $fArray = "streamingserver:554"
local $fArray = "streamingserver1:554"
local $fArray = "streamingserver2:554"
local $fArray = "streamingserver3:554"
local $fArray = "streamingserver.com:554"
Local $replace = "newstreamingserver:554"
local $line = FileReadLine($OSD)
;if $line = -1 then ;will be enabled once i can get it to read the line and use it corectly
;msgbox(0,"Done", "end of file")
MsgBox(0, "test", $line) ; to see the line it is reading
_ReplaceStringInFile($line, $fArray, $replace)
FileClose($OSD)

I am not even sure I can use a variablize fileread to do this. Hopefully someone has seen this or made something like this work. i am still trolling the forums to see what I can find

I should probably mention what the file i am trying to read contains, for testing purposes I am using my D;| drive, but will eventually use a file that actually contains UNC paths

D:APPV.OSD.testapplication_nameapplication_name.osd

D:APPV.OSD.testapplication_name1application_name1.osd

D:APPV.OSD.testapplication_name2application_name2.osd

D:APPV.OSD.testapplication_name3application_name3.osd

D:APPV.OSD.testapplication_name4application_name4.osd

D:APPV.OSD.testapplication_name5application_name5.osd

D:APPV.OSD.testapplication_name6application_name6.osd

Edited by stibbar

Share this post


Link to post
Share on other sites
kylomas

Stibb,

This should read your file with no problem (the code I posted in #11 is incomplete)

#Include <File.Au3>
$h_fl = FileOpen("D:testtextfile.txt")
$string = fileread($h_fl)
$TFind = StringRegExpReplace($string, "(http://)(.*)(:8080)","http://usethisurl:8080")
ConsoleWrite("Output:" & $TFind & @CRLF)

$string then contains the file as a string (complete with line deliniters).

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×