gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 Wow thanks Xeno, that code blows my mind but it does work almost perfectly!! Only problem I have is the count keeps incrementing, I need it to reset to 1 after each file! Do you think you could make that happen as well? Thanks!! Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 2, 2012 Share Posted May 2, 2012 No, problem. You can also write 01, 02 and so on, if you want to.by using StringFormat. #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ Local $FileList = _FileListToArray(@ScriptDir, '*.spf') _ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', $i, 1) Next FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 Thanks, I will try this and then try string format and let you know how I get on! Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 Hey Xeno it works great!! Can you help me with 1 more thing?? I am trying to make it add some text to the end of that file now as well but nothing happens, I think it is because content is not a file. I am not sure how it is working. Is it going through and writing one file at a time? Heres the code now:- expandcollapse popup#region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $FileList = _FileListToArray(@ScriptDir, '*.spf') ;_ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', "("&$i&")", 1) Local $CountLines = _FileCountLines($content) _FileWriteToLine($content, ($CountLines), "3", 1) FileWriteLine($content, "16,1,1,250,66943,-14,1") FileWriteLine($content, "7.949,56.954,'"&$i-1&" WELD MAP NUMBERS',2.579,0.000") FileWriteLine($content, "-1") Next FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ;ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(0, "Done", "The script has stopped and your files are edited!") Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 If i use this code, it writes the text I want exactly how I want it but in the old file, doh! sorry for being a noob, how do I get it into the new file? #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ MsgBox(0, "Running", "The program will prompt you when it has finished!") Local $FileList = _FileListToArray(@ScriptDir, '*.spf') ;_ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', "("&$i&")", 1) Next FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Local $CountLines = _FileCountLines($content) _FileWriteToLine($FileList[$y], ($CountLines), "3", 1) FileWriteLine($FileList[$y], "16,1,1,250,66943,-14,1") FileWriteLine($FileList[$y], "7.949,56.954,'"&$i-1&" WELD MAP NUMBERS',2.579,0.000") FileWriteLine($FileList[$y], "-1") Next ;ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(0, "Done", "The script has stopped and your files are edited!") Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 2, 2012 Share Posted May 2, 2012 The text is added to every file (new!) #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ Local $FileList = _FileListToArray(@ScriptDir, '*.spf') _ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', '(' & StringFormat('%02i', $i) & ')', 1) ; StringFormat to write 01, 02, ... instead od 1, 2, ... Next $content &= @CRLF & _ 3 & @CRLF & _ "16,1,1,250,66943,-14,1" & @CRLF & _ "7.949,56.954,'" & $i - 1 & " WELD MAP NUMBERS',2.579,0.000" & @CRLF & _ "-1" FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ;~ ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(64, "Done", "The script has stopped and your files are edited!") Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 Hey Xeno, Thats superb buddy. The only issue I have now is that count line & _filewritetoline is the only way I have found of overwriting the last line of text and then append to the end of the text. Can I still use a line count in here? See the problem it poses is that the last line of text is "-1" I need to overwrite this last line, if you write to the end of the text file and there are LF's then it might not over write it. So you see here is the end of the text file. 7 15,3,1,1,8195,-14,1 'ARROW_STR',114.416,385.320,2.840,2.840,43.156 -1 3 16,1,1,250,66943,-14,1 7.949,56.954,'7 WELD MAP NUMBERS',2.579,0.000 -1 Everything after the first -1 is going to be ignored, also I cannot just say replace -1 with 3 as -1 will appear elsewhere in the file but its only where it appears on a line on its own that it signifies end of file to the program I am using. Does that make any sense? :s Link to comment Share on other sites More sharing options...
BrewManNH Posted May 2, 2012 Share Posted May 2, 2012 Didn't the other threads you posted on this topic help in any way to demonstrate how to replace that last line? 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 May 2, 2012 Author Share Posted May 2, 2012 Hey Brewman, yes. And I can replace the last line in a text file. However, what xeno is doing is different I think, as a file count and _filewriteline isn't going to work inside "$content =" i dont think?? Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 The only way I know how to attempt it is like this, but I can see it is wrong, I am just not sure how to fix it #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ Local $FileList = _FileListToArray(@ScriptDir, '*.spf') _ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', '(' & StringFormat('%02i', $i) & ')', 1) ; StringFormat to write 01, 02, ... instead od 1, 2, ... Next Global $CountLines = _FileCountLines($content) _FileWriteToLine($content, ($CountLines), "3", 1) FileWriteLine($content, "16,1,1,250,66943,-14,1") FileWriteLine($content, "7.949,56.954,'"&$i-1&" WELD MAP NUMBERS',2.579,0.000") FileWriteLine($content, "-1") FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ;~ ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(64, "Done", "The script has stopped and your files are edited!") Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 2, 2012 Share Posted May 2, 2012 So, you mean always replacing the last 4 lines with 3 16,1,1,250,66943,-14,1 7.949,56.954,'7 WELD MAP NUMBERS',2.579,0.000 -1 will solve the problem? What rule do you want to use? Replace last 4 lines? Search for a complete line containg just -1 and replace the 3 lines above? What is it? Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 (edited) Xeno! If you can make it do exactly what its doing and then make the last 4 lines be... 3 16,1,1,250,66943,-14,1 7.949,56.954,'7 WELD MAP NUMBERS',2.579,0.000 -1 Then yes that will be perfect! Only other thing is the number "7" in that text is obviously the variable for how many of (??) them we have replaced. But you already know that! Thanks for your help on this, I would say it would of took me ages to figure out but i'd be lying, I probably would of never figured this out!! Edited May 2, 2012 by gazeranco Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 I think on this bit... $content &= 3 & @CRLF & _ "16,1,1,250,66943,-14,1" & @CRLF & _ "7.949,56.954,'" & $i - 1 & " WELD MAP NUMBERS',2.579,0.000" & @CRLF & _ "-1" I need to say [backspace][backspace][backspace] maybe to erase the -1. Is that posisble? Link to comment Share on other sites More sharing options...
gazeranco Posted May 2, 2012 Author Share Posted May 2, 2012 No I don't think this is possible is it, as were not working with a file. We are working with the contents of a file in memory? So I could do it to the files after they have been created but then I wouldnt have access to the count we do during execution for the amount of find/replaces? Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 2, 2012 Share Posted May 2, 2012 (edited) #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ Local $FileList = _FileListToArray(@ScriptDir, '*.spf') ;~ _ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', '(' & StringFormat('%02i', $i) & ')', 1) ; StringFormat to write 01, 02, ... instead od 1, 2, ... Next For $z = 1 To 4 ; 4 means 4 Lines are deleted (every loop the last line) $content = StringRegExpReplace($content, 'rn.*$', '') Next $content &= @LF & _ 3 & @CRLF & _ "16,1,1,250,66943,-14,1" & @CRLF & _ "7.949,56.954,'" & $i - 1 & " WELD MAP NUMBERS',2.579,0.000" & @CRLF & _ "-1" FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ;~ ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(64, "Done", "The script has stopped and your files are edited!") What do you mean by the 7 and the replacement? Could change the second loop to ;~ For $z = 1 To 4 ; 4 means 4 Lines are deleted (every loop the last line) $content = StringRegExpReplace($content, 'rn.*rn.*rn.*rn.*$', '') ;~ Next Edited May 2, 2012 by Xenobiologist Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
gazeranco Posted May 3, 2012 Author Share Posted May 3, 2012 Hey Xeno! Don't worry about the 7, you already did it! That works fantastic, only problem I have now is a blank line in the file when we write our new data, I have tried tweaking the code but I seem to always end up with the blank line (which halts the application using this file). Any ideas for that bit?? End of a file looks like this... -1 replaced with a blank line then our text is added on the end? 15,3,1,1,8195,-14,1 182.867,449.500,114.416,385.320 7 15,3,1,1,8195,-14,1 3 16,1,1,250,66943,-14,1 7.949,56.954,'7 WELD MAP NUMBERS',2.579,0.000 -1 Also, I would love it if you could explain how this works as I have no idea what's going on! ;~ For $z = 1 To 4 ; 4 means 4 Lines are deleted (every loop the last line) $content = StringRegExpReplace($content, 'rn.*rn.*rn.*rn.*$', '') ;~ Next Link to comment Share on other sites More sharing options...
gazeranco Posted May 3, 2012 Author Share Posted May 3, 2012 Xeno!!!! It works! I had to play around with carriage returns and Line Feeds! #region ;************ Includes ************ #include <File.au3> #include <Array.au3> #endregion ;************ Includes ************ MsgBox(0, "Starting", "The script will prompt yoy when it's finished!") Local $FileList = _FileListToArray(@ScriptDir, '*.spf') ;~ _ArrayDisplay($FileList) ; show all files in that directory with filter *spf For $y = 1 To UBound($FileList) - 1 $content = FileRead(@ScriptDir & '' & $FileList[$y]) $found_A = StringRegExp($content, '(??)', 3) If @error Then ConsoleWrite(@ScriptDir & '' & $FileList[$y] & ' has no ??!' & @LF) For $i = 1 To UBound($found_A) ; it now starts with 1 for each file $content = StringReplace($content, '(??)', '(' & StringFormat('%02i', $i) & ')', 1) ; StringFormat to write 01, 02, ... instead od 1, 2, ... Next For $z = 1 To 1 ; 4 means 4 Lines are deleted (every loop the last line) $content = StringRegExpReplace($content, 'rn.*$', @CRLF&'3'&@CR) Next $content &= _ "16,1,1,250,66943,-14,1" & @CRLF & _ "7.949,56.954,'" & $i - 1 & " WELD MAP NUMBERS',2.579,0.000" & @CRLF & _ "-1" FileWrite(FileOpen(@ScriptDir & 'NEW' & $FileList[$y], 10), $content) ; ScripDir/NEW/ for all the changed files Next ;~ ShellExecute(@SystemDir & 'notepad.exe', @ScriptDir & 'NEW' & $FileList[1]) ; See first file as example MsgBox(0, "Done", "The script has stopped and your files are edited!") Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 3, 2012 Share Posted May 3, 2012 (edited) Nice. Why did you do this? $content = StringRegExpReplace($content, 'rn.*$', @CRLF&'3'&@CR)Now you are not deleting the last lines. Anyhow, if you are lucky then it is fine. :-) Edited May 3, 2012 by Xenobiologist Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
gazeranco Posted May 3, 2012 Author Share Posted May 3, 2012 (edited) Hey Xeno, I was just using trial and error and once it worked I stopped playing! It kept putting an extra line in, or adding 3 to the end of an existing line. Unfortunately the application I am using only accepts the document when formatted exactly how it expects to read it. Now it erases just the last line (as that's actually what I need, I was confused and told you 4 lines!) And everything carries on as normal, no empty lines! Thanks for your help, your obviously a coding expert!! Edited May 3, 2012 by gazeranco 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