senorita Posted December 17, 2009 Posted December 17, 2009 hi To everyone, Can anyone help me with this code I am having a a file list.txt in which using String Split i want to split the data in two files. please look into the attachment #include <file.au3> Local $path="d:\list.txt" Local $i Dim $roll _FileReadToArray($path,$roll) ; reading d:\list.txt into array If @error Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf For $i = 1 to UBound ($roll) - 1 $split = StringSplit ($roll [$i], 'verified') Next _FileWriteFromArray ($path,$split,0) help me with this codeProblem.txt
AndyG Posted December 17, 2009 Posted December 17, 2009 (edited) Hi, one of the billion possible solutions $text =fileread("test.txt") ;read textfile $delverified=stringreplace($text,"Verified",@crlf) ;replace "Verified" with CR+LF $array =stringsplit($delverified,@crlf,3) ;splits string in each line ; with _arraydisplay($array) you could see the $array, #include<array.au3> necessary for $i=0 to 1 ;2 files.... $file=fileopen("Textfile"&$i+1,2) ;open textfiles for output for $x=$i to ubound($array)-1 step 2 ;run through the array filewrite($file,stringstripWS($array[$x],8)&@crlf) ;write lines into file without any spaces Next fileclose($file) ;close file Next Edited December 17, 2009 by AndyG
ProgAndy Posted December 17, 2009 Posted December 17, 2009 (edited) I think, this solution is easier to understand: $text =fileread("test.txt") ;read textfile $text = StringSplit($text,@CRLF, 1) $hPathFile = FileOpen("Files.txt", 2) $hUserFile = FileOpen("Users.txt", 2) For $i = 1 To $text[0] $line = StringSplit($text[$i], "Verified",1) If $line[0] = 2 Then FileWrite($hPathFile, StringStripWS($line[1], 3) & @CRLF) FileWrite($hUserFile, StringStripWS($line[2], 3) & @CRLF) EndIf Next FileClose($hPathFile) FileClose($hUserFile) Edited December 17, 2009 by ProgAndy *GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes
senorita Posted December 18, 2009 Author Posted December 18, 2009 On 12/17/2009 at 4:25 PM, 'ProgAndy said: I think, this solution is easier to understand: $text =fileread("test.txt") ;read textfile $text = StringSplit($text,@CRLF, 1) $hPathFile = FileOpen("Files.txt", 2) $hUserFile = FileOpen("Users.txt", 2) For $i = 1 To $text[0] $line = StringSplit($text[$i], "Verified",1) If $line[0] = 2 Then FileWrite($hPathFile, StringStripWS($line[1], 3) & @CRLF) FileWrite($hUserFile, StringStripWS($line[2], 3) & @CRLF) EndIf Next FileClose($hPathFile) FileClose($hUserFile) Many Thanks
anixon Posted December 18, 2009 Posted December 18, 2009 This may also be helpful if you want run the routine as a function. You can modify it to suit your purpose. You will see that I have included an option Footer Record determined by the value passed to $sFooter as well as a compulsory 'End of File". I am also setting the files attributes to 'R'ead only and 'S'ystem. System gives you do you want to delete the file prompt in Windows. You will note that I also check that the file exists before running the process then is achieved by wrapping the code in a While statement and then using an Exitloop to skip running the code when the source file does not exist. Whilst my code might not be elegant it does work. expandcollapse popup$sReportId = "YourReport" $Title = "YourTitle" ;//Call the Routine [This code can be placed anywhere within your script at the point you want to process the file" _ProcessorPushedReports("c:\temp\", "Test.txt", 1) ;//Compile a Report Func _ProcessorPushedReports($sReportPath, $sReportID, $sFooter) ;//File Path $sUserRecRptID = @ScriptDir & $sReportId & ".txt" Local $sUserRptCounter While 1 Local $sUserReportRec, $sUserRecWrite ;//Exit if No File Found If Not FileExists($sReportPath) Then ExitLoop ;//Open the File $UserRptFile = FileOpen($sUserRecRptID, 9) ;//Read the File to an Array _FileReadToArray($sReportPath, $sUserReportRec) For $x = 1 To $sUserReportRec[0] $uRecRptID = "" $sUserRecWrite = "" ;//Split the Record on '|' $uRecRptID = StringSplit($sUserReportRec[$x], "|") $sUserRptCounter += 1 $sUserRecWrite = $sUserReportRec[$x] ;//Write the Record to the End of Line FileWriteLine($UserRptFile, $sUserRecWrite) Next ;//Add the Footer Record If $sFooter = 1 Then FileWriteLine($UserRptFile, "") FileWriteLine($UserRptFile, "Total Records " & $sUserRptCounter) FileWriteLine($UserRptFile, StringStripWS($Title, 8) & " - " & @ComputerName) EndIf FileWriteLine($UserRptFile, "") FileWriteLine($UserRptFile, "End of File ") FileWriteLine($UserRptFile, "") FileClose($UserRptFile) FileSetAttrib($sUserRecRptID, "+RS") ExitLoop WEnd EndFunc ;==>_ProcessorFetchedReports
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