1.

Solve : batch file for reading and coping strings within lines?

Answer»

Hi All,

I'm quite new to this, I want to achieve the following within my script,

1.read through each txt file in that folder
 2.loop through each txt file to find the string names in my dolist txt file
 3.if the txt file contains the string such as Daily Cost : 768 004, then print anything minus the Daily Cost
4.if the txt file contains the string such as Hello then printthe string in the next line
 5.the pint text should go to result txt. file

I tried this code;

echo off
if exist result.txt del result.txt
::for %%f in ('C:\Reservoir Management\script\*.txt')

do ( echo String match for %%f: >> result.txt
for /F "delims=" %%s in (dolist.txt)
do ( findstr "%%s" %%f >> result.txt ) )

but I' m getting an error, not even got passed the open folder, error Findstr cannot find %f, when runing the script.


Need your help please,

Thanks,
MatildaI think this is what you are trying to do but I am not sure.

Code: [Select]for %%f in ("C:\Reservoir Management\script\*.txt") do findstr /G:dolist.txt "%%f" >>result.txtHi  All,

here is my edited code
ECHO OFF
SET dolist=c:\miles\dolist.txt
SET searchFolder=C:\Reservoir Management\script\*.txt
SET resultFile=c:\miles\results.txt

DEL %resultFile%
ECHO .
ECHO Searching files, please wait...
FOR /F  %%i IN (%dolist%) DO CALL :FindMyString %%i
ECHO Search completed, see file %resultFile%

GOTO :EOF


:FindMyString
ECHO Search Results For "%1" >> %resultFile%
FINDSTR  %1 "%searchFolder%" >> %resultFile%
ECHO . >> %resultFile%
GOTO :EOF

however, my dolist  search is not returning the whole string like "Well name" the code is currently returning well and not name

really, what I want is within my dolist

if the well name exist within my search folder, then get or print any string minus the well name
if a mud type exist within my search folder then get or print the words in the next line.

Thank you

Here is what I think you should do.
POST a copy of you DoList file here and post an example of some of the text files you want to search and then shows us what you expect to be your end result.Thank you,

here is my dolist;
Well Name :
Midnight depth
Mud Type
Density
Cumulative Cost:
Daily Summary :


