Solved

DOS script to combine data from two files into one

Posted on 2013-11-20
6
180 Views
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
0
Comment
Question by:BigmacMc
6 Comments
 
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

0
 

Author Comment

by:BigmacMc
ID: 39667336
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
0
 
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

: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.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 39

Expert Comment

by:footech
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.
0
 
LVL 68

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:BigmacMc
ID: 39692711
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!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
OfficeMate Freezes on login or does not load after login credentials are input.
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now