Looping through string values from a windows command line bat file Looping through string values from a windows command line bat file windows windows

Looping through string values from a windows command line bat file


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


@Јοеу's answer works great,

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

@echo offfor %%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.cmdyour 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 (....


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.11.22.43.93.9.13.9.23.913.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