Batch extract data from multiple text files

December 14, 2009 at 12:50:14
Specs: Windows XP
Hi, I want to make a files that lists information from each of about 50 data files from temperature dataloggers. I wouid like the output file to contain a line for each of these text files; this line would contain just the "Serial Number" and the date and time of the "First Point" and the "Last Point".

Each file begins with information in the format below. Can anyone help me write a batch file to extract this information? I am a rank beginner at this. Thank you!
"Series"," Temperature (*C)"
"Logger Info","Information specific to the logger"
" Model","Optic StowAway-TEMP(C)ONSET -4C TO 38C"
" Serial Number","186003"
" Memory Size (Bytes)","8192"
" Extra Info","Information used by tech support"
" Model Number","3"
" Version Number","5"
"Series Info","Information about the data in the series"
" Points Used","1325"
" First Point","08/26/97 19:55:59.0"
" Last Point","09/06/97 20:43:59.0"

See More: Batch extract data from multiple text files

Report •

December 14, 2009 at 13:46:05
Can't really help without knowing the systax for the file names. If you could provide an example of the file names it would help.

@echo off

echo Output File > output.txt

for /f "tokens=*" %%a in ('dir/b') do (
  set filename=%%a
  for /f "tokens=1,2,* delims=," %%a in ('type %filename%') do (
    if /I "%%a"=="Serial Number" echo %%a %%b >> output.txt

I think will work but I don't think you can nest For loops like that. I will have to test this

< Tested >

It does not work the closest I can come up with is

@echo off
setLocal EnableDELAYedExpansion

echo Output File > output.txt

for /f "tokens=*" %%a in ('dir *.txt /b') do (
  call :readfile %%a
goto :EOF

for /f "tokens=1,2,* delims=," %%a in ('type %1') do (
  if /i "%%a"==" Serial Number" echo %%a %%b
goto :EOF

but with the quotes it makes it hard to compare. I have to look into this tomorrow.

Report •

December 14, 2009 at 14:39:27
Ace, thanks for the reply. I will look at your reply and learn a bit in the deciphering process. As for the file names, sorry I didn't mention that. There is no system to the file names, except that they all have the "txt" extension. I was hoping I could just put the files into a folder and process them all that way.

Report •

December 14, 2009 at 16:41:03
As a followup: If it makes this simpler, it will work fine for me it the output file contains the whole line for each of the three data chunks I need. that is, the output file could contain:

" Serial Number","186003"
" First Point","08/26/97 19:55:59.0"
" Last Point","09/06/97 20:43:59.0"

and then on to the output from the next text file in the folder. The data I need are always in the same line numbers of the files.

I just need a way to easily inventory the temperature data I have.


Report •

Related Solutions

December 14, 2009 at 17:01:11
you can just use findstr for your simple case of file parsing. see findstr /? for more.

findstr /C "Serial" /C "First Point" .......

GNU win32 packages | Gawk

Report •

December 15, 2009 at 11:55:01
Thanks Ghost. I've looked up help for findstr and yes, it seems that that command would work for my purpose. I am trying to piece together the rest of the file. So far I haven't been successful in outputing results to another file. Sure is difficult to get started with these commands, for me anyway. Could someone help with the basics of forming the rest of the file? I'd really appreciate it and once I get it going I think it will be easier for me to learn more. Thank you!

Report •

December 15, 2009 at 13:11:09
it just looks like you need to follow up with this using ghostdog's suggestion:

@echo off > outfil && setlocal enabledelayedexpansion
:: change 0 to 1 if you want the surrounding quotes included
set /a qts=0
for /f "tokens=2,3 delims=:," %%a in ('findstr /i /l /c:"serial number" /c:"first point" /c:"last point" *.txt') do (
if %qts%==1 (set oo=%%b) else (set oo=%%~b)
echo !oo! >> outfil

Report •

December 15, 2009 at 13:39:39
OK, thanks very much all; this gives me a good base to work from.

Report •

December 16, 2009 at 06:02:07
This acts on a file called TGDATA. If this does the deed, we can make it process multi files.

@echo off > newfile & setLocal enableDELAYedexpansion

for %%a in (" Serial Number" " First Point" " Last Point") do (
find %%a < tgdata >> newfile

Helping others achieve escape felicity


Report •

Ask Question