Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Help adding tokens on same line

Original Message
Name: hernanhr
Date: October 18, 2007 at 10:15:41 Pacific
Subject: Help adding tokens on same line
OS: XP
CPU/Ram: 2gig with 1gig mem
Model/Manufacturer: HP
Comment:
I need help with a batch file that will read from a single file called “Userlist.txt” and then create another file based on that list.

I have some of it already but I’m stuck on getting the first token to be listed on the same line in the “addusers.txt” file instead of on separate lines.

The “Userslist.txt file should look like this:
*********Userlist.txt file***********
Joeuser1,Joe Blow, P@ssw0rd,Information about Joe 1
joeuser2,Joe Blow, P@ssw0rd,Information about Joe 2
joeuser2,Joe Blow, P@ssw0rd,Information about Joe 3
*********Userlist.txt file***********

I want to be able to dynamically create an adduser.txt file using the information from the userlist.txt file. The “Adduser.txt file should look like this:

*********Begin Adduser.txt file***********
[USERS]
Joeuser1,Joe Blow, P@ssw0rd,Information about Joe 1
joeuser2,Joe Blow, P@ssw0rd,Information about Joe 2
joeuser2,Joe Blow, P@ssw0rd,Information about Joe 3
[local]
Administrators,,joeuser1,joeuser2,joeuser3
Engineers,,joeuser1,joeuser2,joeuser3
********End Adduser.txt file***************

Currently I have test.bat file
**********Begin test.bat*************
@echo off
Cls

Rem This portion of the script creates the adduserslist.txt file, but I don’t know how to create the
echo [USERS] > D:\Maintenance\adduserlist.txt
for /f "tokens=*" %%A in (D:\Maintenance\Userlist.txt) do (
echo %%A >> D:\Maintenance\adduserlist.txt
)

echo [local] >> D:\Maintenance\adduserlist.txt
echo Administrators, >> D:\Maintenance\adduserlist.txt
for /f "tokens=1 delims=," %%a in (D:\Maintenance\Userlist.txt) do (
echo ,%%a >> D:\Maintenance\adduserlist.txt
)


echo Engineers, >> D:\Maintenance\adduserlist.txt
for /f "tokens=1 delims=," %%a in (D:\Maintenance\Userlist.txt) do (
echo ,%%a >> D:\Maintenance\adduserlist.txt
)

rem d:\Maintenance\addusers /c d:\Maintenance\adduserlist.txt

*******End test.bat file*************


Report Offensive Message For Removal


Response Number 1
Name: cristian23
Date: October 19, 2007 at 04:31:41 Pacific
Subject: Help adding tokens on same line
Reply: (edit)
How about creating the string to print out in a variable, and then printing it just once the file.

To make the set work nicely in the for loop something of a trick is needed.

Option 1: Delayed Variable Expansion (http://www.robvanderwoude.com/variableexpansion.html)

****************************
setlocal ENABLEDELAYEDEXPANSION

set string=Administrators
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call set string=!string!,%%a
echo %string% >> adduserlist.txt

...

endlocal
****************************


Option 2: A Function call
****************************
set string=Administrators
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call :make_string %%a
echo %string% >> adduserlist.txt

set string=Engineers
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call :make_string %%a
echo %string% >> adduserlist.txt

rem Exit script
goto :EOF

REM Function
:make_string
set string=%string%,%1
goto :EOF
****************************


Report Offensive Follow Up For Removal

Response Number 2
Name: hernanhr
Date: October 22, 2007 at 14:43:55 Pacific
Subject: Help adding tokens on same line
Reply: (edit)
cristian23,

Wow, that worked perfect. Thanks for the help

hrh

Just for your info, my scripts does a little bit more. It creates the users, creates directories for each of the users and then grants permissions to the user directories based on the user that owns them.

Here it is:

*******Begin file
@echo off
Cls
setlocal ENABLEDELAYEDEXPANSION
set yes=d:\Maintenance\y.txt
set xcacls_tool=d:\Maintenance\xcacls.exe

echo [USERS] > D:\Maintenance\adduserlist.txt
for /f "tokens=*" %%A in (D:\Maintenance\Userlist.txt) do (
echo %%A >> D:\Maintenance\adduserlist.txt
)

echo [local] >> D:\Maintenance\adduserlist.txt
set string=Administrators,
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call set string=!string!,%%a
set string=!string!,
echo %string% >> adduserlist.txt

set string=ENG_GRP,
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call set string=!string!,%%a
set string=!string!,
echo %string% >> adduserlist.txt

set string=SCM_GRP,
for /f "tokens=1 delims=," %%a in (Userlist.txt) do call set string=!string!,%%a
set string=!string!,
echo %string% >> adduserlist.txt

d:\Maintenance\addusers /c d:\Maintenance\adduserlist.txt

d:

cd D:\Development\users
for /f "tokens=1 delims=," %%A in (D:\Maintenance\Userlist.txt) do (

md D:\Development\users\%%A
md D:\Development\users\%%A\projects
md D:\Development\users\%%A\submit
)


for /f "tokens=1 delims=," %%A in (D:\Maintenance\Userlist.txt) do (
%xcacls_tool% D:\Development\users\%%A /t /g Administrators:f ENG_GRP:r SCM_GRP:r %%A:c < %yes%
)

endlocal
********End file



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Help adding tokens on same line

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC