Batch file to extract data from text file

May 18, 2010 at 11:19:34
Specs: Windows 2003

I have a text file, 'test1.txt'. It contains data in the following format:,o=combined,c=us

How can I extract the "uid" field into another text file and add it in the following format.
user import,o=combined,c=us
user modify account-valid yes

I will later run the new text file as a batch script.

Any advice will be greatly appreciated.

See More: Batch file to extract data from text file

Report •

May 18, 2010 at 11:27:27
HEre is how...

I am not going to write the code for you, you need to do that yourself.

Report •

May 18, 2010 at 13:57:57
@echo off & setlocal
for /f "tokens=*" %%a in ('find /i "uid="^<test1.txt') do set %%a
(echo user import %uid% cn=%uid%,o=combined,c=us
echo user modify %uid% account-valid yes)>test.bat

Report •

May 18, 2010 at 14:26:27
Hey nbrane,

Thanks a lot for your response.

The script does exactly what I expect it to do, except it does that only for the last user. All the users preceding the last one do not get output to the text file.

I tried by replacing '>test.bat' with '>>test.bat'. Still no luck.

Report •

Related Solutions

May 18, 2010 at 14:33:36
oh, ok, misunderstood thinking only one instance in the file.
try this:
@echo off & setlocal enabledelayedexpnasion
for /f "tokens=*" %%a in ('find /i "uid="^<test1.txt') do (
set %%a
(echo user import !uid! cn=!uid!,o=combined,c=us
echo user modify !uid! account-valid yes)>>test.bat
::------ end

Report •

May 18, 2010 at 14:46:28
Hey nbrane,

This time the output is in the correct format, but does not include the usernames.

user import cn=,o=combined,c=us
user modify account-valid yes

Report •

May 18, 2010 at 15:18:23
Thanks a lot nbrane! Works flawlessly now.

Report •

Ask Question