gazeranco Posted March 29, 2012 Posted March 29, 2012 Hey All, I have a loop within a loop, I can kind of see why it doesn't work becuase I want it to write all lines but it does one then moves onto the next file. Can someone help me figure this out? Thanks!! While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) While 1 Local $line = FileReadLine($filer) If @error = -1 Then ExitLoop FileWriteLine($file, $line) WEnd WEnd
water Posted March 29, 2012 Posted March 29, 2012 Trying to understand what you want to achieve: For all files you can find you want to write the number of records into the first record of the file. Right? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
BrewManNH Posted March 29, 2012 Posted March 29, 2012 _FileCountLines doesn't take a file handle, it needs a filename and path._FileWriteToLine doesn't take a file handle either, it needs a filename and pathIf you use _FileWriteToLine it will either overwrite the previous line, or it will insert it, the way you have it written, it will replace the last line in the file.You haven't declared a variable called $filer, yet you're trying to write text to a file with no information.Please explain what you're trying to do with the above code and I will see if I can fix it for you. 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
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 (edited) Ok, So what I am doing is… Loop For this file Loop Read/write Read/write Read/write End of file reached, exit loop. Back to parent loop For next file Read/write Read/write Read/write End of file reached, exit loop. End of files Exit loop 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 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", 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 While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) While 1 Local $line = FileReadLine($filer) If @error = -1 Then ExitLoop FileWriteLine($file, $line) WEnd WEnd MsgBox(0, "Done", "The script has stopped and your files are edited!") ; Close the search handle mofo FileClose($filer) FileClose($search) It worked for one file, but now ive tried to make it loop through all files it only writes the first line of text to each file. theres about 10 lines of text in text.txt. Edited March 29, 2012 by gazeranco
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 Hey guys, Heres all my files guys. Might make you understand what I am trying to acheive. Thanks Deploy.zip
kylomas Posted March 29, 2012 Posted March 29, 2012 gazeranco, Answer water's question and you will get some help. Your psuedo-logic resembles spahgetti, maybe you could just describe your expected outcome. 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
BrewManNH Posted March 29, 2012 Posted March 29, 2012 Are you trying to overwrite the last line in each spf file, or are you wanting to add the lines to the end of it? 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
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 1# _FileCountLines doesn't take a file handle, it needs a filename and path. I am not sure what you mean by this, but I get the desire effect. The last line in the file is overwritten with the number 3. I need to overwrite the last line as its like a #EOF type statement so I need that going back on the end after I write the new text. 2# _FileWriteToLine doesn't take a file handle either, it needs a filename and path Again, not sure what a handle is but I think I have a variable with a file path, and again I have no idea what I am doing but it seems to work with that. 3# If you use _FileWriteToLine it will either overwrite the previous line, or it will insert it, the way you have it written, it will replace the last line in the file. Yup, thats what I want with that bit. 4# You haven't declared a variable called $filer, yet you're trying to write text to a file with no information. I have provided the rest of the code, I just gave you the bit that wa sbroken I appreciate how seeing it all probably makes it easier to help me In the files provided there is a readme to the user, this might help. Background:- I produce drawings, the program that makes them just writes a text file with coordinates and text. I needed to add text to over 500 drawings. I wanted a program to add this text for me. That worked fine but the text was inside the code. I want other people in my office to use this exe without editing code, so i tried to make it read what I want writing from a text file. This way the users cant mess my code up but they just edit text.txt and run the exe. All the drawings (.spf) in the folder get the desired text added. Ahhh... I should of written all that to start off with!
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 Are you trying to overwrite the last line in each spf file, or are you wanting to add the lines to the end of it?So overwrite last line, then read/add the text from text.txt to afile.spf
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 It used to look like this... which works perfect. But not user friendly! expandcollapse popup#include <File.au3> ; 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 While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) FileWriteLine($file, "63,3,1,300,0,0,0") FileWriteLine($file, "5.244,15.962,'A',3.000,0.000") FileWriteLine($file, "3") FileWriteLine($file, "63,3,1,300,0,0,0") FileWriteLine($file, "20.244,15.962,'RL',3.000,0.000") FileWriteLine($file, "3") FileWriteLine($file, "63,3,1,300,0,0,0") FileWriteLine($file, "31.763,15.547,'10/11/10',3.000,0.000") FileWriteLine($file, "3") FileWriteLine($file, "63,3,1,300,0,0,0") FileWriteLine($file, "56.763,15.547,'ISSUED FOR FABRICATION',3.000,0.000") FileWriteLine($file, "3") FileWriteLine($file, "63,3,1,300,0,0,0") FileWriteLine($file, "831.440,4.802,'A',3.000,0.000") FileWriteLine($file, "-1") ;display a message showing me the file name during debugging. ;MsgBox(4096, "File:", $file) WEnd ; Close the search handle mofo FileClose($search)
kylomas Posted March 29, 2012 Posted March 29, 2012 gazeranco, And where does linecount come in?? 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
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 Local $CountLines = _FileCountLines($file) _FileWriteToLine($file, ($CountLines), "3", 1) Kylomas:- I am using that to overwrite the last line of text... so count all the lines, (example) there's 100 lines so $CountLines = 100. So I write number "3" over the last line.
BrewManNH Posted March 29, 2012 Posted March 29, 2012 Here's how I'd do it without using anything other than file reads and writes, don't use _FileCountLines on large files because it will take you forever. As long as the files to be read can fit in memory, this should work for you. expandcollapse popup51 #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 = "3" & @CRLF, $Array Local $line = FileRead($filer) ; reads the entire file at one time $Text &= $line ; adds the file to the $Text variable which contains the number 3 and a line feed While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop _FileReadToArray($file, $Array) $Array[$Array[0]] = $Text _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) 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
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 Hey BrewManNH! That works great! Only thing it doesn't do is overwrite the last line of txt, the last line in a .spf is "-1" this represents the end of file, i need to overwrite it. Can I do this with out counting up to the last line, these text files can contain between 5,000 and 25,000 lines on average so it would be good to not count them.
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 let me see if I understand all this code this is way above me! please let me know if my understanding is correct? Global $Text = "3" & @CRLF, $Array //This is declaring 2 global variables one called text and one calles array? Local $file = FileFindNextFile($search) //No idea how this works... If @error Then ExitLoop //Or this... _FileReadToArray($file, $Array) //The file to read & the Array to put the info in. $Array[$Array[0]] = $Text //hmmmm?? The first entry in the array = $text and i guess this pushes everything else down doesn't erase anything from the array??
BrewManNH Posted March 29, 2012 Posted March 29, 2012 (edited) Change this line: $Array[$Array[0]] = $Text ; to this $Array[$Array[0] - 1] = $Text Edited March 29, 2012 by BrewManNH 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
kylomas Posted March 29, 2012 Posted March 29, 2012 gazeranco, Try this (similar to BrewmanNH's) expandcollapse popup#include <File.au3> #include <array.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 MsgBox(0, "SPF-Editor", "Buh Bye!") Exit EndIf MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $filer = FileOpen(@ScriptDir & "text.txt", 0) ; Check if file opened for reading OK If $filer = -1 Then MsgBox(0, "Error", "Unable to open file!!") Exit EndIf Local $filer_str = FileRead($filer) ; 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 While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $file_str = FileRead($file) Local $cnt = StringSplit($file_str,@crlf,3) $cnt[UBound($cnt)-1] = UBound($cnt) FileWrite($file & '.new',_arraytostring($cnt,@crlf) & @CRLF & $filer_str) WEnd MsgBox(0, "Done", "The script has stopped and your files are edited!") ; Close the search handle mofo ;FileClose($filer) FileClose($search) 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
BrewManNH Posted March 29, 2012 Posted March 29, 2012 let me see if I understand all this code this is way above me! please let me know if my understanding is correct? Global $Text = "3" & @CRLF, $Array //This is declaring 2 global variables one called text and one calles array? Correct, I used one line to simplify it. Local $file = FileFindNextFile($search) //No idea how this works... If @error Then ExitLoop //Or this... The @error will be set when there are no more files found that match the search string. So the exitloop is to jump out of the While loop when that occurs, otherwise you'd keep looping even though all files have been processed. _FileReadToArray($file, $Array) //The file to read & the Array to put the info in. $Array[$Array[0]] = $Text //hmmmm?? The first entry in the array = $text and i guess this pushes everything else down doesn't erase anything from the array?? $Array[0] will contain the number of lines that FileReadToArray has read from the file, I'm using this to overwrite the entry in the last spot in the array with the new text. Although if you try it with the above change, it should overwrite the line before the last line, which might be blank. 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
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 Change this line: $Array[$Array[0]] = $Text ; to this $Array[$Array[0] - 1] = $Text Hey Brewman, no same results mate. Before:- 10 9 7 8 999 7.8 '18 503X171X45CB 7664 1a1 201011101530 FB 8 3 889 BEAM 2 bridge PB3 18 2D' 'null' -1 After:- 10 9 7 8 999 7.8 '18 503X171X45CB 7664 1a1 201011101530 FB 8 3 889 BEAM 2 bridge PB3 18 2D' 'null' -1 3 63,3,1,300,0,0,0 5.244,15.962,'A',3.000,0.000 3 63,3,1,300,0,0,0 20.244,15.962,'RL',3.000,0.000 3 63,3,1,300,0,0,0 31.763,15.547,'10/11/10',3.000,0.000 3 63,3,1,300,0,0,0 56.763,15.547,'ISSUED FOR FABRICATION',3.000,0.000 3 63,3,1,300,0,0,0 831.440,4.802,'A',3.000,0.000 -1
gazeranco Posted March 29, 2012 Author Posted March 29, 2012 gazeranco, Try this (similar to BrewmanNH's) expandcollapse popup#include <File.au3> #include <array.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 MsgBox(0, "SPF-Editor", "Buh Bye!") Exit EndIf MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $filer = FileOpen(@ScriptDir & "text.txt", 0) ; Check if file opened for reading OK If $filer = -1 Then MsgBox(0, "Error", "Unable to open file!!") Exit EndIf Local $filer_str = FileRead($filer) ; 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 While 1 Local $file = FileFindNextFile($search) If @error Then ExitLoop Local $file_str = FileRead($file) Local $cnt = StringSplit($file_str,@crlf,3) $cnt[UBound($cnt)-1] = UBound($cnt) FileWrite($file & '.new',_arraytostring($cnt,@crlf) & @CRLF & $filer_str) WEnd MsgBox(0, "Done", "The script has stopped and your files are edited!") ; Close the search handle mofo ;FileClose($filer) FileClose($search) kylomas Nice try but no RESULT:- 10 9 7 8 999 7.8 '18 503X171X45CB 7664 1a1 201011101530 FB 8 3 889 BEAM 2 bridge PB3 18 2D' 'null' -1 13 63,3,1,300,0,0,0 5.244,15.962,'A',3.000,0.000 3 63,3,1,300,0,0,0 20.244,15.962,'RL',3.000,0.000 3 63,3,1,300,0,0,0 31.763,15.547,'10/11/10',3.000,0.000 3 63,3,1,300,0,0,0 56.763,15.547,'ISSUED FOR FABRICATION',3.000,0.000 3 63,3,1,300,0,0,0 831.440,4.802,'A',3.000,0.000 -1
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