Get the share name by piping Net Use?

  • Thread starter Thread starter jwgoerlich
  • Start date Start date
J

jwgoerlich

I am writing a cmd batch job wherein I must check to see where a drive
letter is mapped and then perform some action. I am thinking that the
simplest method would be to pipe Net Use and then parse the input.

For example,

:\ Net Use K:

Local name K:
Remote name \\server\share
Resource type Disk
Status OK
# Opens 20
# Connections 1
The command completed successfully.

What I would like to do is something like the following pseudo code:
If "Net Use K:" Contains "\\server\share" Then Goto Subroutine

How can I code this in batch for a Win2000 computer?

Thanks in advance,

J Wolfgang Goerlich
 
----- jwgoerlich wrote: ----

I am writing a cmd batch job wherein I must check to see where a driv
letter is mapped and then perform some action. I am thinking that th
simplest method would be to pipe Net Use and then parse the input

For example,

:\ Net Use K

Local name K
Remote name \\server\shar
Resource type Dis
Status O
# Opens 2
# Connections
The command completed successfully

What I would like to do is something like the following pseudo code:
If "Net Use K:" Contains "\\server\share" Then Goto Subroutin

How can I code this in batch for a Win2000 computer

Thanks in advance

J Wolfgang Goerlic
---------------------

This for loop will find the shares. You must use %% instead of % if batching

for /f "tokens=2" %A in ('net use') do @echo %~A | findstr ":

Produces this list on my machine
---snip--
I:\> for /f "tokens=2" %A in ('net use') do @echo %~A | findstr ":
G
I
J
L
P
---snip--
 
jwgoerlich said:
I am writing a cmd batch job wherein I must check to see where a drive
letter is mapped and then perform some action. I am thinking that the
simplest method would be to pipe Net Use and then parse the input.

For example,

:\ Net Use K:

Local name K:
Remote name \\server\share
Resource type Disk
Status OK
# Opens 20
# Connections 1
The command completed successfully.

What I would like to do is something like the following pseudo code:
If "Net Use K:" Contains "\\server\share" Then Goto Subroutine

How can I code this in batch for a Win2000 computer?

:Determines whether a given \\server\share is mapped to a specified drive
:letter. Does not work if server or share contain spaces. Run under
cmd.exe.

@echo off
setlocal enableextensions
set remote_share=\\server\share
set drive_letter=k:

for /f "tokens=3" %%i ^
in ('net use %drive_letter% ^| findstr /i /c:"%remote_share%"') do (
if /i %%i equ %remote_share% call :subroutine
)
goto :eof

:subroutine
:Put whatever code you want in here.
echo Drive letter %drive_letter% connects to remote share %remote_share%
goto :eof



David
Stardate 4358.1
 
David Trimboli said:
:Determines whether a given \\server\share is mapped to a specified drive
:letter. Does not work if server or share contain spaces. Run under
cmd.exe.

Watch out! I misjudged the length of that line. The line that begins with
":letter" should end in "cmd.exe". "cmd.exe" should not be on its own line.

David
Stardate 4358.1
 
David Trimboli said:
:Determines whether a given \\server\share is mapped to a specified drive
:letter. Does not work if server or share contain spaces. Run under
cmd.exe.

@echo off
setlocal enableextensions
set remote_share=\\server\share
set drive_letter=k:

for /f "tokens=3" %%i ^
in ('net use %drive_letter% ^| findstr /i /c:"%remote_share%"') do (
if /i %%i equ %remote_share% call :subroutine
)
goto :eof

:subroutine
:Put whatever code you want in here.
echo Drive letter %drive_letter% connects to remote share %remote_share%
goto :eof
This should also work with unusal server/share names.

@echo off
setlocal enableextensions
set RS=\\server\share
set DR=k:
for /f "tokens=1,*" %%A in ('net use %DR%^|find "\\"') DO (
if "%%B" EQU "%RS%" call :sub)
 
Matthias Tacke said:
This should also work with unusal server/share names.

@echo off
setlocal enableextensions
set RS=\\server\share
set DR=k:
for /f "tokens=1,*" %%A in ('net use %DR%^|find "\\"') DO (
if "%%B" EQU "%RS%" call :sub)


Oh yeah. Duh! :)

David
Stardate 4360.2
 
What I would like to do is something like the following pseudo code:
If "Net Use K:" Contains "\\server\share" Then Goto Subroutine

If you know the drive letter you are checking and the share,
the following follows your pseudo code:

----start
net use K: | find /I "\\server\share" && goto Subroutine

:Subroutine
echo Found server\share - do processing
------- end

--
Matt Hickman
We dicker and compromise and everybody thinks he has received a raw
deal, but somehow after a tedious amount of talk we come up with a
jury-rigged to do it without getting anybody's head bashed in. That's
politics.
Robert A. Heinlein (1907 -1988)
_Podkayne of Mars_ 1963
 
Many thanks, gentlemen. You have made my life easier.

J Wolfgang Goerlich

setlocal enableextensions
set RS=\\server\share
set DR=k:
for /f "tokens=1,*" %%A in ('net use %DR%^|find "\\"') DO (
if "%%B" EQU "%RS%" call :sub)

- and -
 
Back
Top