[SOLVED] Local variables in "loop" actions

When it comes to Local variables used in an action such as FOR EACH (but I guess this can probably be applied to other actions where there's a loop and it checks if a variable is true or false before moving on?), once the action is done and moves on to the next one, if I have another FOR EACH action, can I use the same variable?

For example let's say I have this:
image

If I then duplicate this action, if I change the destination directory to something else, can I keep the "Local__itemToRename" or do I have to change it to something unique because it's still pointing to the items in the old directory?

Never mind. I was able to find a way to test it and it indeed "resets" the variable once it's done with the "loop, so the name can be the same.

1 Like

I was uncertain of that same thing when I first started using that particular action, and figured out that it resets the variable by doing some testing as well. Kudos!

I think there is perhaps a little bit of a misunderstanding of what happens with the For Each action.

Essentially the For Each action would look like this:

  • Set variable Local__itemToRemove to "/first/file"
  • [actions inside the for loop]
  • Set variable Local__itemToRemove to "/second/file"
  • [actions inside the for loop]
  • Set variable Local__itemToRemove to "/last/file"
  • [actions inside the for loop]
  • [actions after the for loop]

An important note here is that the variable is set each time the loop starts, and the variable is not cleared at the end of the loop. So for example:

image

This will display:

  • Inside 1
  • Inside 2
  • Inside 3
  • Outside 3

Basically the For Each action simply sets the variable, runs the actions, sets the variable, runs the actions, and then carries on to the next action.

So yes, there is no issue in using the same variable on successive For Each actions, because regardless of what value it had on completing the first For Each, the second will simple set it to a new value when it starts executing the first time.

3 Likes