Jump to content

Progress stuck in a loop


Recommended Posts

Could someone please help me figure out why my program is looping? I fear my 'for next' and while loops are stepping on each other. I'm basically setting up a script to scan folders for certain file types, count them and then write them to a csv.

Works fine without the progress bar, but after I add it I cannot figure out where its getting stuck.

I'm trying to sync the status bar progress based on a % of the number of folders divided by 100 so it steps through as the folders are being processed. It seems to work but when it gets to the end of the folder list the progress bar just keeps looping and the script is stuck within the while/for loop.

#Include <File.au3>
#Include <Array.au3>
#Include <Process.au3>

#NoTrayIcon

$xlsnum=1
If FileExists("c:\temp\dirlist.txt") Then
    FileDelete("c:\temp\dirlist.txt")
Endif

Msgbox(64,"Begin Script","Script will now start.  Click OK to continue.  Dialog box will timeout after 30 seconds and script will continue.",30)

ProgressOn("Progress","","0 percent")

;runwait (@Comspec &" /c dir \\evam01ntwk\firewall\*. /s /b > c:\temp\dirlist.txt")
_RunDos("dir c:\windows\*. /s /b > c:\temp\dirlist.txt")


$totalfoldersnum=_FileCountLines("c:\temp\dirlist.txt")
$counter=100/$totalfoldersnum
;Msgbox(262144,"Test",$counter)

;Msgbox (262144,"test",$totalfoldersnum)

If FileExists("c:\temp\output.csv") Then
    FileDelete("c:\temp\output.csv")
Endif

$output=FileOpen("c:\temp\Output.csv",2)
FileWriteLine($output,"Directory,Excel,Word,PowerPoint,Visio")


$dirlist=FileOpen("c:\temp\dirlist.txt",0)


While 1
For $i=1 to $totalfoldersnum Step $counter
ProgressSet($i,"Folder list created.  Scanning folders for files...")
    $dirline=FileReadLine($dirlist)
    If @error= -1 Then ExitLoop
        $xlsfiles=_FileListToArray($dirline,"*.xls",1)
            Select
                Case @error=0
                    $xlsnum=$xlsfiles[0]
                Case @error<>0
                    $xlsnum=0
                EndSelect
        $docfiles=_FileListToArray($dirline,"*.doc",1)
            Select
                Case @error=0
                    $docnum=$docfiles[0]
                Case @error<>0
                    $docnum=0
                EndSelect
        $pptfiles=_FileListToArray($dirline,"*.ppt",1)
            Select
                Case @error=0
                    $pptnum=$pptfiles[0]
                Case @error<>0
                    $pptnum=0
                EndSelect
        $vsdfiles=_FileListToArray($dirline,"*.vsd",1)
            Select
                Case @error=0
                    $vsdnum=$vsdfiles[0]
                Case @error<>0
                    $vsdnum=0
                EndSelect
        If $xlsnum+$docnum+$pptnum+$vsdnum>0 Then
         FileWriteLine($output, $dirline&","&$xlsnum&","&$docnum&","&$pptnum&","&$vsdnum)
        Endif
    If $i > $totalfoldersnum Then Exit
Next
WEnd
    

ProgressSet(100,"Done", "Complete")
Sleep (5000)
ProgressOff()
MsgBox(0,"Script Complete","Script has finished.  This dialog box will timeout after 5 minutes.",300)
Link to comment
Share on other sites

Could someone please help me figure out why my program is looping? I fear my 'for next' and while loops are stepping on each other. I'm basically setting up a script to scan folders for certain file types, count them and then write them to a csv.

Works fine without the progress bar, but after I add it I cannot figure out where its getting stuck.

I'm trying to sync the status bar progress based on a % of the number of folders divided by 100 so it steps through as the folders are being processed. It seems to work but when it gets to the end of the folder list the progress bar just keeps looping and the script is stuck within the while/for loop.

CODE
#Include <File.au3>

#Include <Array.au3>

#Include <Process.au3>

#NoTrayIcon

$xlsnum=1

If FileExists("c:\temp\dirlist.txt") Then

FileDelete("c:\temp\dirlist.txt")

Endif

Msgbox(64,"Begin Script","Script will now start. Click OK to continue. Dialog box will timeout after 30 seconds and script will continue.",30)

ProgressOn("Progress","","0 percent")

;runwait (@Comspec &" /c dir \\evam01ntwk\firewall\*. /s /b > c:\temp\dirlist.txt")

_RunDos("dir c:\windows\*. /s /b > c:\temp\dirlist.txt")

$totalfoldersnum=_FileCountLines("c:\temp\dirlist.txt")

$counter=100/$totalfoldersnum

;Msgbox(262144,"Test",$counter)

;Msgbox (262144,"test",$totalfoldersnum)

If FileExists("c:\temp\output.csv") Then

