Link to home
Start Free TrialLog in
Avatar of BigmacMc
BigmacMc

asked on

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
Avatar of Lior Karasenti
Lior Karasenti

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

Avatar of BigmacMc

ASKER

liorkr-
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"
etc;etc
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

:loop
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.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Qlemo

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!