Solved

DOS script to combine data from two files into one

Posted on 2013-11-20
6
183 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
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

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

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.

Question has a verified solution.

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

Suggested Solutions

OfficeMate Freezes on login or does not load after login credentials are input.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

910 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

24 Experts available now in Live!

Get 1:1 Help Now