Need some help with a Windows script

Jimmah

Golden Member
Mar 18, 2005
1,243
2
0
I've spent the last 3 days trying to make this work, ended up deleting about 5 pages worth of scripting becuase I just can't get it to work.

The idea here is to take a text file with 3 columns - Persons first name, ID and Group type - and run the script to read the group type and sort it while removing duplicates, then call up a second script to create groups from the sorted file while also creating user accounts from the first column of the original text. At the end the users get added to the already created groups and it goes back to the first script.


@Echo Off
**********************************************
Echo 'Backing up text1.txt to text1.t2t in case I screw this up'
Pause
@Echo Off
Copy text1.txt text1.t2t


@Echo Off

Echo Create Groups and Users files.

Echo.

FOR /F "tokens=3" %%i IN (text1.txt) DO Echo >>Groups1.txt %%i


FOR /F "tokens=1" %%f IN (text1.txt) DO Echo >>Users1.txt %%f


Sort Groups.txt > SortedGroups.txt

Call Groupcreation1.bat


Pause



The groupcreation batch file is below **************************

@Echo Off

ECHO creating groups
ECHO.

FOR /F %%i in (groups1.txt) DO NET LOCALGROUP %%i /ADD

REM show groups
NET GROUPS |MORE



ECHO create users
ECHO.

FOR /F "tokens=1" %%i IN (users1.txt) DO NET USER %%i %%j /ADD

ECHO finished creating users.
ECHO adding users to groups.
ECHO.

FOR /F "tokens=1,3" %%i IN (text1.txt) DO NET LOCALGROUP %%i %%j /ADD



**************************

My biggest problems are, in the first script I can't figure out how to have it sort and ignore duplicates then properly call the next script. The second one it seems to add the users and groups twice, I'm not sure exactly how to have it go back to the other script as I've never got that far.

Any insight, help, hell anything at all would be appreciated.
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
FYI, That really is not scripting - it looks like just a regular batch file (.bat) - NOT windows scripting.

What you are trying to do is not possible (or if it is, isn't easy I don't believe) in a batch file - you need to use windows scripting (vbscript/powershell).
 

Jimmah

Golden Member
Mar 18, 2005
1,243
2
0
FYI, That really is not scripting - it looks like just a regular batch file (.bat) - NOT windows scripting.

What you are trying to do is not possible (or if it is, isn't easy I don't believe) in a batch file - you need to use windows scripting (vbscript/powershell).

This explains why I have not had any sleep the past few days....

Sadly, this is how we were taught to do it and expected to do for it. I agree it isn't easy, probably why I'm having so much trouble with it.

Thanks for your input though.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
The idea here is to take a text file with 3 columns - Persons first name, ID and Group type - and run the script to read the group type and sort it while removing duplicates, then call up a second script to create groups from the sorted file while also creating user accounts from the first column of the original text. At the end the users get added to the already created groups and it goes back to the first script.
First of all, that is one really confusing description!

Second, I'm doubting that "sort it while removing duplicates" is possible in Windows without an extra program. I see no equivalent to the *NIX "sort -u" or "uniq".

And finally, may I suggest you...
slashhead.gif

:p
 

Jimmah

Golden Member
Mar 18, 2005
1,243
2
0
First of all, that is one really confusing description!

Second, I'm doubting that "sort it while removing duplicates" is possible in Windows without an extra program. I see no equivalent to the *NIX "sort -u" or "uniq".

And finally, may I suggest you...
slashhead.gif

:p

Ya know what the worst thing is.... for the longest time I was typing sort -u and it was giving me errors, kept thinking I mistyped it somewhere....then realized I was working in Windows not Linux. Two days without sleep does some serious hurting to your brain.

I agree with you, Perl would be great, hell I could do something in C in half the time and space this script/batch file will end up being.....
 

Jimmah

Golden Member
Mar 18, 2005
1,243
2
0
exactly what were your constraints on how this script is written?

It must be a min of two files, create the users and groups without duplication or erros saying a group or user already exist, must prompt when finished then run again and delete the created groups and users.

The deletion part is a no brainer - once I can figure out how to sort the initial groups file. From reading I'm thinking it will require the use of IF EXIST, so far my experiments using it have been unsuccessful.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
It must be a min of two files, create the users and groups without duplication or erros saying a group or user already exist, must prompt when finished then run again and delete the created groups and users.

The deletion part is a no brainer - once I can figure out how to sort the initial groups file. From reading I'm thinking it will require the use of IF EXIST, so far my experiments using it have been unsuccessful.

Is this for a class, or a job? If it is for a job, there is no reason to use a batch file, use windows scripts. If it is for a class, your teacher is sadistic and teaching you pointless stuff that has no real world application beyond being annoying (but have fun figuring it out anyways. Batch scripts are Turing complete, so you could technically do any computation with them that you could do another Turing completely language)
 

GeekDrew

Diamond Member
Jun 7, 2000
9,099
19
81
It must be a min of two files, create the users and groups without duplication or erros saying a group or user already exist, must prompt when finished then run again and delete the created groups and users.

The deletion part is a no brainer - once I can figure out how to sort the initial groups file. From reading I'm thinking it will require the use of IF EXIST, so far my experiments using it have been unsuccessful.

You didn't say in there anywhere that it had to be written as a batch file. I'd use vbscript (powershell would be preferable, but it's not installed by default on winxp).
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
I've spent the last 3 days trying to make this work, ended up deleting about 5 pages worth of scripting becuase I just can't get it to work.

The idea here is to take a text file with 3 columns - Persons first name, ID and Group type - and run the script to read the group type and sort it while removing duplicates, then call up a second script to create groups from the sorted file while also creating user accounts from the first column of the original text. At the end the users get added to the already created groups and it goes back to the first script.


@My biggest problems are, in the first script I can't figure out how to have it sort and ignore duplicates then properly call the next script. The second one it seems to add the users and groups twice, I'm not sure exactly how to have it go back to the other script as I've never got that far.

Any insight, help, hell anything at all would be appreciated.


I only have a few minutes to look at this now, but the first thing I see is that your for loops might be broken.

Code:
FOR /F "tokens=3" %%i IN (text1.txt) DO Echo >>Groups1.txt %%i
FOR /F "tokens=1" %%f IN (text1.txt) DO Echo >>Users1.txt %%f

You're writing token 1 (%%i) to groups1.txt and then reading the entire file again to write token 1 %%f to users1.txt. I would expect groups1.txt and users1.txt to pretty much look the same.

Also, your echo statement to output the data to the file looks like you're adding the data to the filename rather than the content of the file.

Look into using the delims= argument in your for loop to tokenize properly if that is the problem. Inspect your users1.txt and groups1.txt files to see if they are getting the right data.

For efficiency, I would have written it with a single for loop, something more along these lines.
Code:
FOR /F "tokens=3" %%i in (text1.txt) DO (
  echo %%k >> groups1.txt
  echo %%i >> users1.txt
)

Once you get that working, you can start working on removing dupes and trying to do stuff with your data. I'll check back in a bit.