Sign in to follow this  
Followers 0
jtrout

Help with Recursion Level

8 posts in this topic

I have looked at some of the examples and know that I am doing a loop that basically never ends till the end of the file is reached but can someone help me clean this up so I don't get the recursion level has been exceeded - AutoIt will quit to prevent stack overflow.

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=..\..\..\..\..\Program Files\AutoIt3\Icons\CNFNOT.ICO
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
Opt("MustDeclareVars", 1); Variables must be pre-declared
#include <GUIConstants.au3>
#include <file.au3>
Global $ADSCleanerr, $ADSCleanerrinfo, $ADSCleanexe, $ADSCleanexeinfo, $ADSCleanlog, $ADSCleanloginfo
Global $ADSMassCleanerr, $ADSMassCleanerrinfo, $ADSMassCleanexe, $ADSMassCleanexeinfo, $ADSMassCleanlog
Global $ADSMassCleanloginfo, $count, $day, $hour, $Input, $InputFile, $InputFileLines, $InputServer
Global $LineCount, $log, $logmsg, $min, $month, $outputtxt, $passwd, $pathtomap, $sec, $timestamp
Global $user, $year
Global $ADSMassCleanerrinfodate, $ADSMassCleanloginfodate, $ADSCleanerrinfodate, $ADSCleanloginfodate
Global $ADSMassCleanexeinfodate, $ADSCleanexeinfodate, $ADSCleanerrinfodate

DriveMapDel("Q:")
$LineCount = 1
$logmsg = "Start of ADSClean Data Gathering."
logger($logmsg)

$InputFile = 'C:\Temp\Serverlist.txt'
FileOpen($InputFile, 0)
If $InputFile = -1 Then
    $logmsg = "Unable to open Serverlist.txt file."
    logger($logmsg)
    Exit 1
EndIf

$InputFileLines = _FileCountLines($InputFile)
$logmsg = "Number of lines in Serverlist.txt is: " & $InputFileLines
logger($logmsg)

Loop()

Func Loop()
    If $LineCount <= $InputFileLines Then
        $Input = FileReadLine($InputFile, $LineCount)
        $logmsg = "On " & $LineCount & " out of " & $InputFileLines
        logger($logmsg)
        DriveMapDel("Q:")
        If FileExists("Q:") Then
            Sleep(5000);5 seconds
        EndIf
        mapdrive()
        chkfiles()
        writetxtfile()

    ;Add one to $LineCount
        $LineCount += 1
        
        Loop()
    Else
        $logmsg = "End of Serverlist.txt file.  Operation complete."
        logger($logmsg)
        DriveMapDel("Q:")
        Exit 1
    EndIf
EndFunc  ;==>Loop

Func mapdrive()
    $pathtomap = **********
    $user = *******
    $passwd = ********
    DriveMapAdd("Q:", $pathtomap, 0, $user, $passwd)
    $count = 1
    While 1
        If FileExists("Q:") Then
            $logmsg = "Drive mapped successfully to " & $Input
            logger($logmsg)
            ExitLoop
        Else
            Sleep(5000);5 seconds
            $count = $count + 1
            If $count = 20 Then
                MsgBox(0, "Map Drive Error", "Could not map to " & $Input, 5)
                $logmsg = "Could not map to " & $Input
                logger($logmsg)
                ExitLoop
            EndIf
        EndIf
    WEnd
EndFunc  ;==>mapdrive

