# Solved: Detect Network Drives



## scrfix (May 3, 2009)

I need to find out the drive letters for the current network drives connected to the computer from a batch file. I have this written already. I want to know if there is a better way to write this. This has only been tested on a Windows 7 Professional machine. I actually still need to test it on XP (Home Pro), Server 2003, Server 2008, Vista (Home Pro).

I want to know if there is a better way to writee this other than what I have. Currently the way that I have it, it looks through everything over and over and over again.


```
@echo on
::Variables
set _ab=ABCDEFGHIJKLMNOPQRSTUVWXYZ

:_NETWORKDRIVECHECK
setlocal EnableDelayedExpansion
for /F "tokens=2* skip=6 eol=\ delims=: " %%I IN ('NET USE') DO CALL :_NETWORKDRIVEPROCESS %%I
GOTO _COMPLETED
 
:_NETWORKDRIVEPROCESS
if /i [%1]==[Windows] GOTO :EOF
if /i [%1]==[command] GOTO :EOF
FOR /F "tokens=1" %%I IN ("%_ab%") DO (set _ab=!_ab:%1=!)&GOTO :EOF
GOTO :EOF
 
:_COMPLETED
echo %_ab%
pause
```
*Notes*: When I do a Net Use>"net use.txt" I receive the following (Without the [Line XX]: ):

[Line 01]: New connections will be remembered.
[Line 02]: 
[Line 03]: 
[Line 04]: Status Local Remote Network
[Line 05]: 
[Line 06]: -------------------------------------------------------------------------------
[Line 07]: Unavailable S: \\SERVER\Shared Microsoft Windows Network
[Line 08]: Unavailable T: \\SERVER\TimeClock Plus 4.1 
[Line 09]: Microsoft Windows Network
[Line 10]: OK H: \\SERVER\Home\Wayne Microsoft Windows Network
[Line 11]: OK \\SERVER\HOME Microsoft Windows Network
[Line 12]: The command completed successfully.

Currently, when I utilize the FOR loop that I wrote above I receive the following:


```
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\>"netdrive-call-test.bat"
C:\>set _ab=ABCDEFGHIJKLMNOPQRSTUVWXYZ
C:\>setlocal EnableDelayedExpansion
C:\>for /F "tokens=2* skip=6 eol=\ delims=: " %I IN ('NET US
E') DO CALL :_NETWORKDRIVEPROCESS %I
 
C:\>CALL :_NETWORKDRIVEPROCESS H
C:\>if /I [H] == [Windows] GOTO :EOF
C:\>if /I [H] == [command] GOTO :EOF
C:\>FOR /F "tokens=1" %I IN ("ABCDEFGHIJKLMNOPQRSTUVWXYZ") D
O (set _ab=!_ab:H=! )  & GOTO :EOF
 
C:\>(set _ab=!_ab:H=! )  & GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS S
C:\>if /I [S] == [Windows] GOTO :EOF
C:\>if /I [S] == [command] GOTO :EOF
C:\>FOR /F "tokens=1" %I IN ("ABCDEFGIJKLMNOPQRSTUVWXYZ") DO
 (set _ab=!_ab:S=! )  & GOTO :EOF
 
C:\>(set _ab=!_ab:S=! )  & GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS T
C:\>if /I [T] == [Windows] GOTO :EOF
C:\>if /I [T] == [command] GOTO :EOF
C:\>FOR /F "tokens=1" %I IN ("ABCDEFGIJKLMNOPQRTUVWXYZ") DO
(set _ab=!_ab:T=! )  & GOTO :EOF
 
C:\>(set _ab=!_ab:T=! )  & GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS Windows
C:\>if /I [Windows] == [Windows] GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS [URL="file://\\SERVER\HOME"]\\SERVER\HOME[/URL]
C:\>if /I [\\SERVER\HOME] == [Windows] GOTO :EOF
C:\>if /I [\\SERVER\HOME] == [command] GOTO :EOF
C:\>FOR /F "tokens=1" %I IN ("ABCDEFGIJKLMNOPQRUVWXYZ") DO (
set _ab=!_ab:\\SERVER\HOME=! )  & GOTO :EOF
 
C:\>(set _ab=!_ab:\\SERVER\HOME=! )  & GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS command
C:\>if /I [command] == [Windows] GOTO :EOF
C:\>if /I [\\SERVER\HOME] == [command] GOTO :EOF
C:\>FOR /F "tokens=1" %I IN ("ABCDEFGIJKLMNOPQRUVWXYZ") DO (
set _ab=!_ab:\\SERVER\HOME=! )  & GOTO :EOF
 
C:\>(set _ab=!_ab:\\SERVER\HOME=! )  & GOTO :EOF
C:\>CALL :_NETWORKDRIVEPROCESS command
C:\>if /I [command] == [Windows] GOTO :EOF
C:\>if /I [command] == [command] GOTO :EOF
C:\>GOTO _COMPLETED
C:\>echo ABCDEFGIJKLMNOPQRUVWXYZ
ABCDEFGIJKLMNOPQRUVWXYZ
 
C:\>pause
Press any key to continue . . .
C:\>
```
I don't need it to look at:
CALL :_NETWORKDRIVEPROCESS command
CALL :_NETWORKDRIVEPROCESS \\SERVER\HOME
CALL :_NETWORKDRIVEPROCESS Windows

Is there a way to not look at those at all?


----------



## Squashman (Apr 4, 2003)

Use the Colon to your advantage. All the lines you want have a colon in it.


----------



## Squashman (Apr 4, 2003)

I suppose you could use the double back slash as well.


----------



## scrfix (May 3, 2009)

Isnt that what I did with delims=: Where should I use the : if not there?


----------



## Squashman (Apr 4, 2003)

You don't need the delims. By default the for loop will use a space as the deimiter. Pipe the output of the Net use command to the find or findstr command to search for a colon or double backslash. This will limit your output to the lines you need.


----------



## scrfix (May 3, 2009)

okay will try it.

thanks


----------

