FindStr doesn't work correctly

I made a piece of batch code and I thought it would work. What do I think about what this code does? I have plugins and I want to test if the deployment is correct. So I am getting the plugin from the plugins.txt file. Then I get a plugin from SVN with a java suggestion. I am deploying a plugin and getting feedback in test1.txt. Then I do findStr in this file and looks for "BUILD SUCCESSFUL", if there is one, I want to add Build Gelukt suggestion and if it doesn't work I want to add Build Fout. But I always get a Build Gelukt response, and as you can see in the image, it sends back that the build failed.

What's wrong with this piece of code?

for /f "tokens=* delims= " %%a in (plugins.txt) do (
echo %%a
cd "C:\dotCMS Automatic Install"
java -cp .;"C:\dotCMS Automatic Install\svnkit.jar" Test %%a
cd %dotcms_home%
call ant deploy-plugins > test1.txt
FindStr "SUCCESSFUL" test1.txt
if %ERRORLEVEL% ==1 (echo ^<tr BGCOLOR=\"#FFFFFF\"^>^<td^>%%a^</td^>^<td^>Build Fout^</td^>^</tr^> >> C:\dotCMSResults\goedje.html ) else (echo ^<tr BGCOLOR=\"#00FF00\"^>^<td^>%%a^</td^>^<td^>Build Gelukt^</td^>^</tr^> >> C:\dotCMSResults\goedje.html) 
del test1.txt
rem call ant undeploy-plugins >> test.txt
)

      

enter image description here

+3


source to share


2 answers


The classic package problem is you set ERRORLEVEL and try to access it using %ERRORLEVEL%

the same clause DO()

. Expansion %VAR%

occurs during parsing and the entire statement is FOR ... DO()

parsed once, so you see the ERRORLEVEL value before the statement is executed. Obviously this won't work.

jeb referred to the answer in his comment regarding disappearing quotes. Your problem will be fixed if you are setlocal enableDelayedExpansion

at the top then use !ERRORLEVEL!

instead %ERRORLEVEL%

. Also, GregHNZ is correct in that the ERRORLEVEL test should happen immediately after your FINDSTR statement.

There are other ways to handle ERRORLEVEL in parentheses that do not require deferred expansion:

Following tests if ERRORLEVEL is greater than or equal to 1



IF ERRORLEVEL 1 (...) ELSE (...)

      

And below conditionally executes commands based on the results of the previous command

FindStr "SUCCESSFUL" test1.txt && (
  commands to execute if FindStr succeeded
) || (
  commands to execute if prior command failed.
)

      

+10


source


The variable %ErrorLevel%

only applies to the previous command only.

So when you do this:

echo Errorlevel: %ERRORLEVEL%

      



With your current code, you are getting the command error level CD

higher

Try putting the line if %ERRORLEVEL% ==1

right after the command FindStr

and then doing del and cd. Obviously, you will need to put the full path to the html file in your echo statement.

+2


source







All Articles