Func chkfiles()
    If FileExists("Q:\Logs\ADSMassClean.err") Then
        $ADSMassCleanerr = "Y"
        $ADSMassCleanerrinfo = FileGetTime("Q:\Logs\ADSMassClean.err", 0)
        $ADSMassCleanerrinfodate = $ADSMassCleanerrinfo[1] & "/" & $ADSMassCleanerrinfo[2] & "/" & $ADSMassCleanerrinfo[0] & " " & _
                $ADSMassCleanerrinfo[3] & ":" & $ADSMassCleanerrinfo[4] & ":" & $ADSMassCleanerrinfo[5]
    Else
        $ADSMassCleanerr = "N"
        $ADSMassCleanerrinfodate = ""
    EndIf
    
    If FileExists("Q:\Logs\ADSMassClean.log") Then
        $ADSMassCleanlog = "Y"
        $ADSMassCleanloginfo = FileGetTime("Q:\Logs\ADSMassClean.log", 0)
        $ADSMassCleanloginfodate = $ADSMassCleanloginfo[1] & "/" & $ADSMassCleanloginfo[2] & "/" & $ADSMassCleanloginfo[0] & " " & _
                $ADSMassCleanloginfo[3] & ":" & $ADSMassCleanloginfo[4] & ":" & $ADSMassCleanloginfo[5]
    Else
        $ADSMassCleanlog = "N"
        $ADSMassCleanloginfodate = ""
    EndIf
    
    If FileExists("Q:\Logs\ADSClean.err") Then
        $ADSCleanerr = "Y"
        $ADSCleanerrinfo = FileGetTime("Q:\Logs\ADSClean.err", 0)
        $ADSCleanerrinfodate = $ADSCleanerrinfo[1] & "/" & $ADSCleanerrinfo[2] & "/" & $ADSCleanerrinfo[0] & " " & _
                $ADSCleanerrinfo[3] & ":" & $ADSCleanerrinfo[4] & ":" & $ADSCleanerrinfo[5]
    Else
        $ADSCleanerr = "N"
        $ADSCleanerrinfodate = ""
    EndIf
    
    If FileExists("Q:\Logs\ADSClean.log") Then
        $ADSCleanlog = "Y"
        $ADSCleanloginfo = FileGetTime("Q:\Logs\ADSClean.log", 0)
        $ADSCleanloginfodate = $ADSCleanloginfo[1] & "/" & $ADSCleanloginfo[2] & "/" & $ADSCleanloginfo[0] & " " & _
                $ADSCleanloginfo[3] & ":" & $ADSCleanloginfo[4] & ":" & $ADSCleanloginfo[5]
    Else
        $ADSCleanlog = "N"
        $ADSCleanloginfodate = ""
    EndIf
    
    If FileExists("Q:\ADSMassClean.exe") Then
        $ADSMassCleanexe = "Y"
        $ADSMassCleanexeinfo = FileGetTime("Q:\ADSMassClean.exe", 0)
        $ADSMassCleanexeinfodate = $ADSMassCleanexeinfo[1] & "/" & $ADSMassCleanexeinfo[2] & "/" & $ADSMassCleanexeinfo[0] & " " & _
                $ADSMassCleanexeinfo[3] & ":" & $ADSMassCleanexeinfo[4] & ":" & $ADSMassCleanexeinfo[5]
    Else
        $ADSMassCleanexe = "N"
        $ADSMassCleanexeinfodate = ""
    EndIf
    
    If FileExists("Q:\ADSClean.exe") Then
        $ADSCleanexe = "Y"
        $ADSCleanexeinfo = FileGetTime("Q:\ADSClean.exe", 0)
        $ADSCleanexeinfodate = $ADSCleanexeinfo[1] & "/" & $ADSCleanexeinfo[2] & "/" & $ADSCleanexeinfo[0] & " " & _
                $ADSCleanexeinfo[3] & ":" & $ADSCleanexeinfo[4] & ":" & $ADSCleanexeinfo[5]
    Else
        $ADSCleanexe = "N"
        $ADSCleanexeinfodate = ""
    EndIf
EndFunc  ;==>chkfiles

Func Convertdate($date)
    
EndFunc  ;==>Convertdate

Func logger($logmsg)
    $log = FileOpen("C:\Temp\ADSClean.log", 1)
    getTimeStamp()
    FileWriteLine($log, $timestamp & " - " & $logmsg)
    FileClose($log)
EndFunc  ;==>logger

Func writetxtfile()
    $outputtxt = FileOpen("C:\Temp\ADSClean.txt", 1)
;ServerName, ADSMassClean.err File (Y/N), ADSMassClean.log (Y/N), ADSClean.err (Y/N), ADSClean.log (Y/N), ADSMassClean.exe (Y/N), ADSClean.exe (Y/N)
    FileWriteLine($log, $Input & "," & $ADSMassCleanerr & "," & $ADSMassCleanerrinfodate & "," & _
            $ADSMassCleanlog & "," & $ADSMassCleanloginfodate & "," & _
            $ADSCleanerr & "," & $ADSCleanerrinfodate & "," & _
            $ADSCleanlog & "," & $ADSCleanloginfodate & "," & _
            $ADSMassCleanexe & "," & $ADSMassCleanexeinfodate & "," & _
            $ADSCleanexe & "," & $ADSCleanexeinfodate)
    FileClose($outputtxt)
