Jump to content

If..then..else...Do While problem


prgwkp
 Share

Recommended Posts

Can someone please tell me what is wrong with this loop? Once it hits the "Else" section and "EndIf", it loops back thru and it runs every command down the line, ignoring the IF statements. Should it not reset the IF statement once it loops back up to "Do"? If not, what is a better option? I need this to loop thru 200 hundred times and compare the arrays and follow the IF commands. .

Global $SourceLocationArray[200] 
Global $SecondaryArray[200]

$SecondaryArray[0] = "1.PDF"
$SecondaryArray[1] = "2.PDF"
$SecondaryArray[2] = "3.PDF"
$SecondaryArray[3] = "4.PDF"
$SecondaryArray[4] = "5.PDF"

$SourceLocationArray[0] = "1.PDF"
$SourceLocationArray[1] = "2.PDF"
$SourceLocationArray[2] = "3.PDF"
$SourceLocationArray[3] = "4.PDF"
$SourceLocationArray[4] = "5.PDF"
$SourceLocationArray[5] = "6.PDF"


$z = 0
$Loop = 0

Do

   If $SecondaryArray[$z] = $SourceLocationArray[$z] Then
       ;MsgBox(0,"Source and Secondary","Will be deleted from both Source and destination " & $SecondaryArray[$z])
       FileDelete("C:ArrayDestination" & $SecondaryArray[$z])
       FileDelete("C:ArraySource" & $SecondaryArray[$z])
       $z = $z + 1
   Else
      ;MsgBox(0,"Secondary Only","Will only be deleted from Secondary")
      FileDelete("C:DesktopArrayDestination" & $SecondaryArray[$z])
      $z = $z + 1
  EndIf

  $Loop = $Loop + 1

Until $Loop = 200
Edited by Melba23
Added code tags
Link to comment
Share on other sites

For me it looks like it does exactly what I would expect, check the console output.

Global $SourceLocationArray[200]
Global $SecondaryArray[200]

$SecondaryArray[0] = "1.PDF"
$SecondaryArray[1] = "2.PDF"
$SecondaryArray[2] = "3.PDF"
$SecondaryArray[3] = "4.PDF"
$SecondaryArray[4] = "5.PDF"

$SourceLocationArray[0] = "1.PDF"
$SourceLocationArray[1] = "2.PDF"
$SourceLocationArray[2] = "3.PDF"
$SourceLocationArray[3] = "4.PDF"
$SourceLocationArray[4] = "5.PDF"
$SourceLocationArray[5] = "6.PDF"

$z = 0
$Loop = 0

Do

    If $SecondaryArray[$z] = $SourceLocationArray[$z] Then
        ;MsgBox(0,"Source and Secondary","Will be deleted from both Source and destination " & $SecondaryArray[$z])
        ConsoleWrite($z & @tab & $Loop & @tab & "Will be deleted from both Source and destination " & $SecondaryArray[$z] & @crlf)
        ;FileDelete("C:ArrayDestination" & $SecondaryArray[$z])
        ;FileDelete("C:ArraySource" & $SecondaryArray[$z])
        $z = $z + 1
    Else
        ;MsgBox(0,"Secondary Only","Will only be deleted from Secondary")
        ConsoleWrite($z & @tab & $Loop & @tab & "Will only be deleted from Secondary" & @crlf)
        ;FileDelete("C:DesktopArrayDestination" & $SecondaryArray[$z])
        $z = $z + 1
    EndIf

    $Loop = $Loop + 1

Until $Loop = 200
Link to comment
Share on other sites

A much easier way of accomplishing what you want with only one variable is to use a For...Next loop instead of using $z and $Loop you can do it this way:

For $z = 0 To 199

    If $SecondaryArray[$z] = $SourceLocationArray[$z] Then
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $SourceLocationArray[$z] = ' & $SourceLocationArray[$z] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
        ;MsgBox(0,"Source and Secondary","Will be deleted from both Source and destination " & $SecondaryArray[$z])
;~      FileDelete("C:ArrayDestination" & $SecondaryArray[$z])
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $SecondaryArray[$z] = ' & $SecondaryArray[$z] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
;~      FileDelete("C:ArraySource" & $SecondaryArray[$z])
;~      $z = $z + 1
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') :         $z = ' &        $z & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    Else
        ;MsgBox(0,"Secondary Only","Will only be deleted from Secondary")
