Solved Where search match found write following line as well

January 23, 2013 at 02:18:36
Specs: Windows Vista, 1.6 gb 4gb
I have a file which holds lines of text. One line of text indicates a file's location the following line holds a description of the file. I want to be able to search the file using "the directory" as the keyword and where a match is found extract the following line also so I have both the file location and description. For example by searching for one of the directories such as "/s/" I would be able to identify all the files which are in it along with their descriptions. The script below lets me identify all the directory matches but I am stumped as to how I can extract the line following the match so that I have the description as well. There is no way I can search for the descriptions as they are unique.

Any help on how I might achieve this would be appreciated

Thank you


[code]
Const ForReading = 1

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "./s/"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("F:\H&S_Safety_Matters\H&S_Items.txt", ForReading)

Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString

Next
End If
Loop

objFile.Close
[/code]


See More: Where search match found write following line as well

Report •


✔ Best Answer
January 23, 2013 at 06:43:32
:: ===== script starts here ===============
:: extract found line and next line
:: PAIRS.bat 2013-01-23 17:58:34.68
@echo off > newfile & setLocal enableDELAYedeXpansioN

set X=
for /f "tokens=1 delims=[]" %%a in ('find /n "apple" ^< myfile') do (
set/a L=%%a
set/a N=!L!+1
set X=!X! !L! !N!
)

for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
for %%N in (!X!) do (
if %%a equ %%N echo %%b
)
) >> newfile

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle



#1
January 23, 2013 at 03:28:24
clunky but useable

:: ===== script starts here ===============
:: extract found line and next line
:: nextline.bat 2013-01-23 17:58:34.68
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=1 delims=[]" %%a in ('find /n "Scripting" ^< myfile') do (
set/a L=%%a
)
set /a N=!L!+1

for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
if %%a equ !L! echo %%b
if %%a equ !N! echo %%b
) >> newfile

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
January 23, 2013 at 04:33:53
or:

:: ===== script starts here ===============
::
:: nextli2.bat 2013-01-23 19:23:38.98
@echo off > newfile & setLocal enableDELAYedeXpansioN

set F=
for /f "tokens=* delims= " %%a in (myfile) do (
if defined F (
>> newfile echo %%a
goto :done
)

echo %%a | find "Scripting" > nul

if not errorlevel 1 (
>> newfile echo %%a & set F=Y
)

)
:done
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
January 23, 2013 at 05:06:37
Thank you for the batch files. I have found however that both of them only identify the first match. The first version shows a blank output file where the first few lines of the info file are blank. The second batch file however, doesn't do that but as I said only identifies the first match.

I should have said that although no two descriptions will be identical there would be multiple matches of the file locations because many of them, though having different names, share the same directory.

In case I have done something wrong I have attached a copy of the second batch file?

Thank you for your assistance

PS It doesn't make any difference but apologies for the typo - the search string is "\s\" not ".\s\" as in my original script

[code]
:: ===== script starts here ===============
::
:: nextli2.bat 2013-01-23 19:23:38.98
@echo off > SampleTxtOut.txt & setLocal enableDELAYedeXpansioN

set F=
for /f "tokens=* delims= " %%a in (Sample.txt) do (
if defined F (
>> SampleTxtOut.txt echo %%a
goto :done
)

echo %%a | find "\s\" > nul

if not errorlevel 1 (
>> SampleTxtOut.txt echo %%a & set F=Y
)

)
:done
::====== script ends here =================
[/code]


Report •

Related Solutions

#4
January 23, 2013 at 05:33:32
I'll re-do it to accomodate multi found lines.

=====================
M2 Golden-Triangle


Report •

#5
January 23, 2013 at 06:43:32
✔ Best Answer
:: ===== script starts here ===============
:: extract found line and next line
:: PAIRS.bat 2013-01-23 17:58:34.68
@echo off > newfile & setLocal enableDELAYedeXpansioN

set X=
for /f "tokens=1 delims=[]" %%a in ('find /n "apple" ^< myfile') do (
set/a L=%%a
set/a N=!L!+1
set X=!X! !L! !N!
)

for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
for %%N in (!X!) do (
if %%a equ %%N echo %%b
)
) >> newfile

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#6
January 23, 2013 at 08:06:38
Fantastic it works perfectly! It did occur to me that it would have made more sense to have the two lines output merged onto the same line but despite trying out a few suggestions I was unable to make any of them work for me. Maybe it is because I am still using Vista!

Any way, I appreciated your help - you've been "pure dead brilliant"!!

Thank you


Report •

#7
January 23, 2013 at 08:15:01
In keeping with the original language:
Const searchFor = "\s\"
Const filePath = "F:\H&S_Safety_Matters\H&S_Items.txt"

Set file = CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath) 
Do Until file.AtEndOfStream
  line = Trim(file.ReadLine)
  If InStr(1, line, searchFor, 1) Then _
    WScript.Echo line & vbNewLine & Trim(file.ReadLine)
Loop

How To Ask Questions The Smart Way


Report •

#8
January 23, 2013 at 10:26:16
Thank you Razor for taking the time to respond even after a solution was posted. It never fails to amaze me how there are so many ways to achieve the same result! I have an awful lot to learn about VBS and I hope, one day, that I'll feel I've managed to get more than a nodding acquaintance with it!

Thank you again


Report •

#9
January 23, 2013 at 14:47:25
single-line implementation of M2's work (starting at part 2):
set out=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
  for %%N in (!X!) do (
    if %%a equ %%N (
      if defined out (
        echo %%b !out!
        set out=
      ) else (
        set out=%%b
      )
    )
  )
) >> newfile
::===== end

i don't think it's supposed to work, but for some reason it seems to.
For Razor's vbs, all you need is to remove the "& vbnewline" from the output.

Report •

#10
January 27, 2013 at 04:30:51
Razor,

I know this thread is considered solved and I am happy with the batch file solution. However, I still haven't been able to get your VBS suggestion to work. I know I made a mistake with the search string which should have been "/s/" or ("//s//" for my original script). However, when I run your script nothing happens and run from the command line the output file is empty. Is your script standalone or am I missing something?

Bear with me as I only ask because I like to figure out how scripts work so I'll be able to do it myself in future.

Thank you


Report •

#11
January 27, 2013 at 05:33:00
Change this line: Const searchFor = "\s\" to be whatever you need.

How To Ask Questions The Smart Way


Report •

#12
January 27, 2013 at 06:16:31
Razor,

Thank you for you prompt response. I had changed the search string to a word I could see was in the file but it made no difference and the script could not identify it. However, I have solved the mystery. The file that I was trying to search through was saved as a Unicode file so I saved it as UTF-8 and when I used the script to search again it found it immediately.

Can you clarify something for me. The file I was searching through was the text file output from a VBS script. Does that mean all vbs created files are in Unicode format and if so should I resave them as Ansi or UTF-8 before using them in another VBS script?

I must be getting to be a pain in the ass!

Thank you


Report •

#13
January 27, 2013 at 06:59:31
The TextStream objects created by FileSystemObject can be ASCII or UTF-16. If you don't specify, ASCII is assumed. If you wanted to read a UTF-16 file in this script, you'd use this line:
Set file = CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath, 1, False, -1)

If the script upstream is writing file names out, it's typical to make your output UTF-16 because NTFS is UTF-16, and an ASCII TextStream will choke on Unicode only characters (like ☃).

Personally, I suggest you just modify the other script to give you the output you want, instead of having to run two scripts.

How To Ask Questions The Smart Way


Report •


Ask Question