0

Good morning,

I'm currently trying to read multiple directories, instead of using the given statement several times I want to iterate through the folders in a for loop.

Key data:

  • current statement:

robocopy C:\Users\test\v2.5.0.0 C:\Users\enddir *.sql

  • now i saw the following code example for a batch for loop

for /l %%x in (1, 1, 100) do ( echo %%x MY ROBOCOPY STATEMENT )

  • The directories look somewhat like this
  • v2.5.0.0
  • v2.5.0.1
  • v2.5.0.3
  • v2.6.8.2
  • v2.7.4.3

My idea is, to have 4 capsuled for loops to iterate through all directories so I'm able to use variables example: vW.X.Y.Z instead of v2.5.0.0

So, the point of my question is: how can i set and use a variable in a for loop so that it has the value of the current iteration? I'm used to java if that helps in explaining.

Thanks in advance for your help. :)

if there is some information missing, I'm happy to give more!

edit: code i think will work

for /l %%w in (1, 1, 100) do (
   for /l %%x in (1, 1, 100) do (
    for /l %%y in (1, 1, 100) do (
        for /l %%z in (1, 1, 100) do (
            robocopy C:\Users\test\v%%w.%%x.%%y.%%z C:\Users\enddir *.sql
            )
        )
    )
)
Kyo Akashi
  • 213
  • 1
  • 2
  • 13
  • 2
    Do you really want counters? Can you not just loop over the folders with `for /D` e.g. `for /D %f in (c:\users\test\*) do @echo %f` ? also http://stackoverflow.com/questions/817280/how-does-for-work-in-cmd-batch-file and related links. – TessellatingHeckler Feb 10 '17 at 06:51
  • 1
    The value of the current iteration is stored in the parameter variable. So you could be using `echo v%%W.%%X.%%Y.%%Z` to achieve your result. If that is not what you meant, I suggest you show the skeleton of your script for better understanding :) – geisterfurz007 Feb 10 '17 at 06:52
  • @TessellatingHeckler sadly not because there are different directories too and i only want say directory 5.2 to 7.4 – Kyo Akashi Feb 10 '17 at 06:53
  • @geisterfurz007 edited to show my code, do you think this will work? – Kyo Akashi Feb 10 '17 at 06:59
  • 1
    I am certain it should. I will add something to my answer really quick – geisterfurz007 Feb 10 '17 at 07:04

1 Answers1

1

You can easily use a for-loop in batch with the /l switch to achieve a couting loop as shown in your example. These can be nested as well like this:

@echo off
for /l %%w in (1, 1, 10) do (
  for /l %%x in (1, 1, 10) do (
    for /l %%y in (1, 1, 10) do (
      for /l %%z in (1, 1, 10) do (
        echo currently at v%%w.%%x.%%y.%%z
      )
    )
  )
)

Now that above has been shown almost exactly in your question I suggest something to add:

The line if exist C:\Users\test\v%%w.%%x.%%y.%%z ( in front and a closing parenthesis in the line after should be added around your robocopy line, to make sure that the directory you want to copy actually exists.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • So it's just like i thought, thank you very much! Is the "@echo off" important? (and nice username ;P) – Kyo Akashi Feb 10 '17 at 07:05
  • 1
    @echo off prevents possibly annoying command-line output that is not really neccessary, it is not important, but I usually include it. And thanks ;) – geisterfurz007 Feb 10 '17 at 07:08