Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

EnumNetworkDrives Method in batch script

Posted on 2009-05-18
7
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

721 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