NewQueries
NewQueries

Reputation: 4931

.bat - check for a string in file

I have a xml files filles with tabs and spaces. I am trying to search for a string in this file. File is something like below. I am trying to search for </ViewSettings> and this surrounded by tabs and spaces.

<ViewSettings>
  <Location>
  <X>0</X>
  <Y>0</Y>
  </Location>
</ViewSettings>
<WorkspaceName="FREE_UST_BETA_UA" PAth="\\mktxindfs\data_configuration\FREE_BETA"      IsAdmin="false" />
</Workspaces>

I have the code below

echo off
setlocal enabledelayedexpansion

for %%X in (C:\add\WorkspaceXML\Workspaces.xml) do (
set "reference=</ViewSettings>"
for /f "delims=" %%T IN (%%X) do (  
        set output=%%T
        echo output:!output!
        if !output!==!reference! echo found reference.....
    )
)

It does not print "found reference"

Thanks

Upvotes: 0

Views: 781

Answers (1)

Adam S
Adam S

Reputation: 3125

Have you considered building your batch file off of findstr instead? This command would tell you if the string was in the file by reporting the filename.

findstr /M /c:"</ViewSettings>" C:\add\WorkspaceXML\Workspaces.xml

EDIT

If you use findstr /N /O ... instead, you can get the line number and offset of the match(es), maybe that will be of more use to you. The output in your case above would be

6:69:</ViewSettings>

EDIT 2

Proper offset added above thanks to dbenham. Not sure if the offset is still of use to you, but to get both values in vars, try this:

FOR /F "tokens=1,2 delims=:" %%a in ('findstr /N /O /c:"</ViewSettings>" C:\add\WorkspaceXML\Workspaces.xml') do echo %%a %%b 

This just displays the vars for you of course, but you can set them as needed.

Upvotes: 2

Related Questions