dobbelina Posted May 12, 2010 Posted May 12, 2010 Hi !Example to illustrate the problem:The script is searching for the word "dog".If "dog" is not found it will look for the word "cat" andadd "dog" beneath it. If "dog" is found the script wont execute the function,so to safeguard against multiple writes.The script does everything perfectly except for one thing,It deletes the word underneath where "dog" is inserted.Therefore i need a linefeed or something, so i can keepthe "fish" (See example)#Include <File.au3> Global $success = False $myfile = "C:\Test.txt" $file = FileRead($myfile, FileGetSize($myfile)) $file = StringSplit($file, @CRLF, 1) for $i = 0 to $file[0] Step 1 If StringInStr($file[$i], "dog") Then $success = True ExitLoop EndIf Next if $success = Not True Then MsgBox(0, "Not fund", "Line not found, so lets write") _test() EndIf Func _test() for $i = 0 to $file[0] Step 1 If StringInStr($file[$i], "cat") Then _FileWriteToLine($myfile, $i+1,"dog", 1) EndIf Next EndFuncOriginal Test.txt:monkeybirdcatfishcrocodileBecomes: (fish is gone)monkeybirdcatdogcrocodileBut i want:monkeybirdcatdogfishcrocodileIt's cracking me up this problem, any help is much appreciated !
jchd Posted May 12, 2010 Posted May 12, 2010 Like this: #Include <Array.au3> Local Const $myfile = "Test.txt" ;; I had to change that Local Const $cat = "cat" Local Const $dog = "dog" Local $lines _FileReadToArray($myfile, $lines) Local $index = _ArraySearch($lines, $cat, 1) If $index = -1 Then $index = UBound($lines) - 1 _ArrayInsert($lines, $index + 1, $dog) _FileWriteFromArray($myfile, $lines, 1) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
hawky358 Posted May 12, 2010 Posted May 12, 2010 (edited) The _Filewritetoline() function already has this built in Here's the syntax:_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite = 0])change _FileWriteToLine($myfile, $i+1,"dog", 1)to _FileWriteToLine($myfile, $i+1,"dog", 0)or _FileWriteToLine($myfile, $i+1,"dog") Edited May 12, 2010 by hawky358
dobbelina Posted May 12, 2010 Author Posted May 12, 2010 Ahh, thank you very much! Now it works as i want.
Sritra Posted March 2, 2013 Posted March 2, 2013 Ok, what am i doing wrong? I just wanted to check if my _filewritetoline works, but it doesn't seem so #Include Global $success = False $myfile = "C:\Users\Fabi\Desktop\elements\test.txt" $file = FileRead($myfile, FileGetSize($myfile)) $file = StringSplit($file, @CRLF, 1) for $i = 0 to $file[0] Step 1 If StringInStr($file[$i], "dog") Then $success = True ExitLoop EndIf Next If $success = Not True Then MsgBox(0, "Not fund", "Line not found, so lets write") _test() EndIf Func _test() for $i = 0 to $file[0] Step 1 If StringInStr($file[$i], "cat") Then _FileWriteToLine($myfile, $i+1,"dog",0) EndIf Next EndFunc It shouts this Error: >"C:Program Files (x86)AutoIt3SciTE..autoit3.exe" /ErrorStdOut "C:UsersFabiDesktopelementstest.au3" >Exit code: 0 Time: 0.785 And the test.txt hasn't change
Moderators Melba23 Posted March 2, 2013 Moderators Posted March 2, 2013 Sritra,Your code works fine for me, but you have a major logical flaw within it. You are adding a line to the file when you write to it - that means that your line count for any subsequent _FileWriteToLine will be incorrect. If we start with this file:cow pig cat rat flywe get this aftet one replacement:cow pig cat dog rat flySo you can see that all the subsequent lines are now pushed down one and so you will not write the next "dog" in the correct place. You get over this by working up from the bottom of the list so that all the changes are below the current position in the file. I have rewritten the script to show this working:#include <File.au3> Global $fSuccess = False, $aLines ; This gets the file directly into an array - much faster than what you had before $sMyFile = "test.txt" _FileReadToArray($sMyFile, $aLines) For $i = 0 To $aLines[0] If StringInStr($aLines[$i], "dog") Then $fSuccess = True ExitLoop EndIf Next If Not $fSuccess Then MsgBox(0, "Not found", "Line not found, so lets write") _test() Else MsgBox(0, "Found", "OK") EndIf Func _test() For $i = $aLines[0] To 1 Step -1 If StringInStr($aLines[$i], "cat") Then _FileWriteToLine($sMyFile, $i + 1, "dog", 0) ; This is just to show you what is happening as we move up the array $sFile = FileRead($sMyFile) MsgBox(0, "Current file content", $sFile) EndIf Next EndFunc ;==>_testAll clear? Please ask if not. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Moderators Melba23 Posted March 2, 2013 Moderators Posted March 2, 2013 Sritra, And what about the logic problem I highlighted? Will it be worth our responding to you in the future or will any help offered simply be ignored? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Sritra Posted March 2, 2013 Posted March 2, 2013 I thank You for that, but i just wanted to see, if my _FileWriteToLine() is working, the result was uneccesary, but thanks for trying to help me
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