FileDelete("c:\temp\output.csv")

Endif

$output=FileOpen("c:\temp\Output.csv",2)

FileWriteLine($output,"Directory,Excel,Word,PowerPoint,Visio")

$dirlist=FileOpen("c:\temp\dirlist.txt",0)

While 1

For $i=1 to $totalfoldersnum Step $counter

ProgressSet($i,"Folder list created. Scanning folders for files...")

$dirline=FileReadLine($dirlist)

If @error= -1 Then ExitLoop

$xlsfiles=_FileListToArray($dirline,"*.xls",1)

Select

Case @error=0

$xlsnum=$xlsfiles[0]

Case @error<>0

$xlsnum=0

EndSelect

$docfiles=_FileListToArray($dirline,"*.doc",1)

Select

Case @error=0

$docnum=$docfiles[0]

Case @error<>0

$docnum=0

EndSelect

$pptfiles=_FileListToArray($dirline,"*.ppt",1)

Select

Case @error=0

$pptnum=$pptfiles[0]

Case @error<>0

$pptnum=0

EndSelect

$vsdfiles=_FileListToArray($dirline,"*.vsd",1)

Select

Case @error=0

$vsdnum=$vsdfiles[0]

Case @error<>0

$vsdnum=0

EndSelect

If $xlsnum+$docnum+$pptnum+$vsdnum>0 Then

FileWriteLine($output, $dirline&","&$xlsnum&","&$docnum&","&$pptnum&","&$vsdnum)

Endif

If $i > $totalfoldersnum Then Exit

Next

WEnd

ProgressSet(100,"Done", "Complete")

Sleep (5000)

ProgressOff()

MsgBox(0,"Script Complete","Script has finished. This dialog box will timeout after 5 minutes.",300)

The For/Next loop is not required, just do the math each time you update the progress:
<snip>

$totalfoldersnum = _FileCountLines("c:\temp\dirlist.txt")
$counter = 100 / $totalfoldersnum

If FileExists("c:\temp\output.csv") Then
    FileDelete("c:\temp\output.csv")
EndIf

$output = FileOpen("c:\temp\Output.csv", 2)
FileWriteLine($output, "Directory,Excel,Word,PowerPoint,Visio")
$dirlist = FileOpen("c:\temp\dirlist.txt", 0)

$iLineNumber = 0
While 1
    $dirline = FileReadLine($dirlist)
    If @error = -1 Then ExitLoop
    $iLineNumber += 1
    $iProgress = Int(($iLineNumber / $totalfoldersnum) * 100)
    ProgressSet($iProgress, "Folder list created.  Scanning folders for files...")
    $xlsfiles = _FileListToArray($dirline, "*.xls", 1)

    <snip>

    If $xlsnum + $docnum + $pptnum + $vsdnum > 0 Then
        FileWriteLine($output, $dirline & "," & $xlsnum & "," & $docnum & "," & $pptnum & "," & $vsdnum)
    EndIf
WEnd

ProgressSet(100, "Done", "Complete")
Sleep(5000)
ProgressOff()
MsgBox(0, "Script Complete", "Script has finished.  This dialog box will timeout after 5 minutes.", 300)

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The For/Next loop is not required, just do the math each time you update the progress:

<snip>

$totalfoldersnum = _FileCountLines("c:\temp\dirlist.txt")
$counter = 100 / $totalfoldersnum

If FileExists("c:\temp\output.csv") Then
    FileDelete("c:\temp\output.csv")
EndIf

$output = FileOpen("c:\temp\Output.csv", 2)
FileWriteLine($output, "Directory,Excel,Word,PowerPoint,Visio")
$dirlist = FileOpen("c:\temp\dirlist.txt", 0)

$iLineNumber = 0
While 1
    $dirline = FileReadLine($dirlist)
    If @error = -1 Then ExitLoop
    $iLineNumber += 1
    $iProgress = Int(($iLineNumber / $totalfoldersnum) * 100)
    ProgressSet($iProgress, "Folder list created.  Scanning folders for files...")
    $xlsfiles = _FileListToArray($dirline, "*.xls", 1)

    <snip>

    If $xlsnum + $docnum + $pptnum + $vsdnum > 0 Then
        FileWriteLine($output, $dirline & "," & $xlsnum & "," & $docnum & "," & $pptnum & "," & $vsdnum)
    EndIf
WEnd

ProgressSet(100, "Done", "Complete")
Sleep(5000)
ProgressOff()
MsgBox(0, "Script Complete", "Script has finished.  This dialog box will timeout after 5 minutes.", 300)

:D

Perfect Psalty - you the man. Works great.

I actually was able to get it unstuck by switching around where the for-next was, but this method works better. The progress bar times with the end the way it should instead of it 'sticking' at 100% for a period of time.

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

  • Recently Browsing   0 members

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