0

Having problems trying to create a User Manager type program for windows. At this point, I'm not sure if it's a simple or impossible to fix because all of my attempts to find a fix were useless. (Most likely because of horrible knowledge of programming terms.)

I'm trying to combine some strings and the "for" loop counter into one string and then use the resulting string to call an already stored variable that has the name defined in the final string.

The counter should call specific users already defined in an "array" or array substitute from this tutorial: https://www.youtube.com/watch?v=l0ib2kCaVuA&list=PL69BE3BF7D0BB69C4&index=64

How can I make the string instantUser act like the variable allowed_users[0]?

@echo off
setlocal enabledelayedexpansion

title CP Script

setlocal
:Setup
echo What do you want to do? [#]
echo 1. Fix Users + Groups
echo 2. Configure Firewall + Updates
echo 3. Fix Remote Connection
echo 4. Find Illegal Files
echo 5. Configure Audits
echo 6. Fix Minor - 

set COMMAND=
set /p COMMAND=Type input: %=%

If %COMMAND%==1 goto Account
If %COMMAND%==2 goto Basic
If %COMMAND%==3 goto Remote
If %COMMAND%==4 goto Files
If %COMMAND%==5 goto Audits
If %COMMAND%==6 goto Minor
echo Incorrect input & goto Setup

:Account
    cls
    echo File path to user list-
    set DIRECTORY=
    set /P DIRECTORY=Type input: %=%
    set /p Build=<"%DIRECTORY%"
    cls

    call create_array allowed_users "," "%Build%"

    :: PROBLEM WAS HERE
    set /a "allowed_users_length_main=allowed_users_length-1"
    For /L %%b In (0,1,%allowed_users_length_main%) Do (
        Net User "!allowed_users[%%b]!" /Add
    )
    :: PROBLEM ENDED HERE

    echo.
    goto :Setup

:Basic
:Remote
:Files
:Audits
:Minor
endlocal

goto :eof
aschipfl
  • 33,626
  • 12
  • 54
  • 99
makrasnov100
  • 39
  • 1
  • 7
  • You are close! Delete the `%` around `instandUser`. However with that loop you will never have an element [0] as you always add 1 ;) – geisterfurz007 Jan 09 '17 at 10:07
  • You are right about the last part but when I remove the two exclamation points or variable identifies (whatever) the script just closes itself when it reaches the for loop (no new users created aswell). Any other ideas. Also to clarify the value of the string _instantUser_ is the name of the variable I want to call/use its value. – makrasnov100 Jan 09 '17 at 10:15
  • You cannot do arithmetics like that, you have to use `set /A` for that; I suggest to do it in advance, like `set /A allowed_users_length+=1`, then `for /L %%b (1,1,%allowed_users_length%) do `, then `set "instantUser=allowed_users[%%b]"`; by the way, you are missing the closing `)`; and your code will *never* result in `ECHO is off.`! – aschipfl Jan 09 '17 at 10:18
  • And you are missing a closing parenthesis as well at the end. And using requires the line `setlocal EnableDelayedExpansion` at the top of the batch-file. And access it then using `!instantUser!`. – geisterfurz007 Jan 09 '17 at 10:19
  • Ok for now, let's forget about the +1 part (I removed it since the array "index" does start at 0). I didn't put in the full code but in it I do have the closing parenthesis to close the "for" loop. Also, I do have the _setlocal EnableDelayedExpansion_ command at the start of the program. @aschipfl was right that I didn't have ECHO is off result because I mistaken it from a different version of thesame program. _!instantUser!_ also results in a runtime error where the script closes when it reaches the for loop. – makrasnov100 Jan 09 '17 at 10:41
  • All you should need at the `for /l` is ` do net user "!allowed_users[%%b]!" /Add`, don't `set instantuser` if you don't need to …and there's no way that voice goes with the guy in the picture! – Compo Jan 09 '17 at 11:19
  • 1
    I suggest you to read [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Jan 09 '17 at 17:00

1 Answers1

1

[Example]

@Echo Off
SetLocal EnableDelayedExpansion

Set "allowed_users[0]=Johnny Was"
Set "allowed_users[1]=Jimmy Jazz"
Set "allowed_users[2]=Jimmy Jimmy"
Set "allowed_users[3]=Johnny Jewel"

Set/A "allowed_users_length=-1"
For /F %%A In ('Set allowed_users[') Do Set/A "allowed_users_length+=1"

For /L %%b In (0,1,%allowed_users_length%) Do (
    Net User "!allowed_users[%%b]!" /Add
)
Compo
  • 36,585
  • 5
  • 27
  • 39
  • This answer is only slightly flawed in that the variables allowed_users[0, 1, 3...] as well as the "array_ users_length" were already defined in the "create_array" script call. Most likely because of my mistake of not putting the entire code for the script. Yet it is correct in that it achieves what I anted. to use the "for" loop counter as part of the variable name I just needed !allowed_users[%%b]! without the combination of any strings. Thx for the answer @Compo – makrasnov100 Jan 09 '17 at 20:48