Try this:
set test=BE99012345678901
if "%test:~0,4%"=="BE99" (
echo This number begins with BE99
) else (
echo This number doesn't begin with BE99
)
pause
You will get following output:
This number begins with BE99
Kindly note:
if %test% == "BE99????????????"
will do this:
"BE99012345678901" == "BE99????????????"
and this will show following output:
This number doesn't begin with BE99
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.
Best Answer
You have a couple problems:
%var%
expansion occurs when the statement is parsed, and the entire parenthesized code block is parsed in one pass, before any commands are executed. So the value is the value that existed before the loop was started. The solution is delayed expansion, which occurs as each command within the loop is being executed.Your logic is wrong - the assignment of _newp should be based on the value of _srcp
The CMD processor is a complicated beast (and also poorly documented). There are multiple points where various types of variables are expanded, and you must fully understand them if you truly want to make the most of batch programming. It is all explained in the link, but in summary, the order of expansion is:
1) % expansion - Parameter:
echo %1
or Environment variable:echo %var%
---- Most parsing has completed by now ----
2) FOR variable expansion:
for %%A in (*) do echo %%A
3) Delayed environment variable expansion:
echo !var!
4) CALL % expansion - Parameter:
call echo %%1
or Environment variable:call echo %%var%%
5) SET /A environment variable expansion: `set /a "value=var+1"
Note that delayed expansion requires delayed expansion to be enabled via
SETLOCAL EnableDelayedExpansion
The following code using delayed expansion will give the result you seek:
Note that delayed expansion occurs after FOR variable expansion, so the result will be corrupted if
%%G
constains!
. This can be avoided by additional SETLOCAL:You could also get the desired result using CALL with double percents, but this is much slower. The speed is not important if executed a few times, but it becomes very significant if executed thousands of times in a loop.