0

I want to create a batch file to first ping a share drive on start to see if it is ready to be mapped, then map when the ping is returned. As psuedo, something like this:

while true:
    ping ipaddr -t
    if (ping returned):
        break
map drive

I believe the syntax would be something like:

:checkping
ping ipaddr -t
if ping:
    goto mountZ
fi
goto checkping

:mountZ
net use Z:....

So how do I go about setting the ping in a usable variable to break the loop?

Kevin Le
  • 49
  • 2
  • 9
  • 4
    ping a server, sure. But ping a share drive??? – dbenham Jul 16 '18 at 16:37
  • I have a NAS box that I'm pinging too. I wouldn't call it a server, but I guess calling it a shared drive wouldn't be as appropriate. The NAS acts like a server being that it has it's own IP but that's honestly all it can do. It's just a storage system I'm trying to communicate with over a network. I could be completely wrong but that's how the equipment was explained to me. – Kevin Le Jul 16 '18 at 17:03
  • You can certainly ping an IP address, but there is no way to ping a server to see if a Network share is available to the user. Also, I believe you means to say **pseudo** and not **sudo**. Two completely different concepts. – Squashman Jul 16 '18 at 17:15
  • I just wanted to know how to set a variable with the ping command. If the ping command returns, it just means that a connection is there, which is all I need to know. I just want a usable variable to use as a break condition to do something – Kevin Le Jul 16 '18 at 17:31
  • 1
    @KevinLe Checkout this question and its answers. https://stackoverflow.com/questions/3050898/how-to-check-if-ping-responded-or-not-in-a-batch-file – ug_ Jul 16 '18 at 17:34
  • Tried emulating that solution, but changed for my own personal use in which it still didn't work. it seems like what it returns is a string that can be parsed, but my lack in dos is really hindering me in understanding why it doesn't work, the do part is "unexpected" when ran – Kevin Le Jul 16 '18 at 18:40

3 Answers3

0

You could use the below. If there is a TTL (Time To Live) then it will go to A, otherwise it will continue.

:checkping
ping -n 1 www.google.com | findstr TTL && goto a
goto checkping
Break

:a
REM Mapped Drive is connected
Drew
  • 3,814
  • 2
  • 9
  • 28
0
:loop
    net use K: \\127.0.0.1\C$
If errorlevel 1 goto loop

Is one simple way of doing it.

CatCat
  • 483
  • 4
  • 5
0
@echo off
:checkping
ping 8.8.8.8 > nul
IF ERRORLEVEL 1 echo Not Connected &  goto checkping
IF ERRORLEVEL 0 echo Connected     &  net use Z:....
pause