DOS script to combine data from two files into one

Posted on 2013-11-20
Last Modified: 2013-12-03
I have two text files.  

The 1st file is called fullnames.txt and contains the user DN's such as
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext".  
This file will have at least one line, but may have many.

The 2nd text file has a name like SamID.txt  and contains the DN's of groups the user associated with the SamID is a member of; i.e. "cn=groupname,ou=GroupOU,DC=DomainName,DC=ext".  
The group DN's may be different in each file and number of lines may vary in each text file.  There will be as many of these files as there are lines in the 1st file.

What I want to do is create a new text file or append every line in the 2nd file (SamID1.txt; SamID2.txt; etc) to read
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext";"cn=groupname,ou=GroupOU,DC=DomainName,DC=ext"

Hopefully this makes some sense.  In the text files containing the group names I want to add the users DN and a ; at the beginning of each line.  The data from all the files is derived from a FOR command using a source file containing the users SamID:

for /f "tokens=1 delims=," %%a IN (sourcefile) DO dsquery user forestroot -samid %%a | dsget user -dn>>fullnames.txt

for /f "tokens=1 delims=," %%a IN (sourcefile) DO dsquery user forestroot -samid %%a | dsget user -memberof>>%%a.txt
Question by:BigmacMc
LVL 17

Expert Comment

by:Lior Karasenti
ID: 39665550
Try the following script:
@echo off

set File1=1.txt
set File2=2.txt
set NewFile=3.txt

for /f "delims=" %%a in ("%file1%") do for /f "delims=" %%b in ("%file2%") do echo %%a;%%b>>%NewFile%

goto :eof

Open in new window


Author Comment

ID: 39667336
I tried this, but the script as written is processing the files names, not the contents of the file.

File2 will always have multiple lines in it containing the DN of each group the user is a member of.  File1 will always have at least one line containing the DN of user who is a member of the groups in File1.  Since File2 will always have multiple lines perhaps it should the first one listed in the new output file.  The end result should look like this in the new file:

"CN=GroupName1,OU=OUName,DC=Domain,DC=Ext";"CN=FN1 LN1,OU=OUName,DC=Domain,DC=Ext"
"CN=GroupName2,OU=OUName,DC=Domain,DC=Ext";"CN=FN1 LN1,OU=OUName,DC=Domain,DC=Ext"
"CN=GroupName3,OU=OUName,DC=Domain,DC=Ext";"CN=FN1 LN1,OU=OUName,DC=Domain,DC=Ext"
LVL 17

Expert Comment

by:Lior Karasenti
ID: 39668039
How about this?
@echo off

setlocal enabledelayedexpansion

set File1=Groups.txt
set File2=Users.txt
set NewFile=Combined.txt
set skip=
set n=0

for /f "delims=" %%? in (%File1%) do (call :loop
echo "%%~?";"!x!">>%NewFile%)
goto :eof

set x=
for /f "%skip%delims=" %%? in (%File2%) do (set x=%%~?
set /a n+=1
set skip=skip=!n! 
goto :eof)

Open in new window

There's probably a prettier way to do this, but it seems to be working fine.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 39

Expert Comment

ID: 39670991
If I'm understanding right, you actually have a lot more than two files.  You'll have one file where each line is a user.  And then for each line you'll have another file which would match up, so you would actually have; 1 file + (the number of lines in the first file) files
And it sounds like you want to generate as many files as you already have and not combine all of them into one.

Frankly, it would be much simpler to just redo the query and output the information rather than combine all of this.  I also don't understand the desire to have so many different files.
In your example you have the combined output as
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext";"cn=groupname,ou=GroupOU,DC=DomainName,DC=ext"

Open in new window

but what would multiple groups look like?  Would it be like this?
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext";"cn=groupname,ou=GroupOU,DC=DomainName,DC=ext"
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext";"cn=groupname2,ou=GroupOU,DC=DomainName,DC=ext"
"CN=Firstname Lastname,OU=OUName,DC=DomainName,DC=ext";"cn=groupname3,ou=GroupOU,DC=DomainName,DC=ext"

Open in new window

I feel it would be much more useful to output all users and their groups into one file.  PowerShell could do this easily.
LVL 69

Accepted Solution

Qlemo earned 500 total points
ID: 39672678
I cannot see any reason to use
dsquery forestroot -samid %%a | dsget user -dn

Open in new window

instead of
dsquery forestroot -samid %%a

Open in new window

Having that in mind, I go with footech about generating the content as wished from start:
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1 delims=," %%a IN (sourcefile) DO (
  for /F "tokens=*" %%b in ('dsquery user forestroot -samid %%a') do set usrdn=%%b
  for /F "tokens=*" %%b in ('dsget user !usrdn! -memberof') do echo,!usrdn!;%%b
) > %%a.txt

Open in new window

Using PowerShell would be a better idea here, btw. cmd.exe isn't suited that well for processing and mixing command output.

Author Closing Comment

ID: 39692711

Your solution worked perfectly for me.  As you've probably concluded, I'm not a scriptor.  I've only recently started playing with Powershell.

Thanks very much!

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't log in to windows 7 11 60
Cannot Change Local DNS 9 44
Inaccessible Boot Device after restore from backup image 19 68
Script to change all permissions to my user 7 35
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
OfficeMate Freezes on login or does not load after login credentials are input.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question