Jump to content
Sign in to follow this  
TylerH

My code that checks for a change in the number of files isn't working

Recommended Posts

TylerH

I have the following code that successfully reads the amount of files in a folder:

$hNumberofFiles = 0
Func _UBound($hNumberofFiles)
    Global $n
    Global $hPath = "T:\"
    $hNumberofFiles = _FileListToArray($hPath, "*.tif", 1)
    For $n=UBound($hNumberofFiles) - 1 to 0 Step -1
        If StringLen($hNumberofFiles[$n]) > 0 Then ExitLoop
        Next
    Return $n
EndFunc
$hFileCount = _UBound($hNumberofFiles)

(I tested it via a MsgBox returning _UBound($hNumberofFiles), which returned the correct number)

However, I can't seem to write a working loop to check for an increase in the number of files:

$hFileCountNew = $hFileCount
MouseClick("", 280, 420, 1, 10) ;Clicks a button to print files to a virtual printer AKA it writes files to a folder
Sleep(700)
While $hFileCountNew <= _UBound($hNumberofFiles) ;If the number of files is still the same, then the report is still loading, so just wait and check again in three seconds
    Sleep(3000)
WEnd

Specifically, the while loop doesn't seem to be working, so the script just sleeps forever/goes idle.

Edited by TylerH

Share this post


Link to post
Share on other sites
TylerH

Sorry all, I've solved my own problem by replacing the While Loop with a Do ... Until loop:

Do ;If the number of files is still the same, then the report is still loading, so just wait and check again in three seconds
    Sleep(3000)
Until _UBound($hNumberofFiles) > $hFileCount

 

Share this post


Link to post
Share on other sites
JLogan3o13

@TylerH another option, fewer lines:

#include <File.au3>
#include <MsgBoxConstants.au3>

While 1
    MsgBox($MB_OK, "", _findFileNum())
    Sleep(1000)
WEnd


Func _findFileNum()
    Local $sPath = @DesktopDir & "\Test"
    Local $aFiles = _FileListToArray($sPath, "*.tiff", $FLTA_FILES)
    Return UBound($aFiles) - 1
EndFunc

 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
TylerH
1 minute ago, JLogan3o13 said:

@TylerH another option, fewer lines:

#include <File.au3>
#include <MsgBoxConstants.au3>

While 1
    MsgBox($MB_OK, "", _findFileNum())
    Sleep(1000)
WEnd


Func _findFileNum()
    Local $sPath = @DesktopDir & "\Test"
    Local $aFiles = _FileListToArray($sPath, "*.tiff", $FLTA_FILES)
    Return UBound($aFiles) - 1
EndFunc

 

Wouldn't the While loop have to be after the Func? Otherwise it would be calling an undefined function, no?

Share this post


Link to post
Share on other sites
JLogan3o13

@TylerH, no AutoIt does not require your funcs to be above the calling point. In fact, as you look through examples in the help file and on the forum you will see most often they are put at the bottom, with your main body of code calling them.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
RyukShini
1 hour ago, JLogan3o13 said:

@TylerH, no AutoIt does not require your funcs to be above the calling point. In fact, as you look through examples in the help file and on the forum you will see most often they are put at the bottom, with your main body of code calling them.

I can vouch for this!
I wrote something once and all my functions were in the bottom of my script.

Share this post


Link to post
Share on other sites
TylerH
1 hour ago, JLogan3o13 said:

@TylerH another option, fewer lines:

#include <File.au3>
#include <MsgBoxConstants.au3>

While 1
    MsgBox($MB_OK, "", _findFileNum())
    Sleep(1000)
WEnd


Func _findFileNum()
    Local $sPath = @DesktopDir & "\Test"
    Local $aFiles = _FileListToArray($sPath, "*.tiff", $FLTA_FILES)
    Return UBound($aFiles) - 1
EndFunc

 

By the way, I don't need to create a MsgBox; I just need to store the number of files as a variable so that the rest of my code waits until the number of files != the variable stored at the beginning of the snippet.

Share this post


Link to post
Share on other sites
JLogan3o13

It was just an example, you can put the value into a var at the beginning, and then constantly check against it in the While loop

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

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  

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×