gazeranco Posted April 4, 2012 Share Posted April 4, 2012 Hey All, I am still stuck with a problem here, the code I am using successfully writes a "3" to the last line in a file. The bit that is not working is the readto array and write from array. It doesn't give me an error when I run it but when I open the file. The contents of the text.txt file has not been written to the .spf file. Can anyone see from the code why this isn't working?? Thanks, expandcollapse popup#include <File.au3> ; Open the text file and read it ready for writing. ; Prompt the user to run the script - use a Yes/No prompt (4 - see help file) Local $answer = MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") ; Check the user's answer to the prompt (see the help file for MsgBox return values) ; If "No" was clicked (7) then exit the script If $answer = 7 Then Exit MsgBox(0, "SPF-Editor", "Buh Bye!") EndIf MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $filer = FileOpen("text.txt", 0) ; Check if file opened for reading OK If $filer = -1 Then MsgBox(0, "Error", "Unable to open file!!") Exit EndIf ; Start finding files for writing. ; Shows the filenames of all files in the current directory Local $search = FileFindFirstFile("*.spf*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "Eeeeeroar!!") Exit EndIf Global $Text = "", $Array $line = FileRead($filer) ; reads the entire file at one time _FileReadToArray($filer, $Array) While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) _FileWriteFromArray($file, $Array) ; writes $$Array to $File WEnd MsgBox(0, "Done", "The script has stopped and your files are edited!") ; Close the search handle mofo FileClose($filer) FileClose($search) Link to comment Share on other sites More sharing options...
gazeranco Posted April 4, 2012 Author Share Posted April 4, 2012 (edited) Specifically, this is the area where I think I am missing something... Global $Text = "", $Array $line = FileRead($filer) ; reads the entire file at one time _FileReadToArray($filer, $Array) While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) _FileWriteFromArray($file, $Array) ; writes $$Array to $File WEnd Edited April 4, 2012 by gazeranco Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 4, 2012 Moderators Share Posted April 4, 2012 gazeranco,Your problem is here:$line = FileRead($filer) ; reads the entire file at one time _FileReadToArray($filer, $Array)Go and look at the parameters you nee to use for _FileReadToArray - you need the filename, not the file content. And here:_FileWriteToLine($file, ($CountLines), "3", 1) _FileWriteFromArray($file, $Array) ; writes $$Array to $FileYou are correctly changing the last tine with _FileWriteToLine, but then immediately overwriting it with the non-existent array. And you are yet another one who uses the cumbersome FileFindFirst/NextFile pair when it is so much easier to use _FileListToArray to do all the work for you. Try this script which works for me to convert all of the final lines of the *.spf* files to "3":expandcollapse popup#include <File.au3> ; Prompt the user to run the script - use a Yes/No prompt (4 - see help file) Local $answer = MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") ; Check the user's answer to the prompt (see the help file for MsgBox return values) ; If "No" was clicked (7) then exit the script If MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") = 7 Then MsgBox(0, "SPF-Editor", "Buh Bye!") Exit EndIf MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $filer = FileOpen("text.txt") ; Check if file opened for reading OK If $filer = -1 Then MsgBox(0, "Error", "Unable to open file!!") Exit EndIf ; Start finding files for writing. ; Shows the filenames of all files in the current directory Local $aList = _FileListToArray(@ScriptDir, "*.spf*", 1) ; Check if the search was successful If Not IsArray($aList) Then MsgBox(0, "Error", "Eeeeeroar!!") Exit EndIf ; Now loop through the files For $i = 1 To $aList[0] ; Get line count $iCount = _FileCountLines(@ScriptDir & "" & $aList[$i]) ; And change the final line _FileWriteToLine(@ScriptDir & "" & $aList[$i], $iCount, "3", 1) Next ; No idea why you opened it - but you need to close it sometime FileClose($filer)Please ask if you have any questions. 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 Link to comment Share on other sites More sharing options...
gazeranco Posted April 4, 2012 Author Share Posted April 4, 2012 (edited) Hey, That's a bit complex for me, I will try and understand it!! It does what I want, it replaces the last line with "3" now I just need it to read from text.txt and write from text.txt to the current file. I don't understand the _writefromarray command though, it's a little confusing for me at least... text.txt currently just contains... LINE1 LINE2 LINE3 LINE4 LINE5 -1 I will try and let you know how I get on. Thanks for the help!! Edited April 4, 2012 by gazeranco Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 4, 2012 Moderators Share Posted April 4, 2012 gazeranco, I just need it to read from text.txt and write from text.txt to the current fileIt is always better to explain exactly what you want in the beginning - saves a lot of time and wasted effort from those who try to help. Are you saying that you want to add the contents of the .txt file to the end of each *.spf* file? If so then you can use FileWrite directly. expandcollapse popup#include <File.au3> If MsgBox(4, "SPF-Editor", "Warning all SPF's in the .exe's directory will change, ok?") = 7 Then MsgBox(0, "SPF-Editor", "Buh Bye!") Exit EndIf MsgBox(0, "Running", "The program will prompt you when it has finished!") ; Read the text you want to add Local $sText = FileRead("text.txt") ; Check if file opened for reading OK If $sText = "" Then MsgBox(0, "Error", "Unable to open file!!") Exit EndIf ; Start finding files for writing. ; List the filenames of all *.spf* files in the current directory Local $aList = _FileListToArray(@ScriptDir, "*.spf*", 1) ; Check if the search was successful If Not IsArray($aList) Then MsgBox(0, "Error", "Eeeeeroar!!") Exit EndIf ; Now loop through the files For $i = 1 To $aList[0] ; Open the .spf file for appending data - parameter = 1 $hFile = FileOpen(@ScriptDir & "" & $aList[$i], 1) ; Add the data FileWrite($hFile, $sText) ; And close it FileClose($hFile) Next Again ask if you have any questions. 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 Link to comment Share on other sites More sharing options...
BrewManNH Posted April 4, 2012 Share Posted April 4, 2012 How is this thread any different from your You will need to include a copy of the .spf file so we can figure out EXACTLY what's in that file to replace. 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 GudeHow 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 Link to comment Share on other sites More sharing options...
gazeranco Posted April 4, 2012 Author Share Posted April 4, 2012 Brewman, different in that last time the loop confused me, then we digressed and you solved most of my problems. But this time specifically, I didn't understand what I was doing with the readarray/writearray and couldn't get it working, as the last title was loop in a loop i decided a new post was appropriate. Melba, Thats excellent, seems to work perfectly for my files. I will try and understand it all and possibly fire a few questions at you if I don't understand anything! Cheers, 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