DOS script to combine data from two files into one

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
Who is Participating?

Improve company productivity with a Business Account.Sign Up

QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
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.
Lior KarasentiCommented:
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

BigmacMcAuthor Commented:
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"
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Lior KarasentiCommented:
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.
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.
BigmacMcAuthor Commented:

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.