1

The overall objective is that I want to write a batch script which I can pass two arguments- an AD group, and the location of a text file containing a list of users. The finished data will then be passed to an API in another system to import for managing other groups, like mailing lists, etc.

The difficulty is: I want to compare an array value to a numeric variable within a batch script, without using delayed expansion, preferably.

This will allow me to create a "bubble compare" where I can check each new entry against values already in the existing file and then either append the entry to the master list, or do nothing with it if it already exists.

I am trying to do this without using delayed expansion. It has occurred to me that perhaps arrays can not be utilized in this fashion, without delayed expansion. Is it possible to compare incrementing array values without utilizing delayed expansion?

FOR /L %%a IN (0,1,9) DO (
CALL SET numbers[%%a%%]=%%a
)
SET /A i=0
:startLoop
IF "%numbers[%i%]%" EQU "%i%" DO (
ECHO "%i% exists!"
set /A i=%i%+1
GOTO startLoop
)
ELSE ( ECHO "The list has finished at %i%")
  • 4
    why can you not use `delayedexpansion`? – Gerhard May 20 '18 at 10:30
  • @GerhardBarnard This is just a learning exercise and I'm not trying to employ it yet here! – mountainsIT May 20 '18 at 17:47
  • @r4mulus, because this is just a learning exercise you must know, what you're trying to do. Unfortunately you only told us that you want to compare an array value to a number and provided some code. You haven't explained the task you've set yourself nor what the code does or doesn't do in relation to that task. You've obviously got an issue with your script, but unless you [edit your question](https://stackoverflow.com/posts/50433322/edit) to explain what you specifically need us to help you with, your question is off topic and we will be unable to help you with it. – Compo May 20 '18 at 20:39
  • @Compo I didn't realize questions about the mechanics of the language were out of the scope of stack overflow- my apologies! Will edit question now. – mountainsIT May 21 '18 at 00:21
  • @r4mulus, your posted code does not seem to have any relationship to the now edited question. Your question is too broad because you seem to be asking for help with everything. You need to break your task into small pieces and work through each before trying to put them all together. For instance, you talk about an array, have you successfully created an array? is how to do that your question? You talk about your script accepting two parameters, where in the code does it do that? Is how to do that your question? You also mention, numeric variables, but I only see strings; please clarify. – Compo May 21 '18 at 00:52
  • @Compo The main reason that is, is because I haven't written that portion yet- I have the overall objective of what I want in my mind, but I am trying to understand how to make a simple comparison from an array before I start blindly writing code. Look I'm really sorry if you consider this is a waste of your time or out of scope. I researched this with all my resources for 3-4 hours yesterday and could not figure it out. I feel like this language is incredibly poorly documented online. I'm just looking for help with this concept and I feel confident I can do the rest, one step at a time. – mountainsIT May 21 '18 at 02:05
  • @r4mulus, if my time was that precious, I wouldn't waste it trying to leverage information from you. This site is for help with specific code issues, you've provided some code but haven't told us what the issue is with it. What it outputs, what it is supposed to output, and what you've tried to do yourself to rectify those things both before and since posting it. Questions which require extensive use of the comment area are more often than not the result of a poor question, I'm simply trying to coax it's improvement and subsequent chances of solutions. – Compo May 21 '18 at 02:14
  • @GerhardBarnard according to [this](https://stackoverflow.com/a/10167990/5626988) answer, it's impossible to do without delayedexpansion . I do appreciate putting it in my mind that it might be impossible to do without it. Thank you. – mountainsIT May 21 '18 at 02:23
  • @Compo pretty sure its impossible to do without delayedexpansion. The answer [here](https://stackoverflow.com/a/10167990/5626988) seems to makes sense as to why this is. I appreciate your help. I'll try to post more relevant material here in the future. – mountainsIT May 21 '18 at 02:26

1 Answers1

2

There are some issues in your code:

  • call set numbers[%%a%%]=%%a should read set "numbers[%%a]=%%a" (no call necessary here);
  • there is no do in if clauses;
  • if "%numbers[%i%]%" equ "%i%" tries to expand (non-existent) variables numbers[ and ]; call if "%%numbers[%i%]%%" equ "%i%" does not work as call cannot be used with if; but you could do call set "interim=%%numbers[%i%]%%" and then if "%interim%" equ "%i%";
  • set /A i=%i%+1 should read set /A "i+=1";
  • ) else ( must be in a single line;

All in all this means:

for /L %%a in (0,1,9) do (
    set "numbers[%%a]=%%a"
)
set /A "i=0"
:startLoop
call set "interim=%%numbers[%i%]%%"
if "%interim%" equ "%i%" (
    echo %i% exists
    set /A "i+=1"
    goto :startLoop
) else (
    echo The list has finished at %i%
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I appreciate the help, @aschipfl ! The comparison (relational) appears to not be working though as I just get dumped out to the else statement each time. The `set "interim=%%numbers[%i%]%%"` statement is flagged as problematic in my editor... – mountainsIT May 20 '18 at 19:19
  • 1
    @r4mulus, replace `call` with `set` in the `for` loop and try it again. – michael_heath May 21 '18 at 01:55
  • thanks for the suggestion @michael_heath! unfortunately I saw that small mistake earlier and made that change and it doesn't affect the outcome of my program. Pretty sure I've found the answer to this though, and it is: it is impossible to access the values in the array without using delayed expansion. Many thanks to @Aacini for their answer [here](https://stackoverflow.com/a/10167990/5626988)! – mountainsIT May 21 '18 at 02:21
  • 1
    @r4mulus, another issue noticed just below the label `:startloop`. Try `set "interim=%%numbers[%i%]%%"` as `call set "interim=%%numbers[%i%]%%"`. – michael_heath May 21 '18 at 02:44
  • 1
    @michael_heath damn- that did it- and i had already given up on it as impossible! Huge thanks!!! – mountainsIT May 21 '18 at 02:53
  • 1
    Thanks for pointing out the errors, @michael_heath, I just fixed them... – aschipfl May 21 '18 at 11:36