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%")
9
  • 4
    why can you not use delayedexpansion? Commented May 20, 2018 at 10:30
  • @GerhardBarnard This is just a learning exercise and I'm not trying to employ it yet here! Commented May 20, 2018 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 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. Commented May 20, 2018 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. Commented May 21, 2018 at 0: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. Commented May 21, 2018 at 0:52

1 Answer 1

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%
)
Sign up to request clarification or add additional context in comments.

6 Comments

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...
@r4mulus, replace call with set in the for loop and try it again.
@r4mulus, another issue noticed just below the label :startloop. Try set "interim=%%numbers[%i%]%%" as call set "interim=%%numbers[%i%]%%".
@michael_heath damn- that did it- and i had already given up on it as impossible! Huge thanks!!!
Thanks for pointing out the errors, @michael_heath, I just fixed them...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.