3

I made a piece of batch-code, and I thought this will work. What I'm thinking that this code is doing? I have some plugins and I want to test if the deploy correct. So I get the pluginlink from the plugins.txt. Then I get the plugin from SVN with the java sentence. I deploy the plugin and get the feedback in test1.txt. Then I do a findStr in that file and searchs for "BUILD SUCCESSFUL" if it is there I want to add the sentence Build Gelukt and if it fails I want to add Build Fout. But I get always the answer Build Gelukt, while as you can see in the image he sends back that the build is Failed.

Whats 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

Danny Gloudemans
  • 2,597
  • 7
  • 39
  • 57
  • Btw. For simpler echoing html code you could use [the disappearing quotes technic](http://stackoverflow.com/a/7308647/463115). Like `echo !"=! %%aBu...` – jeb Mar 23 '12 at 08:32
  • @jeb that doesn't work for me =O I still get the !"! of ! in the html file. – Danny Gloudemans Mar 23 '12 at 09:26
  • Ok, I forgot to mention that you need to enable the delayed expansion before, with `setlocal EnableDelayedExpansion`. – jeb Mar 23 '12 at 09:31

2 Answers2

14

Classic batch problem - you are setting your ERRORLEVEL and attempting to access it using %ERRORLEVEL% within the same DO() clause. %VAR% expansion happens at parse time, and the entire FOR ... DO() statement is parsed once, so you are seeing the value of ERRORLEVEL before the statement was executed. Obviously that won't work.

jeb alluded to the answer in his comment regarding disappearing quotes. Your problem will be fixed if you setlocal enableDelayedExpansion at the top, and then use !ERRORLEVEL! instead of %ERRORLEVEL%. Also, GregHNZ is correct in that the ERRORLEVEL test should occur immediately after your FINDSTR statement.

There are other ways to handle ERRORLEVEL within parentheses that don't require delayed expansion:

The following tests if ERRORLEVEL is greater than or equal 1

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

And below conditionally executes commands based on the outcome of the prior command

FindStr "SUCCESSFUL" test1.txt && (
  commands to execute if FindStr succeeded
) || (
  commands to execute if prior command failed.
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

The %ErrorLevel% variable applies to the immediately previous command only.

So when you do this:

echo Errorlevel: %ERRORLEVEL%

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

Try putting your if %ERRORLEVEL% ==1 line immediately after the FindStr command, and then do the del and the cd afterward. Obviously you'll need to put the full path to the html file in your echo statement.

GregHNZ
  • 7,946
  • 1
  • 28
  • 30
  • I changed the code, and set it in the first post now.. But it still doesn't work.. I also did for testing only a Echo %ERRORLEVEL% immediately after the findStr sentence. But he always put out the 0. While the text in the command-line said it deploying failed.. – Danny Gloudemans Mar 23 '12 at 08:42
  • @GregHNZ: %ERRORLEVEL% is not giving the error level of the CD command. It is giving the value before the FOR statement was parsed! But you are correct that the error level test should occur immediately after the FINDSTR statement. – dbenham Mar 23 '12 at 11:37