SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FOR loop Batch help!

    Hiya i was just wondering if theres someone with good knowledge of windows batch out there, whos willing to help me?

    For university iv been required to create a windows batch script which will help an admin. So far Iíve done add/remove/edit user, backup, system info etc however my tutor has advised me to create a FOR loop on add user. Atm the admin will manually enter each user name and password however i want to run a loop which will read an existing text file and automatically create the accounts.

    I understand that the structure of a loop is FOR %A IN (list) DO command [ parameters ]( http://www.robvanderwoude.com/for.php) and so far i have come up with ďfor %%a in (users.txt) do [net user /add]Ē something such as this but obviously its not working.

    Do i have to declare users.txt and insert it between net user and /add? Or is this completely wrong? Iv been trying to figure this out for days and there isnít much material to go on. Thanks in advanced for any help given, its much appreciated!

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    This might help a little: http://www.robvanderwoude.com/for.php

    If you want to go line by line, this would work for reading the text file:

    Code:
    FOR /f %%a in (users.txt) DO (
    ECHO %%a
    )



    If the users are delimited with commas, you will have to do this:

    Code:
    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    FOR /f "tokens=1,2,3,4 delims=," %%a IN (users.txt) DO ( 
        SET user=%user%%%a
        SET firstname=%firstname%%%b
        SET lastname=%lastname%%%c
        SET group=%group%%%d
        
        REM -- echo the variable names like this:
        ECHO !user! - !firstname! - !lastname! - !group!
        
        REM -- or the tokens like this:
        ECHO %%a - %%b - %%c - %%d
    )
    
    
    ENDLOCAL
    
    PAUSE
    Contents of users.txt delimited with commas:
    Code:
    roberts, robert, smith, users
    janej, jane, jones, administrators
    samanthar, sam, richardson, users
    peterc, peter, cottontail, administrators
    Resources:
    http://www.robvanderwoude.com/variableexpansion.php
    http://blog.crankybit.com/why-that-b...-isnt-working/
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •