Merge data from two text files

I have two text files.  First one will have a username with the format of the SAMid (UserOne).  There will only be one entry in this file.  The 2nd file has particular groups the user belongs too with the CN name "(cn=groupname.ou=groups,dc=domain,dc=ext)".  There may be two or more entries in this file.  I want to merge these entries into one text file that would prefereably look like

"(cn=groupname1.ou=groups,dc=domain,dc=ext)";UserOne
"(cn=groupname2.ou=groups,dc=domain,dc=ext)";UserOne

OR

UserOne;"(cn=groupname1.ou=groups,dc=domain,dc=ext)"
UserOne;"(cn=groupname2.ou=groups,dc=domain,dc=ext)"

In this case the delimiter needs to be something other than a comma, such as simicolon.  The entries can be reversed, I don't really care.  I'd prefer a DOS command (FOR), since that would fit in with the rest of the script.  We either create a new file with the merged data or append one of the existing files with the data from the other.
BigmacMcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Something like this? It expects the user file name as first argument, the group file name as second argument, and creates a file with the name of both the user and the group file.
@echo off
setlocal
set Delim=;
set UserFile=%~1
set GroupFile=%~2
set ResultFile=%~n1-%~nx2
if exist "%ResultFile%" del "%ResultFile%"
for /f "delims=" %%a in ('type "%UserFile%"') do set UserID=%%~a
for /f "delims=" %%a in ('type "%GroupFile%"') do (
  >>"%ResultFile%" echo %UserID%%Delim%%%~a
)

Open in new window

0
BigmacMcAuthor Commented:
Does line 4 & 5 need the actual path/filename for the user & group files?  I'm confused as to where the actual file names are defined.  The file names are Users_UserOne.txt and GroupsFor_UserOne.txt

0
oBdACommented:
Currently, you'll have to call the script with the two arguments (in case you have more than one of each file and want to process them with another script):
whatever.cmd "Users_UserOne.txt" "GroupsFor_UserOne.txt"
But nothing keeps you from setting the variables directly in lines 4 and 5. If the script is in the same folder as the files, the names are enough.
@echo off
setlocal
set Delim=;
set UserFile=Users_UserOne.txt
set GroupFile=GroupsFor_UserOne.txt
set ResultFile=%~n1-%~nx2
if exist "%ResultFile%" del "%ResultFile%"
for /f "delims=" %%a in ('type "%UserFile%"') do set UserID=%%~a
for /f "delims=" %%a in ('type "%GroupFile%"') do (
  >>"%ResultFile%" echo %UserID%%Delim%%%~a
)

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

BigmacMcAuthor Commented:
It's working kindof.  I was mistaken in that the User file may have more than one entry
UserOne
UserTwo

When I run the script I'm getting  the following in the ResultFile

UserTwo ;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo ;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo ;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo ;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home

Two should say UserOne and two should say UserTwo.  Also there's a space after the Usertwo.
0
oBdACommented:
The space after the user names is in the initial text file.
And if there are several entries in the user file, try this:
@echo off
setlocal
set Delim=;
set UserFile=Users_UserOne.txt
set GroupFile=GroupsFor_UserOne.txt
set ResultFile=%~n1-%~nx2
if exist "%ResultFile%" del "%ResultFile%"
for /f %%a in ('type "%UserFile%"') do (
  for /f "delims=" %%g in ('type "%GroupFile%"') do (
    >>"%ResultFile%" echo %%a%Delim%%%~g
  )
)

Open in new window

0
BigmacMcAuthor Commented:
Ok...almost there.  It looks good and would be workable.  The ResultFile is not exacty right, though usable.  There are two issues with the ResultFile.

1.  There's multiple entries.  For instance if UserOne is a member of two groups (GroupName1,
     Groupname2) and UserTwo is a member of three groups (Groupname1, Groupname2 and
     Groupname3).  I see 10 entries for each user (total of 20 lines).
2.  The ResultFile contains entries for Groupname3 for UserOne, even though he's not a member.

Here's what's in the ResultFile

