Return to Level1Techs.com

Writing a .bat


#1

Hey all,
I was wondering if anyone could help me with a little project my boss gave me.
He wants me to write a .bat file that will silently unmount and remount a network drive.
this is the example he gave me.
@echo off

net use * / delete /yes net use s: unc path /user: username password
but it doesn’t seem quiet right. which he has a history giving wrong information lol.
can anyone help me with this one?
thank you.


#2

A little search brought up this: https://superuser.com/questions/598355/how-to-disconnect-a-network-drive-using-powershell


#3

Net use is the bat command you want to use both for mapping and unmapping. Here is a reference-
https://ss64.com/nt/net-use.html


#4

Source= https://stackoverflow.com/questions/10552812/defining-and-using-a-variable-in-batch-file

Note last version technique as mapped drive is variable being declared then when unmapped being cleared from dim declaration

Dim =
'dim =

The spaces are significant. You created a variable named (enclosing single quotes added to show location of space) ‘location ’ with a value of ’ “bob”’.
If you want quotes in your value, then your code should look like
set location=“bob”
If you don’t want quotes, then your code should look like
set location=bob
Or better yet
set “location=bob”
The last syntax prevents inadvertent spaces from getting in the value, and also protects against special characters like & | etc.


#5

Its better to use the direct IP instead of UNC path because sometimes PCs just dont show up in network discovery and so UNC breaks.


#6

Another nice guide and note dotteddomainname aswell as /p:yes\no