Jump to content

(Solved)Move files until count = 0


Recommended Posts

Can someone point out what I am doing wrong?   I have a directory that I want to move half the files to another directory.   My script does not stop when the counter is at zero

$hFileList = _FileListToArray("C:\PPATemp\SecondPage","*", $FLTA_FILES) 

$iCount = UBound($hFileList, $UBOUND_ROWS) -1
$fCount = $iCount /2

Do
   For $i = 1 To UBound($hFileList) -1
     FileMove("C:\PPATemp\SecondPage\"& $hFileList[$i], "C:\PPATemp\SecondPage\Temp\"& $hFileList[$i])
     $fCount = $fCount -1
   Next
Until $fCount = 0

Just ends up moving all of the files. And never exit the loop.   Thank you

Link to comment
Share on other sites

  • Developers

Quick lesson on debugging:

Add consolewrites in the code to show what it is doing ... something like:

#include <AutoItConstants.au3>
Global $hFileList[11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

$iCount = UBound($hFileList, $UBOUND_ROWS) - 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iCount = ' & $iCount & @CRLF)  ;### Debug Console
$fCount = $iCount / 2
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fCount = ' & $fCount & @CRLF)  ;### Debug Console

Do
    For $i = 1 To UBound($hFileList) - 1
        FileMove("C:\PPATemp\SecondPage\" & $hFileList[$i], "C:\PPATemp\SecondPage\Temp\" & $hFileList[$i])
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hFileList[$i] = ' & $hFileList[$i] & @CRLF) ;### Debug Console
        $fCount = $fCount - 1
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fCount = ' & $fCount & @CRLF) ;### Debug Console
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fCount = ' & $fCount & @CRLF)  ;### Debug Console
Until $fCount = 0

You will quickly see what you have done wrong. ;)

Jos

Spoiler

 

possible solution

#include <AutoItConstants.au3>
Global $hFileList[11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

$iCount = UBound($hFileList, $UBOUND_ROWS) - 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iCount = ' & $iCount & @CRLF)  ;### Debug Console
$fCount = $iCount / 2
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fCount = ' & $fCount & @CRLF)  ;### Debug Console

For $i = 1 To UBound($hFileList) - 1
    FileMove("C:\PPATemp\SecondPage\" & $hFileList[$i], "C:\PPATemp\SecondPage\Temp\" & $hFileList[$i])
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hFileList[$i] = ' & $hFileList[$i] & @CRLF) ;### Debug Console
    $fCount = $fCount - 1
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fCount = ' & $fCount & @CRLF) ;### Debug Console
    if $fCount = 0 then exitloop
Next

 

 

 

 

Edited by Jos

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

Depending on how big $hFileList is your inner loop could result in $fCount becoming a negative number while inside your inner loop, so the "Until $fCount=0" condition of your outer loop may never be satisfied.

If $fCount is being calculated the way you need it to be you could change the last line to:

Until $fCount <= 0

 

Link to comment
Share on other sites

The reason it moves everything is because the for > next loop doesn't know to exit when the desired $fCount value is reached.  @gruntydatsun Has a paint about the $fCount value going into the negative, but the real problem is the for loop.  One way you could address this is eliminate the Do loop, and put a check in the for loop to evaluate the value of $fCount and then ExitLoop.

Link to comment
Share on other sites

  • xcaliber13 changed the title to (Solved)Move files until count = 0

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...