williamk Posted March 6, 2007 Share Posted March 6, 2007 Hello, Is there a way to search for and replace a specific string of text in a text file? I have a file named SQL_1 that in the midst of other text contains the text '1/1/07' and '1/31/07'. I want to replace that text with the input variables $BeginDate and $EndDate as shown in the below code. Is there an easier way of doing this other than opening the file in word pad and doing a find and replace? #include <GUIConstants.au3> Main(); starts GUI part Func Main() GUICreate("New DT Mort and Other Instruments DB Prep", 400, 140) $bdate = GUICtrlCreateInput("", 205, 5, 80, 20) $edate = GUICtrlCreateInput("", 205, 30, 80, 20) GUICtrlSetState(-1, $GUI_NODROPACCEPTED) GUICtrlSetLimit(-1, 10) GUICtrlCreateLabel ( "Enter the beginning date for the report", 10, 5) GUICtrlCreateLabel ( "Enter the ending date for the report", 10, 30) $btn = GUICtrlCreateButton("Continue", 200, 100, 60, 20) $btn2 = GUICtrlCreateButton("Exit", 130, 100, 60, 20) GUISetState() $msg = 0 While $msg <> $GUI_EVENT_CLOSE $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $msg = $btn2 Exit Case $msg = $btn $BeginDate = GUICtrlRead($bdate) $EndDate = GUICtrlRead($edate) ExitLoop EndSelect WEnd EndFunc Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 7, 2007 Moderators Share Posted March 7, 2007 _ReplaceStringInFile()? Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
williamk Posted March 7, 2007 Author Share Posted March 7, 2007 _ReplaceStringInFile()?I don't see that command in the autoit manual. All I see is a "StringReplace" command, and I don't see any options there for plugging in a file name. Can you give me a reference for the _ReplaceStingInFile() command. Yes, I am a newbie Link to comment Share on other sites More sharing options...
PaulIA Posted March 7, 2007 Share Posted March 7, 2007 I don't see that command in the autoit manual. All I see is a "StringReplace" command, and I don't see any options there for plugging in a file name. Can you give me a reference for the _ReplaceStingInFile() command. Yes, I am a newbie Use the help file index to search for functions. This one's in File.au3. Auto3Lib: A library of over 1200 functions for AutoIt Link to comment Share on other sites More sharing options...
williamk Posted March 7, 2007 Author Share Posted March 7, 2007 Use the help file index to search for functions. This one's in File.au3.Arg, ok found it. The lines I needed to add to the script were:_ReplaceStringInFile ("C:\sqlstuff\stats1.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats1.sql", '1/31/2007', ($EndDate))_ReplaceStringInFile ("C:\sqlstuff\stats2.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats2.sql", '1/31/2007', ($EndDate))_ReplaceStringInFile ("C:\sqlstuff\stats3.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats3.sql", '1/31/2007', ($EndDate))Thanks. Link to comment Share on other sites More sharing options...
williamk Posted March 7, 2007 Author Share Posted March 7, 2007 Arg, ok found it. The lines I needed to add to the script were:_ReplaceStringInFile ("C:\sqlstuff\stats1.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats1.sql", '1/31/2007', ($EndDate))_ReplaceStringInFile ("C:\sqlstuff\stats2.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats2.sql", '1/31/2007', ($EndDate))_ReplaceStringInFile ("C:\sqlstuff\stats3.sql", '1/1/2007', ($BeginDate))_ReplaceStringInFile ("C:\sqlstuff\stats3.sql", '1/31/2007', ($EndDate))Thanks.OK now I need a function that will just find a specific string in the above named text files and report the number of occurances. Looked in help file and don't see it. Is there a function for that? Link to comment Share on other sites More sharing options...
Valuater Posted March 7, 2007 Share Posted March 7, 2007 ; read the file = $File_read $File_read = "This is a true story about is and is-not in an abis...lol" $count = StringReplace($File_read, "is", "nada") MsgBox(0,0, @extended) 8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 7, 2007 Moderators Share Posted March 7, 2007 Func _StringReturnOccurence($sString, $sFind) StringReplace($sString, $sFind, '') Return @extended EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
williamk Posted March 8, 2007 Author Share Posted March 8, 2007 ; read the file = $File_read $File_read = "This is a true story about is and is-not in an abis...lol" $count = StringReplace($File_read, "is", "nada") MsgBox(0,0, @extended) 8) Cool beans, this works. THANKS!!! The function one looks good too, just have to figure out how I would use it. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 8, 2007 Moderators Share Posted March 8, 2007 $File_read = "This is a true story about is and is-not in an abis...lol" $nNumberFound = _StringReturnOccurence($File_read, 'is') MsgBox(64, 'Info', $nNumberFound) Func _StringReturnOccurence($sString, $sFind) StringReplace($sString, $sFind, '') Return @extended EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now