Page 1 of 1

Help with "if" code

Posted: 18 Mar 2011 15:58
by scienceguru1.bat
Can someone please tell me what is wrong with this:

Code: Select all

@echo off &setlocal
:start
set /p "x=Enter the number of seconds: "

set /a x+=1

echo/

echo delay start %time%
ping -n %x% localhost>nul
echo delay end %time%

set /p "yn=Would you like to use again? (use lowercase

y/n): "
set /p "cl=Would you like to clear the screen: "
if "cl"=="y"(cls)
if "yn"=="y"(goto start)

pause>nul


Thanks, C

Re: Help with "if" code

Posted: 19 Mar 2011 19:17
by phillid
Where in the code does it have an error?

Thanks,
phillid

Re: Help with "if" code

Posted: 19 Mar 2011 20:10
by scienceguru1.bat
it gets to where it uses the

Code: Select all

if "n"=="x"
then it gives an error and the command prompt shutsdown immediatly. it think it said syntex error

Re: Help with "if" code

Posted: 20 Mar 2011 00:55
by ghostmachine4
scienceguru1.bat wrote:it gets to where it uses the

Code: Select all

if "n"=="x"
then it gives an error and the command prompt shutsdown immediatly. it think it said syntex error


there is such a thing called a vbscript if you have not already known yet. It comes with Win98 and above pre-installed, so there is no reason you can't use it for your scripting task if you are insistent on not downloading stuff. Its more "powerful" than batch and can do many things batch can't.

Code: Select all

Do While 1=1
   WScript.Echo "Enter the number of seconds:"
   x = WScript.StdIn.ReadLine
   WScript.Echo "user input: " & x
   x = x + 1
   WScript.Echo "Delay start: " & Now
   WScript.Sleep 60 * x
   WScript.Echo "Delay end: " & Now
   WScript.Echo "Would you like to use again(y|n):"
   yn = WScript.StdIn.ReadLine
   If LCase(yn) = "n" Then
      Exit Do
   End If
Loop


you don't need to use ping to simulate a time delay... you can use functions such as Lcase to change case of letters, date manipulation is also easier. for maths, vbscript supports floating point maths, which crippled batch cannot.
To run the above script, save as myscript.vbs

Code: Select all

C:\work>cscript //nologo myscript.vbs
Enter the number of seconds:
23
user input: 23
Delay start: 2011-03-20 2:49:46 PM
Delay end: 2011-03-20 2:49:47 PM
Would you like to use again(y|n):
y
Enter the number of seconds:


here's the manual on vbscript

Re: Help with "if" code

Posted: 20 Mar 2011 02:19
by !k
scienceguru1.bat wrote:syntex error

Code: Select all

if "%cl%"=="y" (cls)
if "%yn%"=="y" (goto start)

Re: Help with "if" code

Posted: 20 Mar 2011 06:30
by scienceguru1.bat
thanks, !k, it looks like it should work. ill try it when i can get on the pc.

Re: Help with "if" code

Posted: 20 Mar 2011 15:03
by aGerman
Maybe you're looking for something like that :)

Code: Select all

@echo off &setlocal enabledelayedexpansion
for /f %%a in ('copy /z "%~f0" nul') do set "cr=%%a"
:start
set "x=0"
set /p "x=Enter the number of seconds: 0!cr!Enter the number of seconds: "
echo("!x!"|findstr /x "\"[0-9][0-9]*\"">nul||goto start
set /a x+=1
echo(
echo delay start %time%
ping -n %x% localhost>nul
echo delay end %time%
echo(
set "yn=y"
set /p "yn=Would you like to use again? [y/n] y!cr!Would you like to use again? [y/n] "
if /i "!yn!"=="y" (
  set "cl=n"
  set /p "cl=Would you like to clear the screen? [y/n] n!cr!Would you like to clear the screen? [y/n] "
  if /i "!cl!"=="y" (cls) else (echo(&echo()
  goto start
)

Regards
aGerman

Re: Help with "if" code

Posted: 20 Mar 2011 15:25
by scienceguru1.bat
thanks. ill try that to.

Re: Help with "if" code

Posted: 20 Mar 2011 17:30
by scienceguru1.bat
thanks, aGerman! it worked :D