batch check if network source is av

March 30, 2009 at 03:39:39
Specs: Windows Vista/XP
Hello,

I'm creating a batch which adds network drives. for a user when it logs on.
I got the script this far that it will check if the drive already exists and if it does it will be deleted and created again.

But how do I check if the network source is available?
If the server is offline the network drive can't be created..

this is the script I have so far

@echo off
if not exist X:\nul goto NOT_EXIST
MD X:\mytemp
if not exist X:\mytemp\nul goto NOT_READY
Rmdir X:\mytemp
net use X: /delete
goto create_X

:NOT_EXIST
net use X: \\[servername]\data\users\post
echo Drive X:\ is aangemaakt.
goto next

:NOT_READY
echo Drive X:\ is nog niet klaar!
goto fout

:create_X
net use X: \\[servername]\data\users\post
echo Drive X:\ is aangemaakt.
goto next

:fout
pause
echo Er is een fout opgetreden bij het aanmaken van drive X:\, neem AUB contact op met uw systeem beheerder
goto end

:next
if not exist W:\nul goto W_NOT_EXIST
MD W:\mytemp
if not exist W:\mytemp\nul goto W_NOT_READY
rmdir W:\mytemp
net use w: /delete
goto create_w

:W_NOT_EXIST
net use w: \\[servername]\data\users\archief
echo Drive W:\ is aangemaakt.
goto end

:W_NOT_READY
echo Drive W:\ is nog niet klaar!
goto fout

:create_w
net use w: \\[servername]\data\users\archief
echo Drive w:\ is aangemaakt.
goto end

:end

As you can see there are 2 network drives created X and W, if they already exist they will be deleted and again created. but if the network source is unavailable the creation will result in an error.

Is it possible to do a check on a network source? like this

if not exist \\[servername]\data\users\post goto fout

I hope someone can help me with this.

Greetz Michel


See More: batch check if network source is av

Report •


#1
March 30, 2009 at 04:01:06
In the meanwhile I had the time to test some things out. and it seems that it works to check a network path with
if no exist \\[servername]\data\users goto fout

I also perfected the script a lill more. if someone has tips it would be great!!

@echo off
if not exist X:\nul goto create_X
MD X:\mytemp
if not exist X:\mytemp\nul goto NOT_READY
Rmdir X:\mytemp
net use X: /delete
goto create_X

:NOT_READY
echo Drive X:\ is nog niet klaar!
goto fout_X

:create_X
if not exist \\[servername]\data\users\post goto fout_X
net use X: \\[servername]\data\users\post
echo Drive X:\ is aangemaakt.
goto next

:fout_X
echo Er is een fout opgetreden bij het aanmaken van drive X:\ neem AUB contact op met uw systeem beheerder
pause
goto next

:next
if not exist W:\nul goto create_w
MD W:\mytemp
if not exist W:\mytemp\nul goto W_NOT_READY
rmdir W:\mytemp
net use w: /delete
goto create_w


:W_NOT_READY
echo Drive W:\ is nog niet klaar!
goto fout_w

:create_w
if not exist \\[servername]\data\users\archief goto fout_W
net use w: \\[servername]\data\users\archief
echo Drive w:\ is aangemaakt.
goto end

:fout_w
echo Er is een fout opgetreden bij het aanmaken van drive W:\ neem AUB contact op met uw systeem beheerder
pause
goto end

:end


Report •

#2
March 30, 2009 at 05:41:07
Yes, you are correct in you observation.

As long as the user is logged-on and permissions allow the user access to the server resource, it IS valid to use UNC file names in a batch file.


Report •

#3
March 30, 2009 at 06:06:28
The users that the batch file aplies to do have access to these network resources. (They have the neccesairy permissions so no user authentication is required.)

If my observations are correct this line
if not exist \\[servername]\data\users goto fout
will check if they can establish a connection to the server.
If this check failes you'll get an error which stays in the screen with the Pause command.
If all goes smoothly the user will barely notice the script.

Still haven't got the change to test out the script in the actual business envoirment. But I've created a 2nd (simpler one) to test in my own envoirment. It seems to work just fine.
The thing is that I don't know how the script will react on other warnings...


Report •

Related Solutions


Ask Question