How to step through a file using a batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
albert_newton
Posts: 4
Joined: 23 Jul 2009 15:42

How to step through a file using a batch file?

#1 Post by albert_newton » 23 Jul 2009 15:48

I am trying to create a batch file that steps through a file until it finds a particular line. When it finds the line, it writes a few key words from that line into a new file. I was wondering how this can be done using a batch file? So, how can I step through the lines of a file until I hit the line I am looking for?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 23 Jul 2009 20:12

show examples, of your input file as well as what you want to get.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 23 Jul 2009 21:22

The general concept would have you use a for loop to step through the individual lines, and then use find to compare each line to a specific keyword or phrase to identify the line you want, and then you can stick that in another file. You will probably have to use another for loop if you only want part of that line.


untested

Code: Select all

for /f "tokens=*" %%a in (yourfile.txt) do (
   echo.%%a|find /i "some string that will identify the part you're looking for"
   if not errorlevel 1 (
      echo You found the line.
      echo.%%a>>yourotherfile.txt
   )
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#4 Post by ghostmachine4 » 23 Jul 2009 21:33

actually, the more efficient way is input the file to find, instead of piping each line to find.

Code: Select all

for .. "tokens..delimit....." ('find "searchstring" yourfile.txt' ) do(
  if found do something...
)

albert_newton
Posts: 4
Joined: 23 Jul 2009 15:42

#5 Post by albert_newton » 24 Jul 2009 09:28

I was able to search for the lines containing the string " stop trying to browse through it", and writing it to the TestFile.txt by doing as follows:

TYPE C:\BatchTest\httpd.conf | Find "stop trying to browse through it" > C:\BatchTest\TestFile.txt...

now i need to add some more complications to it...
1.) I need to extract only some part of the line, and put it in the file...
2.) I need to put it in the file only if it does not already exist in the file.


To make things less abstract let me give an example of what I am trying to do:

I have a file that I need to look through to extract all values after the Name tag, and put it in a different file if it does not already exist in that file. The lines within the file I am searching through look like this:


<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot1/*"/>
<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot2/*"/>
<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot3/*.jsp"/>



So I need to look for lines that contain the following text:

<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name=

then only extract the value after the 'Name =' tag


so in this case I need /contextroot1/*, /contextroot2/* and /contextroot3/*.jsp. Then I need to put these values in a file TestFile.txt if that file does not already contain these values.


So for now i am able to just get all the lines containing the text:

<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name=

but not sure how can I extract the value after Name=
and store it in the file TestFile.txt

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#6 Post by ghostmachine4 » 24 Jul 2009 20:41

there are many good tools for parsing text files, eg gawk, perl,python. Batch is not suited for that, although it still can be used, but, with much effort.
Otherwise, you can also use vbscript, which, is installed by default. here's an example, using regular expression. Of course, it could also be done using simple string handling.

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile =  "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
strFileContents = objFile.ReadAll
Set objRE = New RegExp
objRE.Pattern = ".*Uri AffinityCookie.*AffinityURLIdentifier.*Name=" & """" & "(.*)" &"""" & ".*/>"
objRE.Global     = True
objRE.IgnoreCase = False
objRE.MultiLine = True
strRe = objRE.Replace(strFileContents,"$1")
strFound = Split(strRe,vbCrLf)
For i=LBound(strFound) To  UBound(strFound)
   WScript.Echo  strFound(i)
Next
objFile.Close
Set objFile=Nothing


output

Code: Select all

C:\test>cscript /nologo test.vbs
/contextroot1/*
/contextroot2/*
/contextroot3/*.jsp

albert_newton
Posts: 4
Joined: 23 Jul 2009 15:42

#7 Post by albert_newton » 30 Jul 2009 13:28

Unfortunately I need to use batch programming, since the place where this stuff is going to be deployed should not have to install anything like Perl, Jython, etc to be able to run the script. Also, it would also be used with unix, and so can't use VBScript.

So, I have to continue using the batch file approach. I am still stuck with not being able to read a particular string value from a line in the file as mentioned in my previous post.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

hello

#8 Post by rfpd » 30 Jul 2009 17:09

i don't now if his this what you want sorry i am portuguese but you can use findstr type help findstr in cmd.

[/code]

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#9 Post by ghostmachine4 » 30 Jul 2009 18:35

albert_newton wrote:Also, it would also be used with unix, and so can't use VBScript.

what makes you think that writing in batch can be used in Unix?

albert_newton
Posts: 4
Joined: 23 Jul 2009 15:42

#10 Post by albert_newton » 03 Aug 2009 13:16

ghostmachine4 wrote:
albert_newton wrote:Also, it would also be used with unix, and so can't use VBScript.

what makes you think that writing in batch can be used in Unix?


You got me on that one,lol :)... I gather they are going to do something special for the Unix environment... but I do need to use batch file to be able to achieve this in Windows... I made some progress but am stuck on some part of it.

I was able to extract the lines containing the context roots and store them in a new file, and was able to extract the value after the name tag from those lines. For example, I was able to extract the string:
contextroot1/*"/>


Now, I want to be able to just extract the word contextroot1

(in other words, I want to be able to extract all the characters before the first / occurs).



I know this should be easily done with regular expressions, but I do not have a lot of experience with them, and am having difficulty incorporating it in the batch file. This is the batch file I have so far:


@ echo off
setLocal EnableDelayedExpansion

TYPE C:\BatchTest\plugin-cfg.xml | Find "AffinityURLIdentifier=""jsessionid""" > C:\BatchTest\TestFile.txt

for /f "tokens=4" %%a in (C:\BatchTest\TestFile.txt) do (

set myvar=%%a

set finalvar=!myvar:~7,100!

echo !finalvar!



What the above code is doing is coying all the lines containing the text

"AffinityURLIdentifier=""jsessionid" (which contain the context root value) to a new file named TestFile.txt. Then, they copy from 7th ( character or token?) to the remainder of the sentence to get: contextroot1/*"/>

I need to be able to get just contextroot1

ddddyyyy
Posts: 1
Joined: 04 Aug 2009 21:21

#11 Post by ddddyyyy » 05 Aug 2009 19:57

Oh, my dear,I miss you very much,I think everything will bebetter,Good things!

A man becomes learned by asking questions.








air Jordan Shoescheap wedding dressesgucci bagscheap handbagsgucci shoes

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#12 Post by avery_larry » 06 Aug 2009 11:54

Code: Select all

for /f "tokens=2 delims=^/" %%a in (C:\BatchTest\TestFile.txt) do set "finalvar=%%a"

You can combine that with the original for loop instead of dumping the lines to a tmp file first. Something like this:


Code: Select all

for /f "tokens=2 delims=^/" %%a in ('TYPE C:\BatchTest\httpd.conf ^| Find "stop trying to browse through it"') do set "finalvar=%%a"

Post Reply