Here is my search text;
Daily Drilling Report
Well Name: ACA-804
Daily progress
Drilling Hours
Midnight depth
25/09/2011
Date Type
16/09/2010 BOP's Pressure test
Cumulative Cost:  768 098 900
19/09/2010 Gas Drill
19/09/2010 Ser.Co.Safety Mtg
25/09/2011 Sce Company meeting
Daily Summary :
Sailing to ACA-804 new location. Beacons calibration. Move to SHZ. Pressure
test KL, CL, Booster & Hyd Lines. RIW BOP f/ 911m to 954 m.
WellBore
Mud
Mud Type
Sea water
Density (...
1.030I also asked what you expected your output to be.Thank you

search result  should be in the result txt.

for  1.Well name, it should return text line minus the well name, hence it should return ACA-804 as seen in the search text
     2 Cumulative Cost, it should return text line minus the well name, hence it should return 768 098 900 as seen in the search text                                                                                                
     2.Mud Type it should return  stirng in the next line after Mud Type hence it will be sea Water.
     3. Density it should return string in the next line hence it will be 1,030
     5. Daily Summary  it should return  strings in the next 2lines.....

 there are multiple text files within the sub directory, so search will be done on all the text files.

Below is my current result.txt but like I said before my  return search string isn't complete

      Search Results For "Well"
C:\Reservoir Management\script\D.txt
C:\Reservoir Management\script\DD.txt
C:\Reservoir Management\script\DDD.txt
.
Search Results For "Midnight"
C:\Reservoir Management\script\D.txt
C:\Reservoir Management\script\DD.txt
C:\Reservoir Management\script\DDD.txt
.
Search Results For "Mud"
C:\Reservoir Management\script\D.txt
C:\Reservoir Management\script\DD.txt
C:\Reservoir Management\script\DDD.txt
.
Search Results For "Cumulative"
.
Search Results For "Daily"
C:\Reservoir Management\script\D.txt
C:\Reservoir Management\script\DD.txt
C:\Reservoir Management\script\DDD.txt
.Now you are confusing the heck out of me.  I thought you wanted the contents of the text files from your search to be output, not the file names.  I think I am going to TAP OUT on this one.  Hopefully someone ELSE will come along and be able to help you because I am not really understanding what you want.
Hello,

yes you are right, I want the contents of my search as an output.

Many Thanks,

MatildaUnfortunately, you have a fairly tall order you are trying to fill here. Your input does not neatly conform to your desired output, and the added hurdle of seperate lines makes things quite difficult. For one thing, you are definitely going to have a large amount of processing in the embedded FOR loop, including a lot of switches to turn on oand off so that you can accurately capture what you are wanting to capture. I'll see if I can put together something for you here:

Code: [Select]echo off
setlocal enabledelayedexpansion

for /f "delims=" %%A in ("c:\Reservoir Management\script\*.txt") do (
  set file=%%~dpnxA
  echo Daily Summary: >> sum.scrtmp
  REM set midnidep=0
  set mud=0
  set den=0
  set sum=0
  for /f "usebackq tokens=1* delims=:(" %%B in ("!file!") do (
    if /i "%%B"=="Well Name" (echo Well Name: %%C >> Wellname.scrtmp)
    if /i "%%B"=="Cumulative Cost" (echo Cumulative Cost: %%C >> CumulCost.scrtmp)
    if !mud! equ 1 (
      echo Mud Type: %%B >> mud.scrtmp
      set mud=0
    )
    if /i "%%B"=="Mud Type" (set /a mud=1)
    if !den! equ 1 (
      echo Density: %%B >> den.scrtmp
      set den=0
    )
    if /i "%%B"=="Density " (set /a den=1)
    if !sum! geq 1 (
      echo %%B >> sum.scrtmp
      set /a sum-=1
    )
    if /i "%%B"=="Daily Summary " (set /a sum=2)
    REM if !midnidep! equ 1 (
    REM   echo Midnight Depth: %%B >> midnidep.scrtmp
    REM   set midnidep=0
    REM )
    REM if /i "%%B"=="Midnight Depth" (set /a midnidep=1)
  )
  type Wellname.scrtmp >> temp.scrtmp
  REM type midnidep.scrtmp >> temp.scrtmp
  type mud.scrtmp >> temp.scrtmp
  type den.scrtmp >> temp.scrtmp
  type CumulCost.scrtmp >> temp.scrtmp
  type sum.scrtmp >> temp.scrtmp
  echo. >> temp.scrtmp
  type temp.scrtmp >> "c:\Reservoir Management\script\result.txt"
  del *.scrtmp
)

You will need to DELETE all of the "REM"s that you find in there if you are wanting the Midnight Depth in the result as well. Otherwise just delete the lines. The embedded code is setup like it is so that the processing happens like it should. Please let us know if you run into any issues. BTW, I tested this on the input that you gave and the result was this:


Well Name:  ACA-804
Midnight Depth: 25/09/2011
Mud Type: Sea water
Density: 1.030
Cumulative Cost:   768 098 900
Daily Summary:
Sailing to ACA-804 new location. Beacons calibration. Move to SHZ. Pressure
test KL, CL, Booster & Hyd Lines. RIW BOP f/ 911m to 954 m. Thanks Raven,

you are a life saver, it does work and I would like few more twiggs to it.

The code currently works for  the first text file in my directory, if you could get it to work for all the text files in that directory

see below search text;
Midnight depth
04/06/2011
04
1 302,10
DEV 1
1 924,00
Survey Data

The result is currently like
Well Name:  DAL753-4P
Midnight Depth: 03/06/2011
Mud Type: Spud mud
Density: 1,030
Daily Summary:
RIW 36" CP + DAT + PGB + BHA assy - Move ship, from SHZ to well center - Tag
sea
 I would like slight modification to the Mid night depth;
the next line string below the Midnight Depth: is actaually the date;
the Midnight Depth:should have the string above the survey data,
so in the result it should be

Well Name:  DAL753-4P
Date: 03/06/2011
Mud Type: Spud mud
Density: 1,030
Midnight Depth:1 924,00
Daily Summary:
RIW 36" CP + DAT + PGB + BHA assy - Move ship, from SHZ to well center - Tag
sea bed 1320 m/RT - Jet 36" CP to 1385 m/RT - Observe Soaking period -
Thank youQuick question for you, and an explanation to help you understand why. The question is "is the text following the midnight depth line standard or does it vary?" I.e. Is there a set number of lines that follow the midnight depth line before you actually get the true "midnight depth" figure you are LOOKING for? The reason I ask is because the program reads the text line by line. While it may seem simple to me and you to say, "the line above the survey data," the program no longer sees that line as existing at all. So I have to find something standard in a line above the midnight depth to look for instead of below it.Actually, I took some time, had a drink, and everything came to me (alcohol isn't always a bad thing!!!) In order to get your "last line" to work, all we have to do is have a lastline variable and set it appropriately. This will only work though if the midnight depth value ALWAYS resides in the line above the survey data. See minor changes below:

Code: [Select]echo off
setlocal enabledelayedexpansion

for /f "delims=" %%A in ("c:\Reservoir Management\script\*.txt") do (
  set file=%%~dpnxA
  echo Daily Summary: >> sum.scrtmp
  set mud=0
  set den=0
  set sum=0
  for /f "usebackq tokens=1* delims=:(" %%B in ("!file!") do (
    if /i "%%B"=="Well Name" (echo Well Name: %%C >> Wellname.scrtmp)
    if /i "%%B"=="Cumulative Cost" (echo Cumulative Cost: %%C >> CumulCost.scrtmp)
    if !mud! equ 1 (
      echo Mud Type: %%B >> mud.scrtmp
      set mud=0
    )
    if /i "%%B"=="Mud Type" (set /a mud=1)
    if !den! equ 1 (
      echo Density: %%B >> den.scrtmp
      set den=0
    )
    if /i "%%B"=="Density " (set /a den=1)
    if !sum! geq 1 (
      echo %%B >> sum.scrtmp
      set /a sum-=1
    )
    if /i "%%B"=="Daily Summary " (set /a sum=2)
    if /i "%%B"=="Survey Data" (echo Midnight Depth: !lastline! >> midnidep.scrtmp)
    set lastline=%%B
  )
  type Wellname.scrtmp >> temp.scrtmp
  type midnidep.scrtmp >> temp.scrtmp
  type mud.scrtmp >> temp.scrtmp
  type den.scrtmp >> temp.scrtmp
  type CumulCost.scrtmp >> temp.scrtmp
  type sum.scrtmp >> temp.scrtmp
  echo. >> temp.scrtmp
  type temp.scrtmp >> "c:\Reservoir Management\script\result.txt"
  del *.scrtmp
)
Thank you Raven,

Results are exactly what I want. except now the Cumulative Cost isn't working now. Also, I want the code to read all the text files in the directory ("c:\Reservoir Management\script\*.txt"). At the moment, its just the first text.

Thanks for your help and time , guess Santa payed the visit after all... Hi Raven,

the reason why cumulative cost is not picked is because within the text there are spaces;
 Cumulative Cost : 2 581 456; it does work if spaces were removed such as Cumulative Cost: 2 581 456


Matilda



Discussion

No Comment Found