Script - How to check if a network path is available and then map it
Solution 1
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=
Solution 2
Powershell would make this easy:
If(Test-Path \\192.168.1.1\Drive1)
{
net use M: \\192.168.1.1\Drive1 /user:Domain\UserName Password
}
else {net use M: /delete > nul}
Related videos on Youtube
March
Updated on September 18, 2022Comments
-
March over 1 year
My client needs an app where he can input the text data and send it to another android device via Bluetooth. On the receiving device, the text should be inputted into currently active text field (doesn’t matter if it’s in settings, browser or any other app). My idea of this is to broadcast the text as HID so it behaves like an external Bluetooth keyboard. Is it possible to create such app?
-
FernandoSBS almost 11 yearsIf it's available I want to be automatically connected when I logon / screen saver, if it's not then I want it removed from the mapped drives. So what you suggested is not an option.
-
Kevin Versfeld almost 11 yearsI don't understand, it you issue a net use, if the command succeeds it will be connected. If the command fails, then it is removed from the mapped drives. Seems simple to me.
-
FernandoSBS almost 11 yearsok 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 almost 11 yearsok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error?
-
Austin T French almost 11 yearsSave it as a .ps1 and run it from Powershell.
-
FernandoSBS almost 11 yearsi'm not familiar with powershell, how can I automate it in Task Scheduler?
-
Austin T French almost 11 yearsYou should really split the 2 questions. This is a QA site. Also, google it if you have not already: google.com/…
-
FernandoSBS almost 11 yearsok got it. What the bonus of using powershell?
-
Austin T French almost 11 yearsCan use the full .NET libraries, extensible through C#, can use native windows Commands... It has an ISE and IDEs through PowerGUI and others. Great syntax, automatic and explicit data types... In short it does everything
-
Geoff almost 11 yearsThat command is a batch command - it should work from any standard batch file.
-
FernandoSBS almost 11 yearsC:\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 almost 11 yearsok I've set the restriction off. now: net.exe : The network connection could not be found. At C:\Windows\logon.ps1:7 char:10 + else {net <<<< use Y: /delete} + CategoryInfo : NotSpecified: (The network con...d not be found.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError More help is available by typing NET HELPMSG 2250.
-
FernandoSBS almost 11 years
-
Geoff almost 11 yearsI'll add an edit...
-
FernandoSBS almost 11 yearsi'm sorry? (5 chars)
-
Geoff almost 11 yearsI added an extra piece to my answer - see the bottom batch file
-
BV45 almost 6 yearsWould "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?
-
Li Mengran over 4 yearsOn second thought. Broadcasting is not really a good idea. What would serve you better is to make the sender a GATT server, which advertises and exposes a proprietary GATT service you define. This service has a characteristic that stores the live string data. The receiver works as a GATT client. It scans for the GATT server with your service, and then subscribe to the characteristic, whose value gets updated with the keyboard input. Google has extensive documentation and examples here: developer.android.com/guide/topics/connectivity/bluetooth-le
-
Li Mengran over 4 yearsAnd more samples at their github repos: github.com/android/connectivity-samples
-
user1602 almost 4 yearsHow to check the status of the mapped drive? It can be OK, Disconnected, Unavailable
-
Geoff almost 4 years@user1602 That's a different issue - submit a new question, after searching to see if it has already been answered