;~      FileDelete("C:DesktopArrayDestination" & $SecondaryArray[$z])
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $SecondaryArray[$z] = ' & $SecondaryArray[$z] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
;~      $z = $z + 1
    EndIf

;~  $Loop = $Loop + 1

;~ Until $Loop = 200
Next

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

After seeing the console out, I now see that once it the arrays are empty it then goes into the IF statement and the first statement is true at that point and it goes and deletes all contents of both folders. This is because the the array is empty to it deletes all in the directory because it does not specify a file. I though by adding the line of code ...

If $SecondaryArray[$z] Or $SourceLocationArray[$z] = "" Then
 MsgBox(0,"","Exit Loop")
 ExitLoop
   EndIf

would resolve this, but it did not.

Here is the full updated code, what can I do to say if there is a blank array variable in either array to exit loop?

Global $SourceLocationArray[200] 
Global $SecondaryArray[200]

$SecondaryArray[0] = "1.PDF"
$SecondaryArray[1] = "2.PDF"
$SecondaryArray[2] = "3.PDF"
$SecondaryArray[3] = "4.PDF"
$SecondaryArray[4] = "5.PDF"

$SourceLocationArray[0] = "1.PDF"
$SourceLocationArray[1] = "2.PDF"
$SourceLocationArray[2] = "3.PDF"
$SourceLocationArray[3] = "4.PDF"
$SourceLocationArray[4] = "5.PDF"
$SourceLocationArray[5] = "6.PDF"


$z = 0
$Loop = 0
Do

   If $SecondaryArray[$z] Or $SourceLocationArray[$z] = "" Then
      MsgBox(0,"","Exit Loop")
      ExitLoop
  EndIf


   If $SecondaryArray[$z] = $SourceLocationArray[$z] Then
       MsgBox(0,"Source and Secondary","Will be deleted from both Source and destination " & $SecondaryArray[$z])
       FileDelete("C:Documents and SettingsprgwkpDesktopArrayDestination" & $SecondaryArray[$z])
       FileDelete("C:Documents and SettingsprgwkpDesktopArraySource" & $SecondaryArray[$z])
       ;$z = $z + 1
  Else
       MsgBox(0,"Secondary Only","Will only be deleted from Secondary")
       FileDelete("C:Documents and SettingsprgwkpDesktopArrayDestination" & $SecondaryArray[$z])
       ;$z = $z + 1
   EndIf
   $z = $z + 1

   $Loop = $Loop + 1

Until $Loop = 200
Edited by Melba23
Added code tags
Link to comment
Share on other sites

  • Moderators

prgwkp,

If you post code, please use Code tags - put [autoit] before and [/autoit] after your posted code. I will amend the earlier posts in this thread - you do it from now on - deal? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Try adding this line just before your first If/Then statement in the loop in the code I gave you:

If $SecondaryArray[$z] = "" Or $SourceLocationArray[$z] = "" Then ExitLoop

It will exit the loop if either of the array elements are blank.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Maybe this is of use to you, thought this is what you're after :), check every element of array a against every element of array b.

Global $SourceLocationArray[200]
Global $SecondaryArray[200]

$SecondaryArray[0] = "1.PDF"
$SecondaryArray[1] = "2.PDF"
$SecondaryArray[2] = "3.PDF"
$SecondaryArray[3] = "4.PDF"
$SecondaryArray[4] = "5.PDF"

$SourceLocationArray[0] = "1.PDF"
$SourceLocationArray[1] = "2.PDF"
$SourceLocationArray[2] = "3.PDF"
$SourceLocationArray[3] = "4.PDF"
$SourceLocationArray[4] = "5.PDF"
$SourceLocationArray[5] = "6.PDF"

; Use a Buffer

Global $sBuffer
For $i = 0 To UBound($SourceLocationArray) - 1
    $sBuffer &= ";" & $SourceLocationArray[$i] & ";"
Next

For $i = 0 To UBound($SecondaryArray) - 1
    If $SecondaryArray[$i] Then
        If StringInStr($sBuffer, ";" & $SecondaryArray[$i] & ";") Then
            ConsoleWrite("Will be deleted from both Source and destination" & @TAB & $SecondaryArray[$i] & @CRLF)
        Else
            ConsoleWrite("Will only be deleted from Secondary" & @TAB & $SecondaryArray[$i] & @CRLF)
        EndIf
    EndIf
Next
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...