UserOne;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.Three,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.Three,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserOne;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Three,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Three,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.Two,OU=Groups,OU=Development,DC=bigmac,DC=home
UserTwo;CN=Firewall.One,OU=Groups,OU=Development,DC=bigmac,DC=home
0
oBdACommented:
Uhm - based on which information is the script supposed to determine which of the entries in the groups file belongs to which entry in the user file?
This should query the group membership of the users in the text file for any group containing "Firewall" directly from AD:
@echo off
setlocal
set Delim=;
set UserFile=Users_UserOne.txt
set ResultFile=%~n1-%~nx2
if exist "%ResultFile%" del "%ResultFile%"
for /f "%%a" in ('type "%UserFile%"') do (
  for /f "delims=" %%d in ('dsquery user -samid "%%a" ^| dsget user -memberof -expand ^| find /i "firewall"') do (
    >>"%ResultFile%" echo %%a%Delim%%%~d
  )
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ReneGeCommented:
Nice script oBdA

As oBdA was sharing, his script is based on the information you shared, assuming there will not be any spaces.

If spaces could happen, change line 7 to:
for /f "delims=" %%a in ('type "%UserFile%"') do (

Cheers,
Rene
0
BigmacMcAuthor Commented:
Both of these fail in different ways.

If I use Line 7 as written by oBdA I get an 'IN' not expected at this time...and it is referring to 1st IN.  Here's the result with @echo on
___________________________________________________________________________

C:\Scripts\DisableAccts\Test>disablenyaccts

C:\Scripts\DisableAccts\Test>setlocal

C:\Scripts\DisableAccts\Test>set Delim=;

C:\Scripts\DisableAccts\Test>set UserFile=c:\scripts\DisableAccts\Test\Users.txt


C:\Scripts\DisableAccts\Test>set ResultFile=c:\scripts\DisableAccts\Test\Process
.txt

C:\Scripts\DisableAccts\Test>if exist "c:\scripts\DisableAccts\Test\Process.txt"
 del "c:\scripts\DisableAccts\Test\Process.txt"
IN was unexpected at this time.

C:\Scripts\DisableAccts\Test>for /f "%a" IN ('type "c:\scripts\DisableAccts\Test
\Users.txt"') do (

C:\Scripts\DisableAccts\Test>
____________________________________________________________________

If I use it as ReneGe: wrote it I get a 'Target object missing'  Here's what happens.


C:\Scripts\DisableAccts\Test>disablenyaccts

C:\Scripts\DisableAccts\Test>setlocal

C:\Scripts\DisableAccts\Test>set Delim=;

C:\Scripts\DisableAccts\Test>set UserFile=c:\scripts\DisableAccts\Test\Users.txt


C:\Scripts\DisableAccts\Test>set ResultFile=c:\scripts\DisableAccts\Test\Process
.txt

C:\Scripts\DisableAccts\Test>if exist "c:\scripts\DisableAccts\Test\Process.txt"
 del "c:\scripts\DisableAccts\Test\Process.txt"

C:\Scripts\DisableAccts\Test>for /F "delims=" %a IN ('type "c:\scripts\DisableAc
cts\Test\Users.txt"') do (for /F "delims=" %d in ('dsquery user -samid "%a" | ds
get user -memberof -expand | find /I "firewall"') do (echo %a;%~d 1>>"c:\scripts
\DisableAccts\Test\Process.txt" ) )

C:\Scripts\DisableAccts\Test>(for /F "delims=" %d in ('dsquery user -samid "Uone
 " | dsget user -memberof -expand | find /I "firewall"') do (echo Uone ;%~d 1>>"
c:\scripts\DisableAccts\Test\Process.txt" ) )
dsget failed:'Target object for this command' is missing.
type dsget /? for help.
C:\Scripts\DisableAccts\Test>(for /F "delims=" %d in ('dsquery user -samid "UTwo
 " | dsget user -memberof -expand | find /I "firewall"') do (echo UTwo ;%~d 1>>"
c:\scripts\DisableAccts\Test\Process.txt" ) )
dsget failed:'Target object for this command' is missing.
type dsget /? for help.C:\Scripts\DisableAccts\Test>
______________________________________________________________________

Sorry this being such a pain.

Bigmac
0
oBdACommented:
Sorry, the quotes around %%a shouldn't have been there.
@echo off
setlocal
set Delim=;
set UserFile=Users_UserOne.txt
set ResultFile=Users_Groups.txt
if exist "%ResultFile%" del "%ResultFile%"
for /f %%a in ('type "%UserFile%"') do (
  for /f "delims=" %%d in ('dsquery user -samid "%%a" ^| dsget user -memberof -expand ^| find /i "firewall"') do (
    >>"%ResultFile%" echo %%a%Delim%%%~d
  )
)

Open in new window

0
BigmacMcAuthor Commented:
I removed the " " from around "%%a" on Line 7 and it worked perfectly.  Don't know why I didn't see that before.

Thanks so much oBdA
0
BigmacMcAuthor Commented:
Elegantly simple
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Operating Systems

From novice to tech pro — start learning today.

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.