IF ELSE statement based on ping result

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
smallducktown
Posts: 2
Joined: 06 Dec 2010 15:28

IF ELSE statement based on ping result

#1 Post by smallducktown » 06 Dec 2010 15:56

Hey guys,

I'm making a script wich opens a file (at random), but this file is placed on a remote disk. So i want to ping the IP adress to make shure there's connectivity. If there is connectivity it should open a file on the remote disk, else it should open a local file. I got the open file part all worked out, its only the if/else part i need.

ping 129.168.1.2
if "succes"
*open remote file *
else
*open local file *

Is it possible and if so, could you help me with that?
Thnx

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

Re: IF ELSE statement based on ping result

#2 Post by aGerman » 06 Dec 2010 16:46

You could use the ERRORLEVEL, or maybe more simple: && (command was successful) and || (command failed)

Code: Select all

@echo off
ping -n 1 129.168.1.2 >nul &&(
  *open remote file *
)||(
  *open local file *
)

Regards
aGerman

smallducktown
Posts: 2
Joined: 06 Dec 2010 15:28

Re: IF ELSE statement based on ping result

#3 Post by smallducktown » 07 Dec 2010 12:33

thnx :D

Post Reply