Batch file checking if an website exist

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
silent
Posts: 44
Joined: 28 Oct 2011 14:40

Batch file checking if an website exist

#1 Post by silent » 30 Mar 2012 10:50

I want a batch file to check if an website exist,and if it exist it has to say some text,and if it doesnt it has to do nothing.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file checking if an website exist

#2 Post by Ed Dyreen » 30 Mar 2012 11:03

'

Code: Select all

>nul 2>&1 ping www.ed-dyreen.com &&echo.on-line ||echo.off-line

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Batch file checking if an website exist

#3 Post by Ocalabob » 30 Mar 2012 12:23

Greetings Silent,

I really like Ed Dyreen's solution. I think I'll start using his script.

Anyway, I currently use something similar to the script below on
a daily basis. I like to echo the results to the screen as well as
create a log file.

Best wishes!

Code: Select all

::Copied from FAQ #153 by Timo Salmi with modifications.

@echo off & setlocal

:start
set ser=google.com

echo Checking WEB site by^
 pinging %ser%

  set ok_=true
  ping -n 1 %ser%|find "Lost = 0">nul||set ok_=
  if defined ok_ (
    echo "Say some Text". Press the space bar. & echo^
 "Say some Text" on %date% at %time%>>c:\ping_log1.txt
    ) else (
    echo "Do Nothing" Press the space bar. & echo "Do Nothing" on %date% at %time%>>c:\ping_log1.txt
    )
pause>nul
echo===============>>c:\ping_log1.txt

endlocal

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Batch file checking if an website exist

#4 Post by Ocalabob » 30 Mar 2012 14:41

Greetings Ed,

I added a few lines to your code for my use on a daily basis.

Thank you and a tip of the fedora!

Code: Select all

:: Script by Ed Dyreen with modifications.
:: source:http://www.dostips.com/forum/viewtopic.php?f=3&t=3142&sid=d2e10f5a5d0e2d6ea24bb705e98392c2
:: Orginal code ">nul 2>&1 ping www.ed-dyreen.com &&echo.on-line ||echo.off-line"


@echo off & Setlocal

:start
set /p ser=Press 1 for Google.com or 2 for wildblue.net then press Enter. 
if %ser%==1 set ser=google.com
if %ser%==2 set ser=wildblue.net

echo Checking for active Internet Connection by^
 pinging %ser%

>nul 2>&1 ping -n 1 %ser% &&goto connected ||goto n_connected

:connected
echo Active connection to %ser% & echo^
 Active connection to %ser% on %date% at %time%>>c:\ping_log.txt
goto ans

:n_connected
echo NO Active connection to %ser% & echo^
 No active connection to %ser% on %date% at %time%>>c:\ping_log.txt
goto ans
   
:ans
echo=======>>c:\ping_log.txt
set /p reply=Try again? Answer Y or N Case Sentisive:
if %reply%==Y goto :Y
if %reply%==N goto :N
echo You didn't do it right. Press the Space Bar.
pause>nul & cls & goto :ans

:Y
cls & goto start
:N
goto :eof

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Batch file checking if an website exist

#5 Post by silent » 03 Apr 2012 09:23

Guys i need your help,both codes work great,but i want to ping a topic on a website,why it doesnt works ? Always tells me off-line.I want to use it in the game,and it has to check if a topic called "update 2.0" exist, so i want to ping a address like : http://www.website.com/download/update-2.0-f9.html

Like i can ping dostips.com but i cant ping dostips.com/forum,why ????

Thanks in advance,

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch file checking if an website exist

#6 Post by Squashman » 03 Apr 2012 10:53

That is not how ping works.

You would need to use a 3rd party utility or some other scripting language to do that.
wget would be an option.

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Batch file checking if an website exist

#7 Post by silent » 03 Apr 2012 11:48

I dont want the batch file to download the update,but alert that new update is avaiable and this game is outdated.There is no way to ping some topic like the example i gave ?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file checking if an website exist

#8 Post by abc0502 » 03 Apr 2012 12:03

the page is typing 404 file not found error is that mean it's an error to you ?
try this vb script
http://www.robvanderwoude.com/vbstech_internet_website.php
change the website in [ strWebsite = "www.robvanderwoude.com"] "2nd line in script" with your website
i tried that and it say it is up i don't know if that ok with you.
and ples answer the question i asked about the 404 error because there is a script but need changing so i need your answer

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Batch file checking if an website exist

#9 Post by silent » 03 Apr 2012 12:15

abc0502 wrote:the page is typing 404 file not found error is that mean it's an error to you ?
try this vb script
http://www.robvanderwoude.com/vbstech_internet_website.php
change the website in [ strWebsite = "www.robvanderwoude.com"] "2nd line in script" with your website
i tried that and it say it is up i don't know if that ok with you.
and ples answer the question i asked about the 404 error because there is a script but need changing so i need your answer


404 where ? The page i have posted is a fake one :D

PS,the vbs u gave says the site is down 2 :D Well,i can make a free website on some shit server for each update and it would just ping the normal addres (like,www.funkydunky.freehost.org)

The website of my game is www.codealphazero.freeforums.org

(Yeah long as shit)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file checking if an website exist

#10 Post by abc0502 » 03 Apr 2012 12:33

i looked at this URL: http://codealphazero.freeforums.org/test-for-update-t9.html

so basically u want to see if a link in this page is exist or not?
is that it
if so is the link you will provide will be from a permanent website host like mediafire or any host like it ?

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Batch file checking if an website exist

#11 Post by silent » 03 Apr 2012 12:45

Wat ? :D i want to check if the link that you checked exist,and the file will be uploaded as a attachment.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file checking if an website exist

