copy data from rows to columns

January 20, 2012 at 11:21:13
Specs: Windows 7
I m new to batch file coding, I need a code to search a text file that was generated from a server, the file got information about customers contact and some parameters.

File name: contacts.txt
The information looks much like below


Other info
Tag 13
code1 2
code2 5

The above will be repeated for all the customers
What I want to do is transfer the above to other text file in one row per customer such as

ID tag code1 code2
123445456 13 2 5
343454545 53 6 66

See More: copy data from rows to columns

Report •

January 20, 2012 at 12:53:21
well you can use this to extract the line numbers following the ID code
set /a count=1
for /f "skip=1 delims=[]" %%a in ('find /n "ID" contacts.txt') do set /a ID%count%=%%a + 1
rem you need something to extract the next line number from the file, working on it
for /f "skip=1 tokens=1,2 delims= " %%a in ('find /n "Tag" contacts.txt') set Tag%count%=%%b
for /f "skip=1 tokens=1,2 delims= " %%a in ('find /n "code1" contacts.txt') set Code1%count%=%%b
for /f "skip=1 tokens=1,2 delims= " %%a in ('find /n "code2" contacts.txt') set Code2%count%=%%b

now you have envvars for ID, Tag, code1 and code2, just reassemble from there
set /a count=0
for /f "tokens=1,2 delims=^=" %%a in ('SET ID') do set /a count=%count% + 1&echo ID%count% Tag%count% code1%count% code2%count%>>newfile.txt

one comment, im still working on the routine that grabs just a selected line number (the result of ID%count%), so this is not fully working.. but you get the idea, right?

ok to get the the value of the line number following the ID tag, do something like this: (this only is working for one line number, so you have to do a loop to get all the values)

for /f "tokens=1,2 delims=]" %%a in ('find /v /n "" contacts.txt^|find "[7]"') do set ID%count%=%%b

so loop this for every ID%count% and you should be able to reassemble from there.

edit: so this section of code returns the next line following the ID statement. not pretty though

setlocal enabledelayedexpansion
del file3.txt
for /f "skip=2 delims=[]" %%a in ('find /n "ID" contacts.txt') do set /a ID%%a=%%a+1&echo !ID%%a!>>file3.txt

find /v /n "" contacts.txt>contacts2.txt

rem for /f "" %%a in (file3.txt) do echo %%a

for /f "" %%a in (file3.txt) do for /f "skip=2 tokens=1,2* delims=]" %%c in ('find "[%%a]" contacts2.txt') do echo ID%%a is %%d

Report •

January 21, 2012 at 21:54:19
thanks, I'm trying to run from my work PC "limited privilege", i keep getting the error " file not found - contacts.txt", do you think its something to do with the privilege.

Report •

January 23, 2012 at 10:32:25
no, execute the batch file in the folder that contains contacts.txt, else the routine will fail (as you have found)

the file contacts.txt is what you mentioned in your original post, so my routine presumes that it does exist in the current directory. you can also hardcode the path for the file.


Report •
Related Solutions

Ask Question