Jump to content

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


Rhazz
 Share

Recommended Posts

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... :(

Link to comment
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.

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

Monkey's are, like, natures humans.

Link to comment
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 :)

Link to comment
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
 Share

×
×
  • Create New...