Sorian Posted June 8, 2011 Author Share Posted June 8, 2011 Awesome At least the syntax errors tell you what to fix. This time I did run Au3Check:Global $gs_fileToOpen = "SomeFile.txt" Global $gs_fileToWrite = "SomeOutFile.txt" Global $gh_fileOpen = FileOpen($gs_fileToOpen, 0) Global $gh_fileWriteTo = FileOpen($gs_fileToWrite, 2) Global $gs_line, $gs_convert While 1 $gs_line = FileReadLine($gh_fileOpen) If @error Then Exitloop $gs_convert = StringRegExpReplace($gs_line, "(\d+),(\d+),(\d+)", "X$1 Y$3 Z$5") FileWrite($gh_fileWriteTo, $gs_convert & @CRLF) WEnd FileClose($gh_fileOpen) FileClose($gh_fileWriteTo)However I did not run it. Edit: BTW, I'm running the same version of SciTe; however I am using the 3.3.6.1 release version of AutoIt Ok, last time I will mention a problem on yours. It runs now, outputs file, losses data: Original data: 1,3,5 2,4,6 3,6,9 4,8,12 5,10,15.221 Converted data: X1 Y5 Z X2 Y6 Z X3 Y9 Z X4 Y12 Z X5 Y15 Z.221 It took of actual Y number and replaced with Z, except last number left everything after the point. Link to comment Share on other sites More sharing options...
pieeater Posted June 8, 2011 Share Posted June 8, 2011 (edited) i updated my code from earlyer, and im pretty sure the reason is the stringregexpreplace. heres my updated code anyway: $file = FileOpenDialog("txt file", @DesktopDir, "Text (*.txt)", 1) If @error Then Exit $savelocation = FileSaveDialog("Save location and file", @DesktopDir, "(*.txt)", 16) If Not StringRight($savelocation, 4) = ".txt" Then $savelocation = $savelocation & ".txt" ;put this here to fix the .txt problem If @error Then Exit Do $lines = InputBox("how many lines in the txt file?", "line numbers here:") If @error = 1 Then Exit Until $lines >= 10 And $lines <= 10000 For $i = 1 To $lines $fileRead = FileReadLine($file, $i) $string = StringSplit($fileRead, ",") $newValue = "X" & $string[1] & "," & "Y" & $string[2] & "," & "Z" & $string[3] FileWriteLine($savelocation, $newValue) Next FileClose($file) Exit Edited June 8, 2011 by pieeater [spoiler]My UDFs: Login UDF[/spoiler] Link to comment Share on other sites More sharing options...
Sorian Posted June 8, 2011 Author Share Posted June 8, 2011 i updated my code from earlyer, and im pretty sure the reason is the stringregexpreplace. heres my updated code anyway: $file = FileOpenDialog("txt file", @DesktopDir, "Text (*.txt)", 1) If @error Then Exit $savelocation = FileSaveDialog("Save location and file", @DesktopDir, "(*.txt)", 16) If Not StringRight($savelocation, 4) = ".txt" Then $savelocation = $savelocation & ".txt" ;put this here to fix the .txt problem If @error Then Exit Do $lines = InputBox("how many lines in the txt file?", "line numbers here:") If @error = 1 Then Exit Until $lines >= 10 And $lines <= 10000 For $i = 1 To $lines $fileRead = FileReadLine($file, $i) $string = StringSplit($fileRead, ",") $newValue = "X" & $string[1] & "," & "Y" & $string[2] & "," & "Z" & $string[3] FileWriteLine($savelocation, $newValue) Next FileClose($file) Exit New problems on your new script. Missing .txt property on first run. Second run doesn't rewrite, but adds onto existing file. Link to comment Share on other sites More sharing options...
Sorian Posted June 8, 2011 Author Share Posted June 8, 2011 (edited) First off, I would like to thank everyone who took time to help me with this problem and submitting their scripts for me to try and use. I have a new question on this original post: I have been fighting with the scripts from UEZ's: $sHugefile = FileRead("huge.txt") $sNew = StringRegExpReplace($sHugefile, "(\d+),(\d+),(\d+)", "X$1 Y$2 Z$3") $hFile = FileOpen("new.txt", 2) FileWrite($hFile, $sNew) FileClose($hFile And the beginning part of pieeater's: $file = FileOpenDialog("txt file", @DesktopDir, "Text (*.txt)", 1) If @error Then Exit $savelocation = FileSaveDialog("Save location and file", @DesktopDir, "(*.txt)", 16) If Not StringRight($savelocation, 4) = ".txt" Then $savelocation = $savelocation & ".txt" ;put this here to fix the .txt problem If @error Then Exit To see if there is a way to marry the two together. I love the ability to choose what file to read and save to with the simplicity that made UEZ run first time (as long as the of the read file is the same). Something like this: $file = FileOpenDialog("Open Scan File", @DesktopDir, "Text (*.txt)", 1) If @error Then Exit $savelocation = FileSaveDialog("Save Conversion As", @DesktopDir, "Text (*.txt)", 16) If Not stringright($savelocation, 4) = ".txt" Then $savelocation = $savelocation & ".txt" If @error Then Exit $sHugefile = FileRead($file) $sNew = StringRegExpReplace($sHugefile, "(\d+),(\d+),(\d+)", "X$1 Y$2 Z$3") $hFile = FileOpen($savelocation, 2) FileWrite($hFile & ".txt" , $sNew) FileClose($hFile) But it runs into the same error as pieeater's of not making the first run with the right properties. Changing line 8 to $hFile = FileOpen($savelocation & ".txt", 2) fixes the first run, but then runs into the adding of another .txt to all additional runs using the same name then rewriting over the file. Edited June 8, 2011 by Sorian Link to comment Share on other sites More sharing options...
pieeater Posted June 8, 2011 Share Posted June 8, 2011 sorry for the error i didnt test it. i got this to work though: ;~ $file = FileOpenDialog("txt file", @DesktopDir, "Text (*.txt)", 1) ;~ If @error Then Exit ;~ $savelocation = FileSaveDialog("Save location and file", @DesktopDir, "(*.txt)", 16) ;~ If @error Then Exit $save = StringRight($savelocation, 4) If $save = ".txt" Then $savelocation = $savelocation Else $savelocation = $savelocation & ".txt" EndIf ;~ Do ;~ $lines = InputBox("how many lines in the txt file?", "line numbers here:") ;~ If @error = 1 Then Exit ;~ Until $lines >= 10 And $lines <= 10000 ;~ For $i = 1 To $lines ;~ $fileRead = FileReadLine($file, $i) ;~ $string = StringSplit($fileRead, ",") ;~ $newValue = "X" & $string[1] & "," & "Y" & $string[2] & "," & "Z" & $string[3] ;~ FileWriteLine($savelocation, $newValue) ;~ Next ;~ FileClose($file) ;~ Exit [spoiler]My UDFs: Login UDF[/spoiler] Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 8, 2011 Moderators Share Posted June 8, 2011 (edited) Hmm, your input data went from being:Integer + comma + Integer + comma + IntegerTo:Integer + comma + Integer + comma + FloatThe empty data on mine, means that there was a non-digit after the comma ( maybe a space. ).When you're messing around with things like this, you need to be very specific in your needs/wants and set specific rules.This should give you what you want.expandcollapse popupGlobal $gs_fileToRead = "FileToRead" Global $gs_fileToWrite = "OutputFile" ; Read file into lines Global $gs_fRead = FileRead($gs_fileToRead) Global $ga_Lines = StringSplit(StringStripCR($gs_fRead), @LF) Global $gh_FileWrite = FileOpen($gs_fileToWrite, 2) ; Mark what the file delimiter will be Global $gs_Delim = ",", $gs_OutDelim = " " Global $gi_OutDelimLen = StringLen($gs_OutDelim) ; set the max number of columns you're dealing with ; this is set because it's assumed because you're only using xyz that there's no more than 3 ever Global $gi_MaxColumns = 3 Global $ga_OutPrePend[$gi_MaxColumns + 1] = [$gi_MaxColumns, "X", "Y", "Z"] ; vars that will be used in loop Global $ga_Columns, $gs_Output For $iline = 1 To $ga_Lines[0] ; only get lines with actual data If Not $ga_Lines[$iline] Then ContinueLoop ; clear var for next loop $gs_Output = "" $ga_Columns = StringSplit($ga_Lines[$iline], $gs_Delim, 1) For $icol = 1 To $ga_Columns[0] ; if we have more columns than we do fill chars ( eg. XYZ ), exit this loop If $icol > $gi_MaxColumns Then ExitLoop $gs_Output &= $ga_OutPrePend[$icol] & StringStripWS($ga_Columns[$icol], 8) & $gs_OutDelim Next ; trim extra space from end $gs_Output = StringTrimRight($gs_Output, $gi_OutDelimLen) & @CRLF FileWrite($gh_FileWrite, $gs_Output) Next FileClose($gh_FileWrite)Tested it on 100k line, 3 column, mixed Integers and Numbers. Edited June 8, 2011 by SmOke_N 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...
Sorian Posted June 9, 2011 Author Share Posted June 9, 2011 Ok ok, Finally got a scan file with exact data for you guys to use to test scripts with. This roadrunner.txt is 4,465 lines and is just an example, as stated the amount of lines is not going to be the same amount from one run to another it can change. There will always be something in X Y Z and the number will have a point in it (My first example on original post was based on what I was told at the time and when I added the 15.221, that was me seeing if that would work, my bad).roadrunner.txt Link to comment Share on other sites More sharing options...
UEZ Posted June 9, 2011 Share Posted June 9, 2011 (edited) Try this then: expandcollapse popup$filename = FileOpenDialog("Select a file", "", "Text (*.txt)") If @error Then Exit ;method 1: no memory issue $sFile = FileRead($filename) $sNew = StringRegExpReplace($sFile, "(.*),(.*),(.*)", "X$1 Y$2 Z$3") $newfile = FileSaveDialog("Save the result", @ScriptDir, "Text (*.txt)", 2) If @error Then Exit $hFile = FileOpen($newfile, 2) FileWrite($hFile, $sNew) FileClose($hFile) ;method 2: if your file is really huge (> 100MB I would say) but $sNew still in memory! $hFile = FileOpen($filename) $sNew = "" Do $line = FileReadLine($hFile) If @error = - 1 Then ExitLoop $sNew &= StringRegExpReplace($line, "(.*),(.*),(.*)", "X$1 Y$2 Z$3") & @CRLF Until 0 FileClose($hFile) $newfile = FileSaveDialog("Save the result", @ScriptDir, "Text (*.txt)", 2) If @error Then Exit $hFile = FileOpen($newfile, 2) FileWrite($hFile, $sNew) FileClose($hFile) ;method 3: read line / write line for very huge files but very slow $newfile = FileSaveDialog("Save the result", @ScriptDir, "Text (*.txt)", 2) If @error Then Exit $hFile_r = FileOpen($filename) $hFile_w = FileOpen($newfile, 2) While 1 $line = FileReadLine($hFile_r) If @error = - 1 Then ExitLoop FileWriteLine($hFile_w, StringRegExpReplace($line, "(.*),(.*),(.*)", "X$1 Y$2 Z$3")) WEnd FileClose($hFile_r) FileClose($hFile_w) Assumption: roadrunner.txt content format is always a,b,c whereas a,b,c are numbers Br, UEZ Edited June 9, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Sorian Posted June 10, 2011 Author Share Posted June 10, 2011 (edited) Thanks UEZ, Your 3 methods ran, but missed the properties. Fix it this way; $filename = FileOpenDialog("Select Scan File", @ScriptDir, "Text (*.txt)") If @error Then Exit $hFile = FileOpen($filename) $sNew = "" Do $line = FileReadLine($hFile) If @error = - 1 Then ExitLoop $sNew &= StringRegExpReplace($line, "(.*),(.*),(.*)", "X$1 Y$2 Z$3") & @CRLF Until 0 FileClose($hFile) $newfile = FileSaveDialog("Save the result", @ScriptDir, "Text (*.txt)", 18) ; 18 does both verify correct path and if you want to rewrite over file. If @error Then Exit If FileExists($newfile) Then ; $hFile = FileOpen($newfile, 2) ; This If...Then...Else.. section is to rewrite over the file if it exists... Else ; $hFile = FileOpen($newfile & ".txt", 2) ; ...or adds .txt to end if it doesn't. EndIf ; FileWrite($hFile, $sNew) FileClose($hFile) Exit Thank you all for you help! Edited June 10, 2011 by Sorian 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