views:

233

answers:

2

Hi

Battling with a windows command line script I just can't get to work.

Basically I am trying to launch a program called vnctv.exe with the parameters of HOST ipaddress PORT 5900 PASSWORD x, however I only want to run the program with IP addresses of computers currently online.

I've tried a few different things but I cant get anything to work properly.

It seems when I run cmd and type in the commands individually they all work correctly but when I run it in a .bat file it fails.

I have tried enabledelayedexpansion so I wouldn't need to call but I couldn't get that to work.

The for loop first loops through 30-255 addresses. The next loop pings the IP and finds the loss rate. If it's 0 then I need to add that address to a run parameter for vnctv.exe. I've tried echoing to file then removing the \n characters but can't get that to work either. So the script should add the parameter to a variable but it fails.

here it is

set _megga=vnctv.exe

FOR /L %%A IN (253,1,255) DO (

    FOR /F "tokens=2 delims=(%%" %%B 
     IN ('PING -w 500 -n 1 91.40.20.%%A -w 500 -n 1 ^|find "loss"')
     DO ( if %%B EQU 0 call :exec ))
 )

:exec

set %%_megga=%_megga% HOST 91.40.20.%%A PORT 5900 PASSWORD x

goto :EOF

echo %%_megga > run.bat
run.bat

and heres the output

Z:\>set _megga=vnctv.exe

Z:\>FOR /L %A IN (253 1 255) DO (FOR /F "tokens=2 delims=(%" %B IN ('PING -w 500
 -n 1 91.40.20.%A -w 500 -n 1 |find "loss"') DO (if %B EQU 0 call :exec  ) )

Z:\>(FOR /F "tokens=2 delims=(%" %B IN ('PING -w 500 -n 1 91.40.20.253 -w 500 -n
 1 |find "loss"') DO (if %B EQU 0 call :exec  ) )

Z:\>(if 0 EQU 0 call :exec  )

Z:\>set %_megga=vnctv.exe HOST 91.40.20.%A PORT 5900 PASSWORD x

Z:\>goto :EOF

Z:\>(FOR /F "tokens=2 delims=(%" %B IN ('PING -w 500 -n 1 91.40.20.254 -w 500 -n
 1 |find "loss"') DO (if %B EQU 0 call :exec  ) )

Z:\>(if 0 EQU 0 call :exec  )

Z:\>set %_megga=vnctv.exe HOST 91.40.20.%A PORT 5900 PASSWORD x

Z:\>goto :EOF

Z:\>(FOR /F "tokens=2 delims=(%" %B IN ('PING -w 500 -n 1 91.40.20.255 -w 500 -n
 1 |find "loss"') DO (if %B EQU 0 call :exec  ) )

Z:\>(if 100 EQU 0 call :exec  )

Z:\>set

%_megga=vnctv.exe HOST 91.40.20.%A PORT 5900 PASSWORD x

_megga=vnctv.exe

Z:\>set %_megga=vnctv.exe HOST 91.40.20.%A PORT 5900 PASSWORD x

Z:\>goto :EOF

When I set it should read 91.40.20.254 for instance instead of %A and also it's making 2 different variables

Thanks for any Help

Adam

tried this but still getting %1 or 1 instead of the actual value

 set _megga=vnctv.exe
 FOR /L %%A IN (253,1,255) DO (
 FOR /F "tokens=2 delims=(%%" %%B IN 
 ('PING -w 500 -n 1 91.40.20.%%A -w 500 -n 1 ^|find "loss"') 
 DO ( if %%B EQU 0 call :exec %%A))

 :exec
 set %%_megga=%%_megga HOST 91.40.20.%%1 PORT 5900 PASSWORD x
 goto :EOF
+1  A: 

try escaping %:

"tokens=2 delims=(%%"

Edit: to reflect comment Also note that that the inner for loop's variables can clash with the the outers. When %G is set, the other tokens after G (%h & %i) may be overwritten. I suggest you change %i in the outer loop to be %a.

Preet Sangha
Yep that got it thankshowever now I have new problemsset _megga=vnctv.exeFOR /L %%I IN (253,1,255) DO (FOR /F "tokens=2 delims=(%%" %%G IN ('PING -w 500 -n 1 91.40.20.%%I -w 500 -n 1 ^|find "loss"') DO ( if %%G EQU 0 call :exec ))set:execset %%_megga=%_megga% HOST 91.40.20.%I PORT 5900 PASSWORD xgoto :EOF
Adam Hill
the variable %%I is just echoing "I" and the _mega variable is only being overwritten instead of updated
Adam Hill
add that in the question.
Preet Sangha
edited main post
Adam Hill
+1  A: 

The initial complaint that your cmd processor is throwing out is that you dont seem to end the tokens=2 delims=(% options string. This is because you are using the special % in your delim set. Try putting a double % there:

"tokens=2 delims=(%%"

Here is a different strategy to approach your problem:

setlocal enabledelayedexpansion 
set _megga=vnctv.exe
echo echo running %_megga% batch > run.bat
FOR /L %%A IN (253,1,255) DO (

    FOR /F "tokens=2 delims=(%%" %%B IN ('PING -w 500 -n 1 91.40.20.%%A -w 500 -n 1 ^|find "loss"') DO ( 
          if %%B EQU 0 set _megga=!_megga! HOST 91.40.20.%%A PORT 5900 PASSWORD x ))
 )
echo %_megga% >> run.bat
run.bat

I have taken the gist of your loops, but instead of jumping out to a label, I concatenate the command in the loop and later echo it to the run.bat which can then be run at the end. You will note that I employ the setlocal enabledelayedexpansion and use ! to replace % in the loop.

akf
Thankyou this does work however it launches the application several times where as I need to launch the application only once and include all parameters in that one call.
Adam Hill
Adam, note my most recent changes in response to your comment.
akf
AHA brilliant thanks that's where I had been going wrong thanks alot
Adam Hill