48

I am trying to create a batch script for my Windows machine that loops through a list of (string/decimal) values and uses each value as a parameter inside the loop.

Below is an example of a simple for loop I would like to use to display all the different version files (from my list)

FOR ? in ('1.1','1.2','2.4','3.9') do echo V[value_from_for_loop].txt

I am having trouble in how to loop through each item and use a variable in my echo statement.

rob5408
  • 2,886
  • 2
  • 37
  • 50
David
  • 14,585
  • 15
  • 56
  • 80

5 Answers5

70
for %x in (1.1 1.2 2.4 3.9) do echo V%x.txt

For use in a batch file you'll have to double the %:

for %%x in (1.1 1.2 2.4 3.9) do echo V%%x.txt
Joey
  • 316,376
  • 76
  • 642
  • 652
  • What if the strings happen to include `?` or `*`? Is there a way to prevent the command-interpreter from interpreting them as filenames? – Synetech Feb 04 '13 at 18:14
  • I'm using Git for Windows "bash" shell (MINGW32) and I get `bash": syntax error near unexpected token `('`. – Dave Heq Jun 16 '17 at 15:20
  • 2
    @DaveHeq: Don't use bash and try to execute cmd commands, then. – Joey Jun 16 '17 at 15:21
36

@Јοеу's answer works great,

here is how I've used it, to 'walk' a pre-set list of files in a specific order.

@echo off
for %%x in (
        a.js
        storage.js
        logic.js
        main.js
        z.js
       ) do (
         echo your file name is %%x
         echo "%%x" is a cool name
         echo.
         echo =-=-=-=-=-=
         echo.
       )

the reason it looks like a vertical list is so it will be easier to add or remove more items. (and 'echo' with 'dot' is for one empty line).

the output will look like this:

C:\example>yourBatchName.cmd
your file name is a.js
"a.js" is a cool name

=-=-=-=-=-=

your file name is storage.js
"storage.js" is a cool name

=-=-=-=-=-=

your file name is logic.js
"logic.js" is a cool name

=-=-=-=-=-=

your file name is main.js
"main.js" is a cool name

=-=-=-=-=-=

your file name is z.js
"z.js" is a cool name

=-=-=-=-=-=

** p.s. for file name listing one should prefer using something like this:

for %%e in (*.dll) do (....

15

Assume you have a very long list of values which will be very uncomfortable to type on the commandline. Also, there is a length limit for the DOS command line.

In this case the values may be stored in an arbitrarily long file, one per line. Call it my-values.list, with a content similar to:

1.1
1.2
2.4
3.9
3.9.1
3.9.2
3.91
3.91.1
...

Now you could read the variables from this text file, line by line:

for /f "tokens=*" %a in (c:\path\to\my-values.list) do echo.  Version%~nxa.txt
Kurt Pfeifle
  • 78,224
  • 20
  • 220
  • 319
  • 4
    @David Liddle: Of course it has -- you just don't understand (yet) how :-) -- Assume a very long text file `values.list`, where you have all the values listed, 1 per line. Would be very un-handy to type all these on the commandline again (which itself has length limits). I had to do exactly that thing with ~2.500 values a couple of weeks ago. And the list I had created thru another batch. I just didn't need to simply+boringly `do echo ...` but more interesting stuff instead... ;-) – Kurt Pfeifle Aug 09 '10 at 23:13
  • 1
    What is the function of ~nx in %~nxa? – matty Jan 18 '21 at 21:37
  • 1
    @matty: See https://stackoverflow.com/a/3679781/359307 – Kurt Pfeifle Jan 19 '21 at 12:28
2

Something like this, I believe:

for %x in (1.1 1.2 2.4 3.9) do (
    echo V%x.txt
)
mwpowellhtx
  • 353
  • 1
  • 9
1

It won't let me comment, but I wanted to add my 2 cents worth here. The ' do ' has to be on the same line as the right parenthesis of the 'for' command. In other words, this will work:

for %x in (1.1 1.2 2.4 3.9) do echo V%x.txt

...but this will not:

for %x in (1.1 1.2 2.4 3.9)
    do echo V%x.txt
סטנלי גרונן
  • 2,740
  • 21
  • 43
  • 62
  • 1
    You can put a ( before the echo and put a 0 after the txt which will allow you to put echo on it's own line. `for %x in (1.1 1.2 2.4 3.9) do ( echo V%x.txt )` – HPWD Dec 09 '20 at 20:41