0

Batch script does not accept an empty space (not blank) as an array element. So, how to add an element containing only a space to an array in batch file. Actually I want to print that space element with other elements as shown in the loops. I appreciate your help in advance.

echo off
cls
::set arr=(a b c d 'Space')
setlocal vari=' '
set arr=(a,b,c,d, %vari%)

for %%1 in %arr% do (
  echo %%1

    for %%2 in %arr% do (
      echo %%1%%2

        for %%3 in %arr% do (
          echo %%1%%2%%3

            for %%4 in %arr% do (
              echo %%1%%2%%3%%4
            )
        )
    )
)
asif
  • 177
  • 1
  • 14
  • Use `set`, not `setlocal`. And use `"` instead of `'`. – SomethingDark Jan 02 '16 at 20:30
  • ok, but 'space' is not printed even using "" or '' – asif Jan 02 '16 at 20:46
  • 1
    This: `set arr=(a,b,c,d, " ")` is not an array, but a _list_ (or a FOR command _set_). For a description of what an array in Batch is, see [this post](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). I suggest you to fix the question title and description... – Aacini Jan 03 '16 at 00:32
  • A _SPACE_ is a delimiter in `cmd`/batch, like also _TAB_, `,`, `;`, `=`; if you need to define a string that contains any of these characters within a `for` set (that is, the items that are to be enumerated), you need to put quotation marks `""` around it; for instance: `for %%I in (string, "some text") do (echo.%%~I)`; this will return two strings `string` and `some text`; if you remove the `""`, it will return three strings `string`, `some` and `text`; the `~` is explained well in the comments of the [answer below](http://stackoverflow.com/a/34570586/5047996); type also `for /?`... – aschipfl Jan 03 '16 at 19:03

1 Answers1

0
echo off
cls
::set arr=(a b c d 'Space')
set vari=" "
set arr=(a,b,c,d, %vari%)

for %%1 in %arr% do (
  echo(%%~1

    for %%2 in %arr% do (
      echo(%%~1%%~2

        for %%3 in %arr% do (
          echo(%%~1%%~2%%~3

            for %%4 in %arr% do (
              echo(%%~1%%~2%%~3%%~4
            )
        )
    )
)

basically

use set, not setlocal. And use " instead of '. – SomethingDark 18 mins ago 

plus %%~x to remove the qoutes in Output.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • please tell how to use %%~x and where? – asif Jan 02 '16 at 21:12
  • @asif - not a literal `x`, just the variable you're using. `~` is used to remove outer quotes from the value. – SomethingDark Jan 02 '16 at 21:19
  • If `%%x` is a `"quoted string"` then `%%~x` removes the quotes (otherwise it leaves the string alone). Not a good idea to use `%%1..%%4` IMHO - it can lead to unexpected results as `%1`..`%9` are the procedure's parameters. Note also there's no need to use `vari` - `" "` would do just as well. – Magoo Jan 02 '16 at 21:21
  • ok thanks, working well, but please tell is there any other meaning of the ~ sign here? And can you please check is it skipping some outputs e.g. "ab d" – asif Jan 02 '16 at 21:34
  • 2
    `~` and other possibilities are listed with `for /?`. For me, `ab d` _is_ listed. And Magoo is right, using numbers as `for` variables is not the best of all ideas - although it seems to work in most cases. Better use letters (`%%A`, `%%B`,...). Only problem are lines with spaces only. `echo` is not able to handle that. Therefore `echo(` is used to prevent this. – Stephan Jan 02 '16 at 22:02
  • If you `rem`-out all of the `echo`s except the last one (generating the 4-character output) and then pipe the output like so : *thisbatch*|findstr /n /r "." then you'll get a count of 256 output lines - correct since 4x4x4x4 = 256. If you check the output using a tool to detect duplicate lines, you'll find there are no duplicates. 256 output lines with no duplicates, 4 characters must be the full set of combinations. A complicating factor is the presence with the full code of short lines like "a" or "a b" which are different from "a " or "a b ". Replace the space with `d` for a better view – Magoo Jan 03 '16 at 07:33