One of the main problems in your logic is that you set the "import" variable once if something is true, but never reset it, or undefined it if it's not true anymore. So after the first time it's set, it will remain "defined" for the rest of the loop.
My preference is to set the variable specifically (true
or false
). Set it to false
to start, then set it to true
as wanted, but then also ensure you set it back to false
when you need to. Then, at each iteration of the loop check if the variable is specifically set to True
or False
instead of checking if it's just defined.
This code works for me based on your info/goals:
@echo off
setlocal EnableDelayedExpansion
set buildlog=test.txt
set import=false
for /F "tokens=*" %%A in (%buildlog%) do (
if /i "%%A"=="Import:" (
set import=true
)
if /i "%%A"=="View:" (
set import=false
)
if !import!==true (
if not "%%A"=="Import:" (
for /F "tokens=1" %%B in ("%%A") do (
echo %%B
)
)
)
)
We purposefully set the "Import" variable (flag) to false
to start.
First For-loop goes through each line in the file (test.txt
in this example, as specified by the "buildlog" variable; note: you need to remove the quotes around the file name in the variable for it to work in the For-loop).
The first IF sees if the current line is the "Import:" line, and if so, it flips the "import" flag to true
.
The next IF sees if the current line is the "View:" line, and if so, it flips the "import" flag (back to) to false
, so that it stops processing each line.
The 3rd IF checks if that "import" flag is true
, and if so, it processes the line.
If it's true
then the nested (4th) IF then checks if the line is the actual "import:" line, and if not, displays the line (keeps it from showing the "Import:" line in the output).
The second For-loop goes through the line we want displayed, and pulls only the first Token set which is (just) the path you want, leaving off the #head
.
More/related info:
Edit after comments:
To deal with the "View:" line if it has a version number after it you could modify the code to something like:
@echo off
setlocal EnableDelayedExpansion
set buildlog=test.txt
set import=false
for /F "tokens=*" %%A in (%buildlog%) do (
for /F "tokens=1" %%B in ("%%A") do (
if /i "%%B"=="Import:" (
set import=true
)
if /i "%%B"=="View:" (
set import=false
)
if !import!==true (
if not "%%B"=="Import:" (
for /F "tokens=1" %%C in ("%%A") do (
echo %%C
)
)
)
)
)
This added For-loop will pull the first token off the line to check if it's "View:" or "Import:" instead of checking the whole line. Effectively ignoring anything after the first space it encounters on the line to do the check.
For clarity's sake, what's wrong with what you've tried? What results do you get with it (right or wrong)? – Ƭᴇcʜιᴇ007 – 2015-05-01T17:38:42.770
result was wrong.. it is printing everything under Import:.. i would only like //MPackages/Project/config/abc.txt and //Packages/Project/config/cde.txt to be printed out.. – Mihir – 2015-05-01T17:48:03.923
Add the result you want to get – STTR – 2015-05-01T17:53:40.240
added to original question – Mihir – 2015-05-01T17:56:19.767