Solved

EnumNetworkDrives Method in batch script

Posted on 2009-05-18
7
342 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

635 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