Sign in to follow this  
Followers 0
Chimaera

Loops Help Plz

5 posts in this topic

Ok been reading up and researching but i still dont get this bit.

For e.g.

No1

For $i = 5 to 1 Step -1
    MsgBox(0, "Count down!", $i)
Next 
MsgBox(0,"", "Blast Off!")

The bit i have the problem is the $i

Where is the value taken from?

Is $i a preset value or something?, or can it be named to anything for e.g. $iii

No2

Dim $aArray[4] ;<<<< im assuming this has to be the exact number of the items in the array?

$aArray[0]="a"
$aArray[1]=0
$aArray[2]=1.3434
$aArray[3]="test"

$string = ""                      ;<<<<< what is this bit for? 
FOR $element IN $aArray            ;<<<<< where is $element obtained from?
    $string = $string & $element & @CRLF     ;<<<<<< dosent this replace the one 2 lines earlier?
NEXT

Msgbox(0,"For..IN Arraytest","Result is: " & @CRLF & $string)

Check the green comments for the bits i dont get

And Finally

With cmd items like Robocopy which i will need to use in one of my proposed scripts what is the correct way to call it from autoit?

This is how i use Robocopy

SET _back_what=*.m3u *.$01 *.$ri *.$xe *.??? *._00 *.32 *.404 *.669 ;<<<<<< plus lots of files like this which i assume i will need to put in an array of some kind?

         SET _exc_files="Kalimba.mp3" "Maid with the Flaxen Hair.mp3" ;<<<<< etc etc

         SET _exc_folder="Program Files (x86)" "Program Files" "Windows" ;<<<<<< etc etc

         SET _exc_software="Logitech" "Adobe" "DAEMON Tools Lite" ; <<< etc etc

         robocopy "%source%." "%target%." %_back_what% /R:0 /W:0 /XJD /XJF /NJH /NFL /NDL /s /xx /nc /l /XF %_exc_files% /XD %_exc_folder% %_exc_software% "%target%." /LOG:"%target%."\_working.txt >nul

Sorry for asking some dumb questions but i just dont get the way $i works

Thanks for taking the time to help

Chimaera

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

No 1.

You can name this variable anything you want.

For $OMG = 5 to 1 Step -1
    MsgBox(0, "Count down!", $OMG)
Next 
MsgBox(0,"", "Blast Off!")

The use of '$i' is just naming convention. The variable is created upon entering the loop. In other words, you create the variable. Most of the time you would want to use this value to access a certain element in an existing array.

For $i = 5 to 1 Step -1
    Local $s_item = $items[$i]; This will copy item number $i to the local variable $s_item.
Next

No 2.

Dim $aArray[4]; Yes, this is the exact number of items in the array.

$string = ""; This will initialize the variable as an empty string.

The following array will append values to this variable ($string). I am not sure if it is required to initialize it like this, but it improves readability and certainly does not harm.

; This loop will iterate through every item in an array, and the $element 
; variable will always be the current item where the loop is. You could do 
; the same thing using For $i = 0 To 5 but then you would not have $element, 
; but rather $aArray[$i].
For $element In $aArray
; This part will append string values to an existing string array. Let's 
; say the $string value is empty (""). In the first loop iteration you add 
; "LOL" and @CRLF (which is a line break), then in the next iteration you 
; add "OMG". Now your string looks like: 
; LOL
; OMG
    $string = $string & $element & @CRLF
Next; This loop iteration is finished and we will go back to 'For' and start over again if there is still unprocessed items in the array.

I'm not sure if I understood the last question. You want to know how you can call Windows Command Prompt commands in AutoIt? If that's the case, check out this post. You should be able to call robocopy using one of those approaches.

I hope this made things a little more clear ;)

Edited by Calistoga

Share this post


Link to post
Share on other sites

Chimaera,

You want your money's worth with that little lot! :shocked:

No 1. When you use a For...Next loop, AutoIt needs a variable to hold the current value of the loop as it runs. In the case you posted, you have told it to use $i, but you can call the variable anything you want - the following code is exactly the same as far as AutoIt is concerned (as long as $iCapeCanaveral is not being used somewhere else in the script at the same time!) ;) :

