Help with "if" code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Help with "if" code

#1 Post by scienceguru1.bat » 18 Mar 2011 15:58

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

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Help with "if" code

#2 Post by phillid » 19 Mar 2011 19:17

Where in the code does it have an error?

Thanks,
phillid

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Help with "if" code

#3 Post by scienceguru1.bat » 19 Mar 2011 20:10

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Help with "if" code

#4 Post by ghostmachine4 » 20 Mar 2011 00:55

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Help with "if" code

#5 Post by !k » 20 Mar 2011 02:19

scienceguru1.bat wrote:syntex error

Code: Select all

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

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Help with "if" code

#6 Post by scienceguru1.bat » 20 Mar 2011 06:30

thanks, !k, it looks like it should work. ill try it when i can get on the pc.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with "if" code

#7 Post by aGerman » 20 Mar 2011 15:03

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

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Help with "if" code

#8 Post by scienceguru1.bat » 20 Mar 2011 15:25

thanks. ill try that to.

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Help with "if" code

#9 Post by scienceguru1.bat » 20 Mar 2011 17:30

thanks, aGerman! it worked :D

Post Reply