Sign in to follow this  
Followers 0
Rhazz

Logic problem: "For" loop and multiple file creating/editing

4 posts in this topic

Hello. First of all, my full code:

#include <WinAPI.au3>
#include <File.au3>
#include <String.au3>

Local $aFiles, $IntOrFloat
Local $aFileToRead = _WinAPI_GetOpenFileName("Open file to read", "Text Files (*.txt)")
Local $aFinalFile = _WinAPI_GetSaveFileName("How do you want to save the file?", "Text Files (*.txt)")
Local $sFilenameWithoutExtension = _StringBetween($aFinalFile[2],"",".")
Local $sLinesPerLine = InputBox("Lines Per Line", "How many lines per line do you want?","","")
Local $sCountLines = _FileCountLines($aFileToRead[2])

If ($sCountLines/$sLinesPerLine) >= 1 Then ; When the division result is a number with decimal, there will be less words in the last file and the script won't create it , therefore I add 1 to the result to prevent a file lack. Am I wrong? For example: if user introduces 3 the operation will be "4/3=1.33" and I need two files, the first "line1,line2,line3" and the second "line4".
    $IntOrFloat = $sCountLines/$sLinesPerLine
Else
    $IntOrFloat = ($sCountLines/$sLinesPerLine) + 1
EndIf


For $i = 1 To $IntOrFloat
    MsgBox(0,"", "Creating file " & $i)
    $aFiles = $aFinalFile[1] & "\" & $sFilenameWithoutExtension[0] & "-" & $i & ".txt"
    _FileCreate($aFiles)
    FileOpen($aFiles,2)
    For $a = ( ( ( $i - 1 ) * $sLinesPerLine ) + 1 ) To ( $i * $sLinesPerLine )
        FileWrite($aFinalFile[2],FileReadLine($aFileToRead[2], $a) & ",")
    Next
    FileClose($aFiles)
Next

MsgBox(0,"Done","Done")

With it I want to read a text file with a lot of lines and I wanna split it in multiple files with multiple lines (of the first file) in each line, separated by commas.

For example: the user executes the script and selects a text file to open that contains:

Line 1 asdf

Line 2 asdf

Line 3 asdf

Line 4 asdf

Then, the user selects the path and name of the result files. Suppose "C:\result.txt"

Finally, the user introduces the number of lines per line (lines of the initial text file to each line of the result text files). Suppose the user enters 2.

So far my code works fine. But I tried to code a loop for do (continuing the example):

  • Start first loop repetition.
  • Create "C:\result.-1.txt", and write on it "Line 1 asdf,Line 2 asdf"
  • End first loop repetition.
  • Start second loop repetition.
  • Create "C:\result.-2.txt", and write on it "Line 3 asdf,Line 4 asdf"
  • End second loop repetition.

But my code results (continuing the example) in three files created:

  • C:\result.txt that contains "Line 1 asdf,Line 2 asdf,Line 3 asdf,Line 4 asdf,"
  • C:\result.-1.txt that contains nothing.
  • C:\result.-2.txt that contains nothing.

Where is the error? I can't understand where is it... :(

Share this post


Link to post
Share on other sites



At a quick glance I think maybe this line...

FileWrite($aFinalFile[2],FileReadLine($aFileToRead[2], $a) & ",")

Should be...

FileWrite($aFiles,FileReadLine($aFileToRead[2], $a) & ",")

Of course I could be wrong, but if I'm not you should also close that file in the loop the line is in.

1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

At a quick glance I think maybe this line...

FileWrite($aFinalFile[2],FileReadLine($aFileToRead[2], $a) & ",")

Should be...

FileWrite($aFiles,FileReadLine($aFileToRead[2], $a) & ",")

Of course I could be wrong, but if I'm not you should also close that file in the loop the line is in.

Yes! There is it. When I read it I can find the mistake... and I tried and it worked!

Thanks :)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • ur
      By ur
      How to get the thrid number of a file version.
      Example: Let's say I have a file with below properties.
      I want only the third number like here it is 1941 as highlighted.
      Is there any built in function to get it??

    • ur
      By ur
      Generally we will use tools like Winmerge or Beyond Compare for this purpose.
      Are there any UDF or libraries available in AutoIT to compare any two files or folder contents.
    • tremolux66
      By tremolux66
      I've run into the problem where multiple script processes writing to a log file at the same time can interfere with one another. That is, file writes using FileWriteLine are not atomic, so a line of text from one process can have fragments of text from other processes mixed in with it. Exclusive file-locking should take care of the problem.
      Looking through the standard UDFs, I see that _WinAPI_LockFile is available, but according to the MSDN documentation it doesn't block - it returns immediately if the lock can't be obtained - and there doesn't seem to be a UDF for the LockFileEx function. I considered writing my own UDF for LockFileEx, but some aspects (e.g., creating the OVERLAPPED structure and its members) look too complex for the amount of time I have available. I think approximating LockFileEx using LockFile could solve my problem, but I'd like to keep it simple while still working reliably. I'm not confident that (e.g.) a simple retry loop to simulate blocking will be adequate.
      Any suggestions for a simple way to achieve exclusive file-locking with blocking?
    • Rapidnxit
      By Rapidnxit
      Hi! I want to send by mail some files with a certain extension (.xml in my case). These files are located in the script folder. First, I thought to use _FileListToArrayRec function to have a list of these files. But than I don't know how to send them all at once. I know that I can attach multiple files in this way: path1;path2;path3; etc.  and so I have tried to make a string of this type with the path of the files (with a for loop) but It doesn't attach any file (only instructions.txt). How could I do? ( what I did is just an idea, if there's something better that would be great) Thanks!
      $aArrayXml = _FileListToArrayRec(@ScriptDir, "*.xml", $FLTAR_FILES) _ArrayDisplay($aArrayXml, "LIST XML") $LenghtArrayXml = UBound($aArrayXml) If ($LenghtArrayXml > 2) Then For $i = 2 To $LenghtArrayXml - 1 $temp = "&@ScriptDir&""\"&$aArrayXml[$i]&";"&"""" $XmlListFile = $XmlListFile & $temp Next EndIf $XmlListFile = StringTrimLeft($XmlListFile, 1) if($LenghtArrayXml = 2)Then $rc =_INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @ScriptDir&"\"&$aArrayXml[1]&";"&@ScriptDir&"\instructions.txt", $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl) Else $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @ScriptDir&"\"&$aArrayXml[1]&$XmlListFile&@ScriptDir&"\instructions.txt", $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl) EndIf  
    • Jdr
      By Jdr
      I'm trying to check for the existance of a file, on logon the file is there, then gets deleted and after a while recreated. I'm using this code:
      Sleep(100) If not FileExists($sFile) Then Sleep(150) If FileExists($sFile) Then GUICtrlSendToDummy($iDummy) What happens is this: 
      User logs on, the file is present, or at least it usually is After about 10 seconds the file gets deleted Then after about 50 seconds the file gets created again, this is when I need the check, because now the process is finished. The above code is part of a pretty long do....until false loop running an animation.
      The problem is that sometimes this code fails. Without the sleep statements it didn't work at all so I think this is a timing issue. Pretty primitive code I admit, there must be a better way...