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!

How to get your question answered on this forum!

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!

How to get your question answered on this forum!

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!

How to get your question answered on this forum!

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

    • Burgs
      By Burgs
      Greetings,
        I seem to be having a problem trying to insert values into an array in excess of the size of the array (its Ubound value).  I thought the command would simply 'ReDim' the array in order to add another value...however that does not seem to be happening.  My code is as below:
       
      ;**SET DYNAMIC ARRAY DIMENSIONS... $vValue = Int($_STRUCTURE_LEVEL - 1) ;seek the '$_HIERARCHY' level that is one 'previous' to the 'current' value...! $iStart = 0 ;set to begin search from element "0" in array... Do $_Files_Located = _Arraysearch($_HIERARCHY, $vValue, $iStart) if Int($_Files_Located) <> -1 Then $iEnd = 1 For $_RIGGING = 0 to Ubound($_LINE_DETAIL3) - 1 _ArrayInsert($_STRUCTURES, $_Files_Located + $iEnd, String($_LINE_DETAIL3[$_RIGGING])) _ArrayInsert($_HIERARCHY, $_Files_Located + $iEnd, Int($_STRUCTURE_LEVEL)) _ArrayInsert($_INFERIOR_TMPLS, $_Files_Located + $iEnd, Int(-1)) $iEnd += 1 ;increment EACH ITERATION... Next ;Next $_RIGGING EndIf ;'$_Files_Located' NOT "-1"...value for previous '$_STRUCTURE_LEVEL' ;was located in '$_HIERARCHY' array... $iStart += (Ubound($_LINE_DETAIL3) + 1) ;increment the offset index element position to begin the next search... ;"+ 1" to INCLUDE the 'parent' ('searched') UNIT...! Until $_Files_Located == -1 ;end loop when previous '$_STRUCTURE_LEVEL' is NOT found in '$_HIERARCHY' array... ;**  
        This code routine works perfectly fine except when the 'searched' value ($_Files_Located) happens to be the final element position in the searched array...how can I modify this routine so that the final additions at the end of the array(s) are made?  I thank in advance for any replies. 
       
    • DarkFingers1337
      By DarkFingers1337
      Hey,
      what is more efficient, using multiple arrays or one multidimensional array?
       
      ; Hamster data structure using multiple arrays Global $HamsterCount = 6 Global $HamsterX[$HamsterCount] Global $HamsterY[$HamsterCount] ;giving each hamster an x and y position For $i=0 To $HamsterCount-1 $HamsterX[$i] = 0 $HamsterY[$i] = 0 Next ; The same hamster data structure using one multidimensional array Global $HAMSTER_COUNT = 6 Global Enum $HAMSTER_X, _ $HAMSTER_Y, _ $HAMSTER_MAX Global $Hamsters[$HAMSTER_COUNT][$HAMSTER_MAX] ;giving each hamster an x and y position For $i=0 To $HAMSTER_COUNT-1 $Hamsters[$i][$HAMSTER_X] = 0 $Hamsters[$i][$HAMSTER_Y] = 0 Next  
    • XinYoung
      By XinYoung
      HI! ... this is a big one (at least for me) 
      You guys previously helped me copy the used range in column A and paste them into a Website one at a time in a loop. Cool! Now, for another function, I have 2 columns, A and B, and two input boxes in the Website. I'm having a hard time replicating the loop for the 2 columns. 
      This is how I'm opening the Excel workbook (copied from the previous function that only had 1 column). I need to also get the used range in column B.
      Func OpenExcelForCopy() Global $aBBTableData Global $oExcel = _Excel_Open() Global $oWorkbook = _Excel_BookOpen($oExcel, $ChosenFileName, Default, True, True) $oExcel.Sheets("CopyCourses").Activate ;~ Get all used cells in column A:A Global $aSearchItems = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.Sheets("CopyCourses").Usedrange.Columns("A:A")) ;~ Duplicate the $aSearchItems Array Global $aSearchResult = $aSearchItems ;~ Loop through the array starting at 0 until the end of the array which is (Ubound($aSearchItems) - 1) For $i = 0 To UBound($aSearchItems) - 1 $aSearchResult[$i] = SearchCourseForCopy($aSearchItems[$i]) Next _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") Finished() EndFunc ;==>OpenExcelForCopy Then we eventually get here. I don't think anything needs to change here but I'm not sure. This is where I paste the data from Column A into an input field (which is a search tool in a website). If the search is good, then we get to the tricky part...
      ;~ OK, we logged in and we searched for a course. Lets COPY it! Func CopyCourseBegin() Local $sResult $iSearchIndex = _ArraySearch($aBBTableData, "Course ID", 0, 0, 0, 1, 1, 0) ;~ If the course was not found, do this. If $iSearchIndex = -1 Then ;~ MsgBox(4096, "Search Error", "Item not found") $sResult = "Source Not Found" _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") ;~ Now go back to the Excel sheet and search for the next one. ;~ If the course was found, begin the COPY process. Else For $i = 0 To UBound($aSearchItems) - 1 $aSearchResult[$i] = CopyCourseNow($aSearchItems[$i]) Next $sResult = "Copied" _Excel_RangeWrite($oWorkbook, Default, $aSearchResult, "C1") EndIf Return $sResult EndFunc ;==>CopyCourseBegin This is the "tricky part" where I'm confused. I can copy and paste what's in column A just fine, but I can't manage to replicate it for column B. I need to paste whats in Column B into "destinationCourseId"
      ;~ The course search was successful. COPY the course now. Func CopyCourseNow($_sSearchResult) ;~ Navigate to the course copy page. _IENavigate($oIE, $urlBBCourseCopy) ;~ Copy the SOURCE course ID from the Excel sheet ;~ Paste whats copied from column A into the Source Course ID text box Local $oForm = _IEGetObjByName($oIE, "selectCourse") Local $oSearchString = _IEFormElementGetObjByName($oForm, "sourceCourseId") _IEFormElementSetValue($oSearchString, $_sSearchResult) ;~ Paste whats copied from column B into the Destination Course ID text box ?!?!?!?! Local $oForm = _IEGetObjByName($oIE, "selectCourse") Local $oSearchString = _IEFormElementGetObjByName($oForm, "destinationCourseId") _IEFormElementSetValue($oSearchString, $_sSearchResult) ;~ Just exit cause im stuck :( _Exit() EndFunc ;==>CopyCourseNow After I paste the data from column A into "sourceCourseId" and column B into "destinationCourseId", I'll make it do some stuff. Then I need it to loop around until the used ranges in column A & B is finished.
      Does the entire code need to change now that there's two columns?
       
       
    • Skysnake
      By Skysnake
      I know how JSON works. However JSON data is typically received from web based servers. 
       
      My question is:
      Has anyone considered to use, or currently actively using JSON as an internal data container? 
      Internal here as in an ordinary AutoIt data type, generated, populated and manipulated within an AutoIt script, without any external data sources. 
      The JSON and BinaryCall UDFs work well and appear the de facto defaults. Are there any thoughts on using JSON for internal variable values instead of the typical array? 
      Also, there can be situations where such a JSON collection contains an array and it will have to be parsed using typical array functions. 
      Currently I am working on a small project where I have to keep and use various identifiers from different sources. There are several ideas in this regard, some involving SQL child tables and arrays stored with keys (maps), which lead me to think that JSON may be a more suitable data container. It readily shrinks and stretches, allowing for dynamic data growth.  JSONs ability to add new repeating data groups on the fly makes it particularly powerful and appealing. 
      Ideas and suggestions welcome
      Skysnake
    • XinYoung
      By XinYoung
      Why hello there! 
      I have a script reading an Excel sheet (currently only looking at A1), copying the data in that cell, and then searching for it in a Website's search tool. Then it does some stuff if it actually finds something.
      After that, I need it to go back to the Excel sheet, write "yes" into column B if the search was good -- "no" if no results were found, then go down to A2 and do the whole thing all over again until column A has nothing left.
      I really suck at loops and have a hard time grasping how it works. Any help or guidance is really appreciated!
       
      SearchCourse() ;~ Now that were logged in, navigate to the course search page. Func SearchCourse() _IENavigate($oIE, $urlBBCourseSearch) ;~ Change the search criteria to "Course ID" _bbCourseSearchCategoryChange("Course ID") ;~ Open the selected Excel file Local $oAppl = _Excel_Open() Local $oWorkbook = _Excel_BookOpen($oAppl, $ChosenFileName, Default, Default, True) ;~ Copy whats in the first cell (A1) _Excel_RangeCopyPaste($oWorkbook.Worksheets(1), "A1") Global $WhatsCopied = ClipGet() ;~ Paste whats copied into the search text box and click submit Local $oForm = _IEGetObjByName($oIE, "courseManagerFormSearch") Local $oSearchString = _IEFormElementGetObjByName($oForm, "courseInfoSearchText") _IEFormElementSetValue($oSearchString, $WhatsCopied) _IEFormSubmit($oForm) ;~ Lets see what we got from the search Local $oBBTable = _IETableGetCollection($oIE, 2) Global $aBBTableData = _IETableWriteToArray($oBBTable) _ArrayDisplay($aBBTableData) DeleteCourseBegin() EndFunc ;==>SearchCourse ;~ OK, we logged in and we searched for a course. Lets delete it! Func DeleteCourseBegin() $iSearchIndex = _ArraySearch($aBBTableData, "Course ID", 0, 0, 0, 1, 1, 0) ;~ If the course was not found, do this. If $iSearchIndex = -1 Then MsgBox(4096, "Search Error", "Item not found") ;~ Now go back to the Excel sheet and search for the next one....? ;~ If the course was found, begin the deletion process. Else MsgBox(4096, "Search Success", $aBBTableData[$iSearchIndex][0] & " = " & $aBBTableData[$iSearchIndex][1]) DeleteCourseNow() EndIf EndFunc ;==>DeleteCourseBegin ;~ The course search was successful. Delete the course now. Func DeleteCourseNow() ;~ Click the checkbox located beside the course that has been found. Local $CheckBox = _IEGetObjByName($oIE, "ckbox") _IEAction($CheckBox, "click") _IELoadWait($oIE) ;~ Click the Delete button. _IELinkGetCollection($oIE) Local $oLinks = _IELinkGetCollection($oIE) For $oLink In $oLinks If $oLink.href = "javascript:validateRemove();" Then _IEAction($oLink, "click") ExitLoop EndIf Next ;~ Click the "Delete course, including all of its files" radio button. Local $RadioButton = _IEGetObjById($oIE, "removeAllFiles_t") _IEAction($RadioButton, "click") _IELoadWait($oIE) ;~ Click the "Submit" button Local $Submit = _IEGetObjByName($oIE, "bottom_Submit") _IEAction($Submit, "click") _IELoadWait($oIE) ;~ Now go back to the Excel sheet and search for the next one....? EndFunc ;==>DeleteCourseNow  
       
×