Hadi Farah
Hadi Farah

Reputation: 1162

My Batch file Is not looping?

I am new to this and I am trying a basic example I saw on Youtube:

@echo off
echo this little program helps you to refresh a website 
pause
cls
echo but first we have to do some preferences
pause
cls
echo please enter the website name here
set /p website=
cls
echo please enter the time between refreshes
set /p time=
cls
echo Set limited or unlimited refresh
set /p refresh=
if'%refresh%'=='1' goto :once
if'%refresh%'=='0' goto :unlimited
cls

:once
cls
echo Preferences finished
pause
timeout /t %time% /nobreak
start %website%
exit

:unlimited
cls
echo Preferences finished
pause
:again
timeout /t %time% /nobreak
start %website%
goto :again

This code is supposed to ask a user for a website and a refresh time and it will refresh that website by reopening it in IE (Internet Explorer), however I have 2 problems with this:

  1. The loop does not work if I enter 0 or '0' it will only execute once and exit the .bat
  2. Is there a way to refresh a page without reopening the page in a new tab, for example just refresh the current tab ?

Upvotes: 0

Views: 127

Answers (1)

user6250760
user6250760

Reputation:

There are some syntax errors in your script.


EXIT command

EXIT exits the script. You may want to change that to something else.


IF Statement

if'%refresh%'=='1' goto :once

is slightly incorrect, as there is a missing space between if and the comparee. In addition, using double quotes ensure it to successfully compare with values including spaces.

if "%refresh%"=="1" goto :once

Undefined Input

If variable %refresh% is not 1 or 0, it goes to :once and execute the code there.


Refreshing Webpage

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.AppActivate("Chrome")
WshShell.SendKeys "{F5}"

Save this script as Refresh.vbs and put it in the same folder as the batch file.

Then, when you want to refresh the webpage, do cscript //nologo Refresh.vbs.

Upvotes: 1

Related Questions