You can use the exist
command to check if the path is valid:
if exist \\192.168.1.1\drive1 net use s: \\192.168.1.1\drive1
If you need to provide credentials (i.e. your current Windows user doesn't have access to that share), add /user
:
if exist \\192.168.1.1\drive1 net use s: \\192.168.1.1\drive1 /user:myDomain\myUser myPassword
If there's a chance that the share already exists, and you want to delete it if it's no longer available, add an else
clause:
if exist \\192.168.1.1\drive1 (net use s: \\192.168.1.1\drive1) else (net use /delete s:)
And once again, add the /user
if you need it.
You can tie this all together in a batch file similar to the following:
@echo off
if exist \\192.168.1.1\drive1 (set shareExists=1) else (set shareExists=0)
if exist y:\ (set driveExists=1) else (set driveExists=0)
if %shareExists%==1 if not %driveExists%==1 (net use y: \\192.168.1.1\drive1)
if %shareExists%==0 if %driveExists%==1 (net use /delete y:)
set driveExists=
set shareExists=
Would "if exist" also confirm if the shared network path is accessible or not? Similar to how one would manually click the link to make sure it isn't down? – BV45 – 2018-07-16T20:07:27.017
ok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error? – FernandoSBS – 2013-05-21T21:35:29.830
1That command is a batch command - it should work from any standard batch file. – Geoff – 2013-05-21T22:28:30.357
C:\Windows>if exist \192.168.1.1\volume1 (net use y: \192.168.1.1\volume1 ) e lse (net use /delete y: ) The network connection could not be found.
More help is available by typing NET HELPMSG 2250. – FernandoSBS – 2013-05-21T22:44:28.677
I'll add an edit... – Geoff – 2013-05-22T12:53:54.420
i'm sorry? (5 chars) – FernandoSBS – 2013-05-22T21:17:43.480
I added an extra piece to my answer - see the bottom batch file – Geoff – 2013-05-22T21:33:44.513