Submitted byCategory
Review Cycle
.
Public
Joachim Mutter/sysarc
on 08/05/2011 at 08:57 AM
DOS\Batches

Automatic drive mapping

People who often work in different networks, would whish a simple script, which maps the appropriate drives which are available in one particular network.
But simple mapping of drives, especially more than one, could produce nightmares, because the system waits a long time, if you use "net use" with a non existing UNC path.

But with a bit shell programming you could avoid this behavior. And besides, it offers you the possiblilty of map different sets of drives depoending on the used subnet.

Take a look to the code section, where you find the code (standart DOS batch commands)

For the descission we use a normal ping with the parameters n -1 w -200 to use only one ping and a timeout period of 200 milliseconds, to shorten the waiting time.
If the ping finds a target, the system variable errorlevel will get the value 0, otherwise a value different to 0.
And this is the trick.
We ping the UNC Drive part (the net address) and if we got an errorlevel of 0, which could be evaluated by the construct
IF %ERRORLEVEL%==0 the machine is accessible and we could use "net use" to map the appropriate drive.

Leute die oft in unterschiedlichen Netzen unterwegs sind, würden sich ein einfaches Script wünschen, in das sie die Laufwerke, auf die sie in diesem speziellen Netz Zugriff haben, mappen möchten.

Doch ein einfaches Mappingscript kann zu einem Alptraum werden, wenn man vefrsucht per "Net use" Laufwerke zu mappen, deren Maschine nicht zugreifbar ist.

Aber mit ein wenig DOS Wissen und ein paar Zeilen Batch Code lässt sich das sehr elegant umgehen. Zudem kann mit diesem Script auch ein Automatismus implementiert werden, der abhänig vom Subnetz unterschiedliche Sets von Laufwerken mappt.

Wenn Sie sich folgenden Batchcode mal ein wenig näher ansehen, dann versteht man auf Anhieb den Mechanismus.

Für die Entscheidung ob eine Maschine da ist, kann man einfach einen Ping absetzen. Wir verwenden dazu die Parameter n-1
und w -200, um nur einen Ping mit einem Timeout von 200 Millisekunden zu schicken, was die Wartezeit verkürzt.
Und das ist der Trick!
Wenn der Ping mit dem UNC Pfad (nur die Netzadresse) nun eine erfolgreiche Verbindung hergestellt hat, dann setzt er die Systemvariable ERRORLEVEL auf 0. Im anderen Fall steht hier ein Wert ungleich 0, was per IF %ERRORLEVEL%==0 Konstrukt dazu verwendbar ist, ein "net use" auszuführen, oder eben nicht.