Batch file - how to extract data from txt file and read

October 30, 2012 at 09:26:32
Specs: DOS
=====================================================================Transactions Report.
All clients.
Stock : B1234567 name.
Holding Filter : Active
Restricted to nominee :

Want to read B1234567 into a variable called "stock_sedol". This data always appears in row 4 of the text file.

5 disposals, selling 94 stocks for £1.78
4 acquisitions, buying 102 stocks for £3.54

I then want to read the 94 into a variable called "disposals" and the 102 into a variable called "acquisitions", these rows are always 5th and 6th from the bottom of the text file.

I have tried reading the file line by line but its too slow (Some files can be 1000 lines long) so need it to jump to the 4th line, then the last line - 5, then the last line -6.

Any help appreciated......

See More: Batch file - how to extract data from txt file and read

Report •

October 31, 2012 at 07:14:25
You need to work on describing your problem better. Very unclear. Nevertheless here's my attempt, modify it to your preference:
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /C:"stringyouwanttosearchfor" myfile') do set line=!line!%%a

Report •

October 31, 2012 at 20:05:41
This might work, but only if the search-strings ("Stock", "aquisitions", "disposals") are unique in each file.

::======= begin script letmein
@echo off & setlocal
for /f "tokens=1-5 delims=:, " %%a in ('findstr /g:letmein.g infile') do (
if %%a equ Stock (set %%a=%%b) else (set %%b=%%d)
echo stock: %stock%
echo aq: %aquisitions%
echo disp: %disposals%
::====== end script

===== begin textfile letmein.g (used by findstr in above):
Stock :
====== end textfile

You might get away with no textfile, using this findstr syntax:
findstr "Stock aquisitions, disposals," infile
Anything you use has to read the whole file, but of course binaries like findstr are much faster than batch. vbscript can read the whole file into one var., but it still has to be parsed/filtered for your data.

Report •

November 1, 2012 at 02:26:48
Thanks for the replies....

When I run nbrane scripts I get follwoing...

FINDSTR: Search string too long.
Press any key to continue . . .

Any thoughts?

Also how is the script to find text "acquisitions" and "disposals", I can see it checking for "stock".... Do I replay it 3 times with different parameters?


Report •

Related Solutions

November 1, 2012 at 13:47:09
It sounds like the .g file is not right. It should look exactly like the contents I posted. Maybe there's a bunch of spaces in it, or invisible characters. If you would, post the content of whatever file you are using for the /g: argument (which I called letmein.g). Also, did you try the other syntax? Looks like this:
for /f "tokens=1-5 delims=:, " %%a in ('findstr "Stock aquisitions, disposals," infile') do

This syntax obviates the necessity of using another file (the /g: source-file).

Report •

Ask Question