,cmd/.bat file called to create users+groups from provided .txt file - getting endless input screen

时间:2017-04-10 00:36:06

标签: windows batch-file

This is my second question as I am really new to programming.

I have a .cmd file that was called from a previous .cmd using %%A %%B %%C for the following .txt file:

Name     ID     Department
Anna     A002   News
Bob      B006   Distribution
Billy    B007   Sales
Joe      J004   News
Mark     M007   Printing

The user name is to be the ID, full name the name and group the department.

Currently all my scrips run perfectly except my creation script. It is my understanding that when a token is passed to a child script it will use %1, %2, %3 instead of %%A, %%B, %%C.

NO errors display on the screen to tell me what has gone wrong, however the script only reads the first user then proceeds to a never ending _ _ ... well you get the idea. ctrl+C exits.

The script is as follows.

::Create users and groups using columns stated by variables in caller script.

:CHECKGROUP
@ECHO OFF
NET LOCALGROUP | FIND /i "%3" > NUL && GOTO GROUPEXISTS
NET LOCALGROUP %3 /ADD

:GROUPEXISTS
GOTO USER

:USER
NET USER | FIND /i "%2" > NUL && GOTO USEREXISTS
NET USER %2 %1 /ADD

:USEREXISTS 
NET LOCALGROUP %3 | FIND /i "%2" > NUL && GOTO FOLDER
NET LOCALGROUP %3 %2 /ADD

:FOLDER
FIND /i "C:/home/%2" > NUL && GOTO End
MKDIR C:/home/%2

:END
ECHO Users and/or groups have been created.
PAUSE
EXIT

I have edited to try fixing it and now get:

The command completed successfully 
The command completed successfully
The command completed successfully
_
_
:: have to ctrl+C to exit

The edit is as follows.

::Create users and groups using columns stated by variables in caller script.
:CHECKGROUP
@ECHO OFF
NET LOCALGROUP | FIND /i "%3" > NUL && GOTO GROUPEXISTS
NET LOCALGROUP %3 /ADD

:GROUPEXISTS
GOTO USER

:USER
NET USER | FIND /i "%2" > NUL && GOTO 
NET USER %2 %1 /ADD
NET LOCALGROUP %3 %2 /ADD


:FOLDER
FIND /i "C:/home/%2" > NUL && GOTO End
MKDIR C:/home/%2

:END
ECHO Users and/or groups have been created.
EXIT

1 个答案:

答案 0 :(得分:0)

Found the problem.

The find command couldn't locate the specified Home folder (wasn't needed) as well as a couple other things.

In case anyone has a similar issue, the final script is as follows:

::Create users and groups using columns stated by variables in caller script and create a folder with the username.
:CHECKGROUP
@ECHO OFF
NET LOCALGROUP | FIND /i "%3" > NUL && GOTO GROUPEXISTS
NET LOCALGROUP %3 /ADD

:GROUPEXISTS
GOTO USER

:USER
NET USER | FIND /i "%2" > NUL && GOTO FOLDER
NET USER %2 %1 /ADD
NET LOCALGROUP %3 %2 /ADD

:FOLDER
MKDIR C:\home\"%2"

:END
ECHO Users and/or groups have been created.
PAUSE
EXIT