For $iCapeCanaveral = 5 to 1 Step -1
    MsgBox(0, "Count down!", $iCapeCanaveral)
Next 
MsgBox(0,"", "Blast Off!")

No 2.

a ). Arrays must be big enough to hold at least the number of items they must hold. So if you have 4 items, you need [4] as minimum size, although there is nothing to stop you using [4004] or any other number greater 4 if you do not mind wasting all that memory. :)

A couple of other points:

- Try to avoid Dim - use an explicit Global/Local declaration instead.

- You can fill your array as you declare it like this:

Global $aArray[4] = ["a", 0, 1.3434, "test"]

Obviously this is only sensible for small arrays, but it does save wear and tear on the typing finger(s).

b ). $element is rather like $i in the example above. AutoIt needs something to hold the values within the For...In...Next loop as it progresses. The For line just tells AutoIt what this variable is called - again you can use any name you want as you can see below.

c ). If you are adding or concatenating inside of a loop you need something to add/concatenate to - so you need to declare a variable with an initial value. In this case you are concatenating with the & operator, so you need to create an empty string beforehand so that the loop know what it is adding to.

Another point:

- You can use combination operators as shorthand (I like typing as little as possible as you may have noticed!). This is the equivalent of your code:

$string = ""                                   ; Declare the initial empty string
FOR $WhatEverIWantAsAName IN $aArray           ; Use any name you want here
    $string &= $WhatEverIWantAsAName & @CRLF   ; Use the combination operator to concatenate to $string 
NEXT

There are similar combination operators for the mathematical operations - look in the Help file under <Language Reference - Operators>

No 3. There are lots of Robocopy scripts out there - I have not used it so I cannot help too much. However, some general advice:

- You will obviously have to use variables in place of the environment strings.

- Be very careful with your quotes - remember that you can use single quotes to include double quotes within a string.

Please ask if anything is not clear or if there are any more questions concerning the first 2 sections. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

You are assigning the value to $i, with each loop the value changes (in this example, from 5 to 1 counting down by 1). "$i" is just commonly used, but you can name the value anything you want as long as it is a proper value name, i.e. $Count or $Timer or $z

The array value can be exact or a "starting" declaration if you plan on changing its value with ReDim. Sometimes in a script you may not know exactly what size the array needs to be (reading strings or counting files in a directory, for example), but it is usually a good practice to start with more elements than you need and then trim down.

The $string = "" just declares that $string is a variable (blank, but still a variable)...since it has no value, you could do the same thing with:

Dim $String or Local $string or Global $string

$element is the variable name used in the For loop and since you used "In" the loop just goes through every element in the array. You could just as easily use $i or another variable name:

For $i In $aArray
This line adds the current value to the $string variable (with a carriage return). This is called concatenation. Actually, it could be written as
$string &= $element & @CRLF
The one thing to remember that in order to use concatenation, the variable must already be declared. The result of all the loops running for $string is:

a

0

1.3434

test

You must declare your variable correctly ($Variable)...I am not sure of the exact syntax for Robocopy, but here is what I think it is...note the spaces that I placed between variables so the command is correct

$_back_what = "*.m3u *.$01 *.$ri *.$xe *.??? *._00 *.32 *.404 *.669"

$_exc_files = '"Kalimba.mp3" "Maid with the Flaxen Hair.mp3"'

$_exc_folder = '"Program Files (x86)" "Program Files" "Windows"' ;<<<<<< etc etc

$_exc_software ='"Logitech" "Adobe" "DAEMON Tools Lite"' ; <<< etc etc

$Source = ""; your coode for source

$Target = "";your code for target

Run("robocopy " & $Source & " " & $Target & " %_back_what% /R:0 /W:0 /XJD /XJF /NJH /NFL /NDL /s /xx /nc /l /XF " & $_exc_files & " /XD " & $_exc_folder & " " &  $_exc_software & " " &  $Target & "  /LOG:" & $Target & "\_working.txt >nul")

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  
Followers 0