Solved

EnumNetworkDrives Method in batch script

Posted on 2009-05-18
7
340 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 70

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 70

Accepted Solution

by:
Qlemo earned 500 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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 70

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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