How to step through a file using a batch file?
Moderator: DosItHelp
-
- Posts: 4
- Joined: 23 Jul 2009 15:42
How to step through a file using a batch file?
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?
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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
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
)
)
-
- Posts: 319
- Joined: 12 May 2006 01:13
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...
)
-
- Posts: 4
- Joined: 23 Jul 2009 15:42
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
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
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.
output
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
-
- Posts: 4
- Joined: 23 Jul 2009 15:42
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.
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.
hello
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]
[/code]
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Posts: 4
- Joined: 23 Jul 2009 15:42
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
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
A man becomes learned by asking questions.
air Jordan Shoescheap wedding dressesgucci bagscheap handbagsgucci shoes
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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"