Jump to content

FileWrite stops after writing 510 files


Recommended Posts

Hi coders, I sure help someone can help me with this. Here is a general description of what the script is inteded to do:

There are 3 'While' loops that read two text files and one directory of 285 files with the intention of creating 14 directories, and thousands of files. To do this, Loop #2 cycles through a directory of 285 files and uses FileWrite to create a new file for each file in the directory until Loop #1 reaches the end after 14 cycles. Loop #3 creates more files with FileWrite based upon the newly created files.

The problem is that the script is only creating 2 folders and stops after creating file # 225 in the second folder, for a total of 510 writes. When running properly it should write 3990 times (14 cycles x 285 files)

Thank you for your help.

$strategies = FileOpen("strategies.txt", 0)
If $strategies = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

While 1
$strategiesline = FileReadLine($strategies)
If @error = -1 Then ExitLoop

$strategieslinearray = stringsplit($strategiesline, @TAB)

$suffix = $strategieslinearray[1]
$dir = $strategieslinearray[1]
$replacement = $strategieslinearray[2]

dircreate($dir)

    $search = FileFindFirstFile("*.WS")  

    If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
    EndIf

    While 1
    $currentstrategyfilename = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    SplashTextOn($suffix,$currentstrategyfilename)
    sleep(100)
    splashoff()
   
    $currentstrategycode = fileread($currentstrategyfilename)
    $newstrategycode = stringreplace($currentstrategycode, "SellAtMarket( Bar + 1, p, '' );", $replacement)
    $newstrategyfilename = $dir & "\" & StringTrimRight($currentstrategyfilename,3) & "_" & $suffix & ".WS"

    $newstrategyfilenamewrite = filewrite($newstrategyfilename,$newstrategycode)
    fileclose($newstrategyfilename)
    if $newstrategyfilenamewrite = 0 Then
    msgbox(0,"",$newstrategyfilename & " did not write, exiting...")
    EndIf
    
        $parameters = FileOpen("parameters.txt", 0)
        If $parameters = -1 Then
        MsgBox(0, "Error", "Unable to open " & $newstrategyfilename)
        EndIf

        While 1
        $parametersline = FileReadLine($parameters)
        If @error = -1 Then ExitLoop

        $parameterslinearray = stringsplit($parametersline, @TAB)
        $parametername = $parameterslinearray[1]
        $currentparameter = $parameterslinearray[2]
        $newparameter = $parameterslinearray[3]

        $newparametercode = stringreplace($newstrategycode, $currentparameter, $newparameter)
        $numreplacements = @extended

        if $numreplacements > 0 then

        SplashTextOn($newstrategyfilename,$parametersline)
        sleep(100)
        splashoff()

        $parameterfilename = $newstrategyfilename & "_" & $parametername & ".WS"
        $parameterfilewrite = filewrite($parameterfilename,$newparametercode)
        fileclose($parameterfilename)       
        EndIf

        Wend

    WEnd

FileClose($search)

WEnd

msgbox(0,"","done")
Link to comment
Share on other sites

  • Developers

You have 2 FileClose() statements for files you didn't open and forgot to close 1 that you did do a fileopen for:

$strategies = FileOpen("strategies.txt", 0)
If $strategies = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $strategiesline = FileReadLine($strategies)
    If @error = -1 Then ExitLoop

    $strategieslinearray = StringSplit($strategiesline, @TAB)

    $suffix = $strategieslinearray[1]
    $dir = $strategieslinearray[1]
    $replacement = $strategieslinearray[2]

    DirCreate($dir)

    $search = FileFindFirstFile("*.WS")

    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    While 1
        $currentstrategyfilename = FileFindNextFile($search)
        If @error Then ExitLoop

        SplashTextOn($suffix, $currentstrategyfilename)
        Sleep(100)
        SplashOff()

        $currentstrategycode = FileRead($currentstrategyfilename)
        $newstrategycode = StringReplace($currentstrategycode, "SellAtMarket( Bar + 1, p, '' );", $replacement)
        $newstrategyfilename = $dir & "\" & StringTrimRight($currentstrategyfilename, 3) & "_" & $suffix & ".WS"

        $newstrategyfilenamewrite = FileWrite($newstrategyfilename, $newstrategycode)
;~      FileClose($newstrategyfilename)
        If $newstrategyfilenamewrite = 0 Then
            MsgBox(0, "", $newstrategyfilename & " did not write, exiting...")
        EndIf

        $parameters = FileOpen("parameters.txt", 0)
        If $parameters = -1 Then
            MsgBox(0, "Error", "Unable to open " & $newstrategyfilename)
        EndIf

        While 1
            $parametersline = FileReadLine($parameters)
            If @error = -1 Then ExitLoop

            $parameterslinearray = StringSplit($parametersline, @TAB)
            $parametername = $parameterslinearray[1]
            $currentparameter = $parameterslinearray[2]
            $newparameter = $parameterslinearray[3]

            $newparametercode = StringReplace($newstrategycode, $currentparameter, $newparameter)
            $numreplacements = @extended

            If $numreplacements > 0 Then

                SplashTextOn($newstrategyfilename, $parametersline)
                Sleep(100)
                SplashOff()

                $parameterfilename = $newstrategyfilename & "_" & $parametername & ".WS"
                $parameterfilewrite = FileWrite($parameterfilename, $newparametercode)
;~              FileClose($parameterfilename)
            EndIf

        WEnd

        FileClose($parameters)

    WEnd

    FileClose($search)

WEnd

MsgBox(0, "", "done")

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Awesome Jos! Thanks so much! That worked!

Do you always use FileOpen and FileClose even on simple, non-looping scripts? Is it really that necessary? I tend not to use them and until I attempted this script with multiple 'While' statements never had a problem in the past.

Also, I had read in one of the Forum posts that FileWrite already contained the FileOpen and FileClose statements, so they weren't necessary, is this true?

Link to comment
Share on other sites

Thanks for the good information guys! I typically only use filenames instead of handles because its easier for me to comprehend. Will using filenames instead of handles ever cause errors to occur? Should I switch to using handles ASAP?

Link to comment
Share on other sites

If you are writing a lot of text to the individual file you are creating then use its handle, if you are just creating these thousands of files with nothing in them then I personally think a filename is easier.

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...