#12 Post by abc0502 » 03 Apr 2012 12:53

are you the one who will upload the update?
if so i have an easy idea

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file checking if an website exist

#13 Post by foxidrive » 03 Apr 2012 18:22

silent wrote:Like i can ping dostips.com but i cant ping dostips.com/forum,why ????


Ping checks a server on a domain, not a page on a server.

Use wget as suggested to download the html page. If Wget returns an error then the page isn't there.

It doesn't download the attachment unless you tell it to, with switches.

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Batch file checking if an website exist

#14 Post by silent » 04 Apr 2012 12:37

abc,what is that idea ? :D

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file checking if an website exist

#15 Post by abc0502 » 06 Apr 2012 03:12

Here is the batch but first you must READ THIS:

Requirement:
1- Always include a text file in all of your updates name it "ver.txt" you will write in it the version of the update in this format "update-x.x". without qoutes. "x mean numbers"

2- A direct link on your website take that form "http://website/what-ever-directory/update.txt"
this file will be updated with the latest version of the update file in this form "update-x.x"
like the "ver.txt" file.

basicaly every time u make update put a ver.txt with the update and put update.txt in your website write in both of them this word "update-x.x" numbers must be the same.
so when a user have old update and the website have new one when the batch work it will download the update.txt from your site and comapare the version with the ver.txt file if it is the same it will pop-up a message saying there is no update available and if it doesn't match it say there is new update available.


Now the Batch:

Code: Select all

@echo off
cls
Title Update Checker
REM Please Remember The Only Changes That Might be Done Wil be In The Variable Section
::===== LOGO =========================================::
echo:
echo:
echo:
echo:
echo:
echo:
echo:
echo:
echo:                 ######  #### ##       ######## ##    ## ########
echo:                ##    ##  ##  ##       ##       ###   ##    ##   
echo:                ##        ##  ##       ##       ####  ##    ##   
echo:                 ######   ##  ##       ######   ## ## ##    ##   
echo:                      ##  ##  ##       ##       ##  ####    ##   
echo:                ##    ##  ##  ##       ##       ##   ###    ##   
echo:                 ######  #### ######## ######## ##    ##    ##   
ping localhost -n 2 >nul
set "batch_file=%temp%\Search.bat"
set "vb_file=%temp%\download.vbs"
set "update_file=%temp%\update.txt"

::===== Variable ========================================::

set "ver_file=D:\ver.txt"
set "download_link=http://www.dreamwidth.org/stats/stats.txt"
Rem Change Both up Lines with your ver.txt directory and the link of update.txt

::===== Batch File =======================================::

>"%batch_file%" (
echo @echo off
echo cls
echo setlocal
echo for /f ^%%%%z in ^('findstr "update" "%update_file%"'^) do set comp1=^%%%%z
echo for /f ^%%%%y in ^('findstr "update" "%ver_file%"'^) do set comp2=^%%%%y
echo IF ^%%comp1%% EQU ^%%comp2%% ^( msg * There is no Update Available
echo ^) Else ^( msg * New Update is Available ^)
echo del /f /s /q "%vb_file%"
echo del /f /s /q "%batch_file%" )>>"%batch_file%"
echo del /f /s /q "%update_file%"

::===== VBScript ========================================::

>"%vb_file%" (
echo HTTPDownload "%download_link%", "%update_file%"
echo Sub HTTPDownload^( myURL, myPath ^)
echo     Dim i, objFile, objFSO, objHTTP, strFile, strMsg
echo     Const ForReading = 1, ForWriting = 2, ForAppending = 8
echo     Set objFSO = CreateObject^( "Scripting.FileSystemObject" ^)
echo     If objFSO.FolderExists^( myPath ^) Then
echo         strFile = objFSO.BuildPath^( myPath, Mid^( myURL, InStrRev^( myURL, "/" ^) + 1 ^) ^)
echo     ElseIf objFSO.FolderExists^( Left^( myPath, InStrRev^( myPath, "\" ^) - 1 ^) ^) Then
echo         strFile = myPath
echo     Else
echo         WScript.Echo "ERROR: Target folder not found."
echo         Exit Sub
echo    End If
echo    Set objFile = objFSO.OpenTextFile^( strFile, ForWriting, True ^)
echo     Set objHTTP = CreateObject^( "WinHttp.WinHttpRequest.5.1" ^)
echo     objHTTP.Open "GET", myURL, False
echo     objHTTP.Send
echo     For i = 1 To LenB^( objHTTP.ResponseBody ^)
echo         objFile.Write Chr^( AscB^( MidB^( objHTTP.ResponseBody, i, 1 ^) ^) ^)
echo     Next
echo     objFile.Close^( ^)
echo Set oShell = CreateObject^("WSCript.shell"^)
echo sCmd = "%batch_file%"
echo oShell.Run sCmd, 0, False
echo End Sub )>>"%vb_file%"

Rem VBscript Original Source Code from: http://www.robvanderwoude.com/vbstech_internet_download.php

::===== Commands ======================================::

start /min %ComSpec% /c %vb_file% >nul

::===== End Batch =======================================::


Notes:
1- Change the Variable Section Only and all variable in the batch will be set.
2- The link make sure it start with "http://" because it give errors some times.
3- Make sure the link is always the same doesn't change and so is the ver.txt file.
4- Make sure there is no spaces between "update" and "-" and "version number".

I tested this patch with this link: http://www.dreamwidth.org/stats/stats.txt and this location of the "ver.txt" D:\ver.txt and it work fine with me.
I hope it help. :mrgreen:

Post Reply