Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

EnumNetworkDrives Method in batch script

Posted on 2009-05-18
7
Medium Priority
?
346 Views
Last Modified: 2012-05-07
hey,

is there any way to check above in a batch file ?
my idea was to play with net use, but how can i capture specific drive UNC ?

one last thing after i got the unc how can i play with the parts ?
what i mean for example :

\\serverA\public

how to "pull" serverA and lets say ping it ?
or check if the folder name it's public and if not remap another folder ?

if i asking to much in one question just say :-)

thanks
0
Comment
Question by:yehudaha
  • 4
  • 3
7 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 24419834
If you have a UNC, you can treat it like a file name, hence
for %U in (\\serverA\public) do @echo %~nU
will echo the share name only (in a batch file, you need to use %%U and %%~nU).

To process the net use output, for /F is useful:
for /F "tokens=2-3 delims=\ "  %S in ('net use ^| find "\"') do @echo Server: %S    Share:  %T

Now, the last does not make sense. It is not necessary to check if a share already is used on a specific server, but others do. If you need the share, just map it with net use. If it exists already, the command will silently do nothing.

0
 
LVL 14

Author Comment

by:yehudaha
ID: 24428514
thanks Qlemo for your respone

the output i'm getting is the drive letter and the host name.

my direction in this script is to pull the host name, ping it, pull the subnet of the host in the share
and check if it's match the local subnet if not remove the non matching share.

we have branches with local file servers, but the problem that the users is traveling between the branches
and getting the wrong drive mapping from there original branches, using it and causing big slow down.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 24429933
That makes sense. But do your folks need to use persistent network drives? It would make things easier if drive mappings wouldn't survive a reboot.

Having said that, let's make a first attempt.
It's not easy to decide the subnet thing - how should that work? Should it be a decision based
  1. on IP address  -  dangerous if you use subnet masks differing from Classes A,B,C, or different subnet masks in the branches,
  2. or DNS names - difficult, as the full qualified name might not appear in net use, and a DNS query will not help.
The following snippet is based on
  • each server is reachable via ping
  • we are using Class C only
It does not matter if a drive mapping or only UNC is used. The net use /d is only echoed, for test reasons. You can remove the echo (word only) to execute it.

@echo off
setlocal EnableDelayedExpansion
 
REM Assuming Class C, 192.168.x.y => net 192.168.x
for /F "tokens=2 delims=[]" %%C in ('ping -n 1 %computername% ^| find "["') do set net=%%~nC
 
for /F "tokens=2-4 delims=\ " %%S in ('net use ^| find "\"') do (
  set srv=%%S
  if "!srv:~1,1!" == ":" (
    set srv=%%T
    set share=%%S  
  ) else (
    set share=\\%%S\%%T
  )
  for /F "tokens=2 delims=[]" %%C in ('ping -n 1 !srv! ^| find "["') do (
    if not %net% == %%~nC echo net use !share! /d
  )
)

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Author Comment

by:yehudaha
ID: 24435861
mm nice one

batch scripting very intresting,
do you have a suggestion how can i learn batch ?
even better how did you learn ? :-)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24436487
I'm a dinosaur, we had nothing else in the early days before the World War ;-)
Seriously, I don't know of a good tutorial. It might exist, however.

A good start is to read the help comming from
if /?
set /?
for /?
cmd /?
help

All stuff used in above script can be derived out of those help pages.

0
 
LVL 14

Author Closing Comment

by:yehudaha
ID: 31582787
Thanks For The Help !
0
 
LVL 14

Author Comment

by:yehudaha
ID: 24438018
>> I'm a dinosaur, we had nothing else in the early days before the World War

:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question