BrandNewTester Posted August 13, 2012 Share Posted August 13, 2012 Hi alll.... I m new to Autoit. i want to read data from file. i pasted my file here and explain the problem also.. test.txt 123.234.567; uname1;password1 878.534.524; uname2;password2 809.787.973;uname3;passwd3 this way number of lines,here i need first line first value like 123.234.567 after that second value uname1 and third value password1.. same way for second line,third line etc... how to read that field by field content... please any one help me to do this... thanks in advance......... Link to comment Share on other sites More sharing options...
UEZ Posted August 13, 2012 Share Posted August 13, 2012 One possibility is to read the file and split it into a 2d array. How? E.g. here: Br, 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...
water Posted August 13, 2012 Share Posted August 13, 2012 (edited) And because this questions are so basic you should make sure to read one of the AutoIt tutorials. Edited August 13, 2012 by water 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 Link to comment Share on other sites More sharing options...
BrandNewTester Posted August 13, 2012 Author Share Posted August 13, 2012 I m not getting from the tutorial.. plz any one help me... thanks.. BNT Link to comment Share on other sites More sharing options...
water Posted August 13, 2012 Share Posted August 13, 2012 Does the example given by UEZ work for you? 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 Link to comment Share on other sites More sharing options...
UEZ Posted August 13, 2012 Share Posted August 13, 2012 (edited) Try this:expandcollapse popup#include <Array.au3> $sText = FileRead(@ScriptDir & "test.txt") $aList = StringSplitW($sText, ";") _ArrayDisplay($aList) ; #FUNCTION# ======================================================================================================================================== ; Name .................: StringSplitW() ; Description ..........: Splits a string into columns instead of rows as it is done by SplitString(), like a csv file to a 2d array ;-) ; Syntax ...............: StringSplitW($sString, $sDelimiter, $iWidthLen) ; Parameters ...........: $sString - string to split ; $sDelimiter - [optional] the delimter how to split the string ; $iWidthLen - [optional] length of the row (amount of columns - default is 256) ; Return values .......: Success - 2d array ; Error 1 - either $sString or $delimter is not set ; Error 2 - array width exceeded ; Error 3 - error splitting string ; ; Version .............: v0.96 build 2015-01-20 beta ; Author ..............: UEZ ; Modified ............: ; Remarks .............: RegEx take from http://stackoverflow.com/questions/4476812/regular-expressions-how-to-replace-a-character-within-quotes ; Related .............: StringSplit, StringReplace, StringRegExpReplace, StringLen, StringStripCR ; =================================================================================================================================================== Func StringSplitW($sString, $sDelimiter = ";", $sQuotationMark = '"', $sDummy = "¦", $iWidthLen = 256) If $sString = "" Or $sDelimiter = "" Then Return SetError(1, 0, 0) Local $chk, $iWidth, $i, $j, $k, $iLen, $iMax = 1, $iMaxWidth Local $aPos[1], $l = 0 Local $aSplit = StringSplit(StringStripCR($sString), @LF) If @error Then Return SetError(3, 0, 0) Local $aVertical[$aSplit[0]][$iWidthLen], $iDelimiterLen = StringLen($sDelimiter) - 1, $sLine For $k = 1 To $aSplit[0] $iLen = StringLen($aSplit[$k]) If $iLen > 1 Then $sLine = StringRegExpReplace($aSplit[$k], '(?m)\' & $sDelimiter & '(?=[^' & $sQuotationMark & ']*' & $sQuotationMark & '(?:[^' & $sQuotationMark & '\r\n]*' & $sQuotationMark & '[^' & $sQuotationMark & ']*' & $sQuotationMark & ')*[^' & $sQuotationMark & '\r\n]*$)', $sDummy) $chk = StringReplace($sLine, $sDelimiter, $sDelimiter) $iWidth = @extended If $iWidth > $iWidthLen Then Return SetError(2, 0, 0) If $iWidth >= $iMax Then $iMax = $iWidth + 1 Switch $iWidth Case 0 $aVertical[$l][0] = $sLine Case Else Dim $aPos[$iWidth * 2 + 2] $j = 1 $aPos[0] = 1 For $i = 0 To $iWidth - 1 $aPos[$j] = StringInStr($sLine, $sDelimiter, 0, $i + 1) - 1 $aPos[$j + 1] = $aPos[$j] + 2 + $iDelimiterLen $j += 2 Next $aPos[UBound($aPos) - 1] = StringLen($sLine) $j = 0 For $i = 0 To UBound($aPos) - 1 Step 2 $aVertical[$l][$j] = StringMid(StringReplace($sLine, $sDummy, $sDelimiter), $aPos[$i], $aPos[$i + 1] - $aPos[$i] + 1) $j += 1 Next EndSwitch $l += 1 EndIf Next ReDim $aVertical[$l][$iMax] Return $aVertical EndFuncBr,UEZ Edited January 19, 2015 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...
RedneckTech Posted August 13, 2012 Share Posted August 13, 2012 (edited) UEZ, that's a very well coded script, but something more basic may be in order in this case just so he understands what's actually going on, now keep in mind, this is a very lazy way to do it, but it works. $n=1 $infohandle = filepath.inf ;^this calls the variable and sets it as the path to your test text file[/i] While 1 ;^setting while 1 basically says to stay in the loop until i tell you to exit this loop ;be careful doing this as you can easily end up with an infinite loop which can cause your computer to lock and reboot $line = FileReadLine($Infohandle, $n) If @error = -1 Then ExitLoop Else $n = $n +1 EndIf ;^This is the exit line. this will cause the loop to continue till the end of the file is reach, at which case it will stop reading your .inf file $ctrlarray = StringSplit($line, $delimeter) ;^In this case use [i]";"[/i] instead of $delimeter ;now do your stuff here _MyFunc() Wend Func MyFunc() Whatever You want to do with your stuff EndFunc In that case your pieces will look like this: 123.234.567; uname1;password1 $ctrlarray[1] = 123.234.567 $ctrlarray[2] = uname1 $ctrlarray[3] = password This will continue for every line and will change the $ctrlarray variables for every loop, which will do one loop for ever line. If you guys want to check over that to make sure it's right, please do as im kinda out of it today Edit: Edited for $n = $n +1 factor for counting lines Edited August 13, 2012 by RedneckTech Link to comment Share on other sites More sharing options...
UEZ Posted August 13, 2012 Share Posted August 13, 2012 (edited) @RedneckTech: many ways lead to rome, also easier ones. This kind of question has been asked and answered several times already. I showed only a way which might be look more complicated than others. E.g. your example shortend: #include <Array.au3> $aList = StringRegExp(FileRead(@ScriptDir & "test.txt"), "(.*);(.*);(.*)", 3) _ArrayDisplay($aList) Is this example better to understand? For me at least, is the best way to analyse and understand an example because you are forced to look to the help file to understand and thus you will get more information and learn other stuff. Br, UEZ Edited August 13, 2012 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...
kylomas Posted August 13, 2012 Share Posted August 13, 2012 BrandNewTester, Yet another way to Rome... Simplified version simialr to UEZ's post. #include <Array.au3> Opt("MustDeclareVars", 1) local $a1,$a2 local $string = "12345:a100:a200:9876:0:dfgh" & @crlf & _ "string:z43:%:6:0" & @crlf & _ "str:z:%the:9876:0:dfgh:what:the :hell" & @crlf & _ "basketball" & @crlf & @crlf & "this:is:an:example:row:following:a:blank:row" & @lf & "test:for:@lf:mixed:with:@CRLF" local $result_array = _stringsplit2d($string,":") _arraydisplay($result_array) ; #FUNCTION# ====================================================================================== ; Name ................: _stringsplit2d($str,$del) ; Description .........: create 2d array where each row is a @lf delimited text string comprised ; of columns delimited by a user defined string ; Syntax ..............: StringSplitW($str, $del) ; Parameters ..........: $str - string to split ; $del - the delimter for columns ; ================================================================================================= func _stringsplit2d($str,$del) $a1 = stringsplit($str,@lf,1) local $rows = ubound($a1),$cols = 0 ; determine max number of columns by splitting each row and keeping highest ubound value for $i = 0 to ubound($a1) - 1 $a2 = stringsplit($a1[$i],$del,1) if ubound($a2) > $cols then $cols = ubound($a2) next ; define and populate array local $o[$rows][$cols] for $i = 1 to $rows - 1 $a2 = stringsplit($a1[$i],$del,1) for $j = 0 to ubound($a2) - 1 $o[$i][$j] = $a2[$j] Next next return $o endfunc *** Warning - _arraydisplay is slow and has a size limit of appx. 64000 rows, depending on the number of controls in your scipt. 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 Link to comment Share on other sites More sharing options...
RedneckTech Posted August 13, 2012 Share Posted August 13, 2012 (edited) Now I'm curious. Any particular reason you guys split the whole inf file into a 2d array rather than doing a separate loop for each line? I guess speed, but thinking realistically, how long is his inf file going to be if he's storing user passwords in it? Edited August 13, 2012 by RedneckTech Link to comment Share on other sites More sharing options...
kylomas Posted August 13, 2012 Share Posted August 13, 2012 RedneckTech, Because the OP asked for a row/col format, similar to a spread sheet. 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 Link to comment Share on other sites More sharing options...
kylomas Posted August 13, 2012 Share Posted August 13, 2012 BrandNewTester, Long, boring afternoon so I cleaned up the code a bit and added a function to create a formatted output file. Enjoy! expandcollapse popup#include <Array.au3> Opt("MustDeclareVars", 1) local $string = "12345:a100:a200:9876:0:dfgh" & @crlf & _ "string:z43:%:6:0" & @crlf & _ "str:z:%the:9876:0:dfgh:what:the :hell" & @crlf & _ "basketball" & @crlf & @crlf & "this:is:an:example:row:following1234567890:a:blank:row" & @lf & "123456789:for:@lf:mixed:with:@CRLF" local $result_array = _stringsplit2d($string,":") _arraydisplay($result_array) _print2d_array($string,":","c:dsdtemptest.txt") run ("notepad.exe " & "c:dsdtemptest.txt") ; #FUNCTION# ====================================================================================== ; Name ................: _stringsplit2d($str,$del) ; Description .........: create 2d array where each row is a @lf delimited text string comprised ; of columns delimited by a user defined string ; Syntax ..............: StringSplitW($str, $del) ; Parameters ..........: $str - string to split ; $del - the delimter for columns ; ================================================================================================= func _stringsplit2d($str,$del) local $a1 = stringsplit($str,@lf,1), $a2 local $rows = ubound($a1),$cols = 0 ; determine max number of columns by splitting each row and keeping highest ubound value for $i = 0 to ubound($a1) - 1 $a2 = stringsplit($a1[$i],$del,1) if ubound($a2) > $cols then $cols = ubound($a2) next ; define and populate array local $o[$rows][$cols] for $i = 1 to $rows - 1 $a2 = stringsplit($a1[$i],$del,1) for $j = 0 to ubound($a2) - 1 $o[$i][$j] = $a2[$j] Next next return $o endfunc ; #FUNCTION# ====================================================================================== ; Name ................: _print2d_array($str,$del$output_file_name) ; Description .........: create a formatted text file from a 2d array ; Syntax ..............: StringSplitW($str, $del) ; Parameters ..........: $str - string to split ; $del - the delimter for columns ; $output_file_name - fully qualified output file name ; ================================================================================================= Func _print2d_array($str,$del,$output_file_name) $str = stringregexpreplace($str,@crlf,@lf) Local $a10 = StringSplit($str,@lf,3), $numcols = 0, $a20, $matrix = "" ; find max number of columns For $i = 0 To UBound($a10) - 1 $a20 = StringSplit($a10[$i],$del) if ubound($a20) > $numcols then $numcols = ubound($a20) Next ; find max size of each column local $colsize[$numcols] For $i = 0 To UBound($a10) - 1 $a20 = StringSplit($a10[$i],$del) For $j = 0 To $a20[0] If StringLen($a20[$j]) > $colsize[$j] Then $colsize[$j] = StringLen($a20[$j]) + 5 Next Next ; now format the output line For $i = 0 To UBound($a10) - 1 $a20 = StringSplit($a10[$i],$del) For $j = 0 To $a20[0] $matrix &= StringFormat("%-" & $colsize[$j] & "s",$a20[$j]) & "|" Next $matrix &= @CRLF next ; and finally write the output file local $hfl = FileOpen($output_file_name,2) FileWrite($hfl,$matrix) FileClose($hfl) $hfl = 0 endfunc kylomas BrandNewTester 1 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 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