having problems batch yes/no

February 14, 2011 at 09:36:15
Specs: Windows 7
Hello,
im having a problem,im doing batch on the side of my networking and internet security course at school and am having trouble figuring out whats not working in the code iv created.It gives me a weird error saying something like it cannot accses something at the moment i cant really tell because the cmd windows closes to quickly heres the code.
@echo off
echo ipconfig troubleshooter and fixer
ping -n 10 google.com
set /p = Do you want to continue (y/n)
if (%run%=='y') (
goto continue
) else (
if (%run%=='n') exit
)
:continue
ipconfig /release
set /p = Do you want to continue (y/n)
if (%run%=='y') (
goto continue1
) else (
if (%run%=='n') exit
)
:continue1
ipconfig /renew
set /p = Do you want to continue (y/n)
if (%run%=='y') (
goto continue2
) else (
if (%run%=='n') exit
)
:continue2
ipconfig /flushdns
set /p = Do you want to continue (y/n)
if (%run%=='y') (
goto continue3
) else (
if (%run%=='n') exit
)
:continue3
ipconfig /registerdns
set /p run= Do you want to continue (y/n)
if (%run%=='y') (
goto continue4
) else (
if (%run%=='n') exit
)
:continue4
ping -n 10 google.com
echo Done pinging google
pause
exit

See More: having problems batch yes/no

Report •

#1
February 14, 2011 at 09:51:20
Script runs fine for me.

A good way to see the error is to open a prompt window and drag the file into it (or manually open the file).

That way when the error occurs and the bat ends, the window will not close.


Report •

#2
February 14, 2011 at 10:02:39
delete the "@echo off" and put in a pause at the end. just to test you program untill it works

Report •

#3
February 14, 2011 at 10:09:29
thanks guys quick question run as admin right?

Report •

Related Solutions

#4
February 14, 2011 at 10:35:27
nah not gna matter

Report •

#5
February 14, 2011 at 17:13:16
k well its still not working its giving me ") is unrecognized at this time" and it wont continue properly and i cant seem to get it to work without useing brackets

Report •

#6
February 14, 2011 at 17:24:35
post the complete output. I mean copy everything from the command console (of course run the code with echo on ) and then paste here

Subhash Chandra.
www.s-chandra.co.cc


Report •

#7
February 14, 2011 at 20:08:29
yeah i dont know how to get it to copy over yet but it will just got to the first y/n argument then say ) is unrecognized at this time iv tried running as admin and everything itll work on windows xp for some unknown reason but i need it to work on windows 7

Report •

#8
February 14, 2011 at 21:53:04
It looks like your used to "C" like if statements, batch differs greatly.

A few important things to consider:

[1] Batch if statements include any quotes in the comparison.

[2] Batch if statements shouldn't have the comparison enclosed in parentheses.

[3] Batch variables expand empty if undefined.

[4] Standard batch variables(%var%) can execute code unless you are careful.


All of that being said the code is rather repetitive and can be made smaller:

@echo off
SetLocal EnableDelayedExpansion


echo ipconfig troubleshooter and fixer
ping -n 10 google.com



for %%a in (release renew flushdns registerdns) do (
    echo.
    call :YorN %%a
    if /i !yn!==n exit /b
    ipconfig /%%a
)

echo.
call :YorN "Ping Google"
if /i !yn!==y (
    ping -n 10 google.com
    echo Done pinging google
    pause
)
exit /b


:YorN
set yn=
set /p yn= Do you want to continue (y/n) (%1)
if /i "!yn!"=="y" goto :eof
if /i "!yn!"=="n" goto :eof
goto YorN


Report •

#9
February 14, 2011 at 23:17:43
wow ..thats surprising :) its hard enough for me to even think of such code...

Subhash Chandra.
www.s-chandra.co.cc


Report •

#10
February 15, 2011 at 02:31:49
Line 4 & 5:

In line 5 you have: if (%run%=='y') ( goto continue
But variable "%run%" does not exist since you dont reserved it at line 4.

Line 4:

Replace: set /p = Do you want to continue (y/n)
With: set /p run= Do you want to continue (y/n)

And check the other variables too, check if they are reserved or not.

Regards,

Happy is ONE who says I am an OttoMAN.


Report •

#11
February 15, 2011 at 07:08:36
thank you all i got it working it was the if statements and the variables pretty much a total redesign in the code and as for the shortened version wow i dont know much about the call command but will look into it thank you

Report •

Ask Question