EndFunc  ;==>writetxtfile

Func getTimeStamp()
    $month = @MON
    $day = @MDAY
    $year = @YEAR
    $hour = @HOUR
    $min = @MIN
    $sec = @SEC
    $timestamp = $year & $month & $day & " " & $hour & ":" & $min & ":" & $sec
EndFunc  ;==>getTimeStamp

Help me Obi-wan your my only hope

Share this post


Link to post
Share on other sites



Your Loop() function is using recursion where it isn't needed. Use _FileReadToArray instead, then perform your processing.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

you recursion come from there:

"---------> Loop() <------------"

Sleep(5000);5 seconds
        EndIf
        mapdrive()
        chkfiles()
        writetxtfile()

    ;Add one to $LineCount
        $LineCount += 1
        
 ---------> Loop() <------------oÝ÷ ÙªÞrêìåz÷¥Ì!z|¨¹Æ¥Ø^~éܶ*'{"u쨹ªÞ)jëh×6func loop()
      loop()
endfunc

So what happen, is a new scope called loop, is created inside the scope loop, it is ok to do that, but never more then 250.

It a little like looking a mirror from a mirror, you know? image become duplicated inside each other :)

I think from your code, it is not good usage you do, since you recall your loop function. better do a while $notAskedToBreak.

What ever, i hope it help you find your way :)

Edited by Greenseed

GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Share this post


Link to post
Share on other sites

Your Loop() function is using recursion where it isn't needed. Use _FileReadToArray instead, then perform your processing.

Under a bit of a time crunch can you let me know where I need to use this? Thanks for your help!

Share this post


Link to post
Share on other sites

I can't really test your code. It has a lot of logic problems. Like why are you deleting a drive mapping after every line is read in????????

#include <File.au3>
Dim $fileArray
_FileReadToArray ($InputFile, $fileArray )

Loop()

Func Loop()
    For $X = 1 to $fileArray[0]
        $logmsg = "On " & $X & " out of " & $fileArray[0]
        logger($logmsg)
        DriveMapDel("Q:")
        If FileExists("Q:") Then
            Sleep(5000);5 seconds
        EndIf
        mapdrive()
        chkfiles()
        writetxtfile()
    Next
    
    $logmsg = "End of Serverlist.txt file.  Operation complete."
    logger($logmsg)
    DriveMapDel("Q:")    
EndFunc  ;==>Loop

Share this post


Link to post
Share on other sites

Under a bit of a time crunch can you let me know where I need to use this? Thanks for your help!

Something like this?!?

If _FileReadToArray($InputFile, $serverarray) Then
        For $x = 1 To $serverarray[0]
            $Input = $serverarray[$x]
            $logmsg = "On " & $LineCount & " out of " & $InputFileLines
            logger($logmsg)
            DriveMapDel("Q:")
            If FileExists("Q:") Then
                Sleep(5000);5 seconds
            EndIf
            mapdrive()
            chkfiles()
            writetxtfile()
        Next
    Else
        $logmsg = "End of Serverlist.txt file.  Operation complete."
        logger($logmsg)
        DriveMapDel("Q:")
        Exit 1
    EndIf

Share this post


Link to post
Share on other sites

I can't really test your code. It has a lot of logic problems. Like why are you deleting a drive mapping after every line is read in????????

#include <File.au3>
Dim $fileArray
_FileReadToArray ($InputFile, $fileArray )

Loop()

Func Loop()
    For $X = 1 to $fileArray[0]
        $logmsg = "On " & $X & " out of " & $fileArray[0]
        logger($logmsg)
        DriveMapDel("Q:")
        If FileExists("Q:") Then
            Sleep(5000);5 seconds
        EndIf
        mapdrive()
        chkfiles()
        writetxtfile()
    Next
    
    $logmsg = "End of Serverlist.txt file.  Operation complete."
    logger($logmsg)
    DriveMapDel("Q:")    
EndFunc  ;==>Loop
That worked thanks!!!!

Share this post


Link to post
Share on other sites

Good eye Greenseed, that was the real problem.

If you are reading average size files _FileReadToArray() is fine however you could get an error if you are reading very large files. Your system for reading the file was fine.

However, you have incorrect info for opening the file

$InputFile = 'C:\Temp\Serverlist.txt'
FileOpen($InputFile, 0)
If $InputFile = -1 Then

$inputFile is not used for the error correctly.

8)


NEWHeader1.png

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