-1

I am trying to create a DOS batch script on my Windows 7 machine. I want to execute file 123456.bat, which contains the following command and parameters:

call **startSelenium.bat** 55 someSuiteName suite-someSuite.html development 1 1 10 10

That script calls into the startSelenium.bat script below:

Setlocal EnableDelayedExpansion

SET TimeStamp= 
FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do set datestr=%%l%%j%%k
FOR /f "tokens=1-4 delims=.: " %%i in ("%time%") do set timestr=%%i%%j%%k%%l
SET TimeStamp=%datestr%%timestr%

set a=2
set b=0
set /a c=%a%+%b%

FOR /l %%t IN (1, 1, %c%) do (
  call :SEARCHPORT %%t
  echo %startPort%
  Start java -jar C:\Selenium\2\selenium-server-standalone-2.47.1.jar -port %startPort% -singleWindow -userExtensions C:\selenium\2\user-extensions.js -firefoxProfileTemplate "c:\selenium\2\ffprofiles\2rc" -htmlSuite "*chrome" "https://www.google.com" "Z:\selenium\2\environment\%4\suites\%3" "u:\results\%4\result-%1-%computername%-1234-%TimeStamp%.htm"  
  timeout /t 10
)
GOTO :EOF

:SEARCHPORT 

netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL
if "%ERRORLEVEL%" equ "0" (
  set /a startPort +=1
  GOTO :SEARCHPORT
) ELSE (
  set freePort=%startPort%
  echo %startPort%

GOTO :EOF

When I run the script, the first instance of the java applications runs with a free Windows port that the SEARCHPORT subroutine found; however, the second instance pops up and quits immediately. I suspect the code is using the same variable from the first time it went through the FOR loop instead of getting a new unused port number.

What am I doing wrong? I copied various parts of this code from other sources. I am obviously a nube, so plain English would be helpful. :)

lalbright
  • 3
  • 2
  • 3
    Please edit your code to make it readable. According to your description, that sounds like a [delayed expansion](http://stackoverflow.com/a/30284028/2152082) problem. – Stephan Apr 05 '16 at 20:36
  • Are you really still working with **MS-DOS**? I don't think so; please check the tags and adapt accordingly... – aschipfl Apr 05 '16 at 20:48
  • 1
    To further what Stephan said - you entered your code as quoted text, making it totally unreadable. We have no way of knowing where the line breaks are, and line breaks are critical in batch scripts. Edit your question and delete the quotes (yellow section) and re-enter the code, but use the code button (curly braces). The code will be gray if you did it correctly. – dbenham Apr 05 '16 at 21:46
  • @dbenham ... thx for being patient and specific since I am new. Much appreciated. – lalbright Apr 06 '16 at 19:40
  • I have also tried pulling out the searchPort subroutine as a separate batch file and calling it with: call searchPort %startPort% as the first line after the start of the FOR loop. Of course, not knowing what I am doing, it did not work. – lalbright Apr 06 '16 at 19:44
  • Have you read my [link](http://stackoverflow.com/a/30284028/2152082)? You already enabled delayed expansion, but you don't use it. Replace `%startPort%` with `!startPort!` inside your `for /l` loop. – Stephan Apr 06 '16 at 20:14
  • Yes, that was it Stephan. TY. However, I do not see a green checkmark by your answer to give you the credit (and yes I am logged in). I read this article: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – lalbright Apr 06 '16 at 20:58

1 Answers1

0

You have got an delayed expansion issue.

You already enabled delayed expansion, but you don't use it. You have to replace %startPort% with !startPort! inside your for /l loop.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91