Tech Support > Microsoft Windows > Batch file to show Network drives
Batch file to show Network drives
Posted by joshboski on June 3rd, 2008


I am currently trying to write a batch file that will delete a list of
files from all of my network drives. The problem I am having is
getting the network drives. I am on a rather large network, and I
would like this to run on several different computers that may be
connected to different drives.

Posted by Bob I on June 3rd, 2008


See "net use" and "If exist" in Windows Help and support. Also there is
a cmd prompt group that has some pretty sharp folks(cross posting this)

news://msnews.microsoft.com/microsof...mdprompt.admin

joshboski wrote:


Posted by joshboski on June 3rd, 2008


On Jun 3, 9:21 am, Bob I <bire...@yahoo.com> wrote:
My apologies, that I did not include this also...I need the list of
local hard drives as well

Posted by Pegasus \(MVP\) on June 3rd, 2008



"joshboski" <josh.burkholder@gmail.com> wrote in message
news:4e9ec7fc-5000-4ab5-bbe8-115ebce19dde@j22g2000hsf.googlegroups.com...
As Bob recommended: net use (for networked drives), and
mountvol | find ":\" for local drives.



Posted by Pegasus \(MVP\) on June 3rd, 2008



"joshboski" <josh.burkholder@gmail.com> wrote in message
news:4e9ec7fc-5000-4ab5-bbe8-115ebce19dde@j22g2000hsf.googlegroups.com...
If you want something a little snazzier then you can use this script
file and massage it to give you the information you want. You need
to invoke it like so in your batch file: cscript //nologo c:\diskparms.vbs

Const Removable = 1
Const Fixed = 2
Const Network = 3
Const CDROM = 4
Const RAMDisk = 5

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set drives = oFSO.Drives

NewLine=Chr(10)
Line = ""

For Each drive In drives
Line = Line & "Drive " & drive.Path
Line = Line & " " & ShowDriveType(Drive)
If drive.IsReady Then Line = Line & ", ready" Else Line = Line & ", not
ready"

If drive.IsReady Then
If drive.DriveType=Network Then
Line = Line & ", Label=" & drive.ShareName
Else
Line = Line & ", Label=" & drive.VolumeName
End If

Line = Line & ", FS=" & drive.FileSystem
Line = Line & ", Total=" & Int(drive.TotalSize/1000000)
Line = Line & ", Free=" & Int(drive.FreeSpace/1000000)
Line = Line & ", Available=" & Int(drive.AvailableSpace/1000000)
Line = Line & ", Serial=" & Hex(drive.SerialNumber)
End If

Line = Line & NewLine
Next
wscript.echo Line

Function ShowDriveType(Drive)
Select Case drive.DriveType
Case Removable
T = "Removable"
Case Fixed
T = "Fixed"
Case Network
T = "Network"
Case CDROM
T = "CD-ROM"
Case RAMDisk
T = "RAM Disk"
Case Else
T = "Unknown"
End Select
ShowDriveType = T
End Function



Posted by Dean Wells \(MVP\) on June 3rd, 2008


Not as backwards compatible as some solutions offered here but,
nonetheless, worth a mention -

C:\>fsutil fsinfo drives

Drives: C:\ D:\ E:\ Z:\

.... iterating through that list and using -

C:\>fsutil fsinfo drivetype <insert drive letter here>

.... will return greater detail regarding the type of drive assignment.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Bob I" <birelan@yahoo.com> wrote in message
news:er75LzXxIHA.5472@TK2MSFTNGP06.phx.gbl...


Posted by Pegasus \(MVP\) on June 3rd, 2008



"Dean Wells (MVP)" <dwells@maskmsetechnology.com> wrote in message
news:%23EsU3iYxIHA.5520@TK2MSFTNGP06.phx.gbl...
For reasons which I am unable to explain, Microsoft turned the
output of the command "fsutil fsinfo drives" into a set of $00-delimited
strings. The result is that I am unable to extract the individual
drive letters out of the string "Drives: C:\ D:\ E:\ F:\ Q:\ R:\",
using a batch file. Do you know of a way to do this?



Posted by Timo Salmi on June 3rd, 2008


joshboski <josh.burkholder@gmail.com> wrote:
53} How can I quietly test if a disk device is ready or not?
http://www.netikka.net/tsneti/info/tscmd053.htm

54} How can I get the type of a disk device?
http://www.netikka.net/tsneti/info/tscmd054.htm

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:ts@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Posted by Dean Wells \(MVP\) on June 3rd, 2008


Nod, I encountered the same limitation ... irritating isn't it.

Take a look at the 'bootSwitch.cmd' script here -

ftp://falcon.msetechnology.com/scrip...Switch.cmd.txt

.... it contains a means of resolving this behavior by exploiting an
all-too-often useful unicode oddity. Other ways were and probably still
are available though; didn't give it much thought after I came it that
one.

As I'm sure you know, part of the enjoyment of solving these kind of
annoyances is the creativity involved in making such a limited
environment play nicely ... this is one of my favs. to date.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Pegasus (MVP)" <I.can@fly.com.oz> wrote in message
news:evRJvyYxIHA.548@TK2MSFTNGP06.phx.gbl...


Posted by Herb Martin on June 4th, 2008



"Bob I" <birelan@yahoo.com> wrote in message
news:er75LzXxIHA.5472@TK2MSFTNGP06.phx.gbl...
I have been meaning to ask advice or figure the following out myself
so I will just post my solution and describe where it has problems.

The following code WORKS on Vista, but not on 2003, nor on
XP if I recall the latter correctly.

Following is all one line (in a batch file):

@for /f "tokens=1-26 delims=\ " %%a in ('fsutil fsinfo drives') do @for %%A
in (%%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n
%%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z) do @fsutil fsinfo drivetype
%%A


=======================
Ugly but it works on Vista like this:

A: - Remote/Network Drive
C: - Fixed Drive
D: - Fixed Drive
E: - Fixed Drive
F: - CD-ROM Drive
G: - CD-ROM Drive
H: - CD-ROM Drive
J: - Remote/Network Drive
O: - Remote/Network Drive
P: - Remote/Network Drive
Q: - Remote/Network Drive
R: - CD-ROM Drive
S: - Remote/Network Drive
T: - Remote/Network Drive
U: - Remote/Network Drive
W: - Remote/Network Drive
X: - Remote/Network Drive
Y: - Remote/Network Drive
Z: - Remote/Network Drive




Posted by Herb Martin on June 4th, 2008




"Herb Martin" <news@learnquick.com> wrote in message
news:OSAywudxIHA.2208@TK2MSFTNGP04.phx.gbl...

Ok, based on what someone (sorry) wrote in another message this
thread I finally figured out that XP/2003 were using NULLS instead
of spaces and so now my Perl version works on both all OSes:

Following is all one line for a batch file but works if pasted or typed in
directly:

@fsutil fsinfo drives | perl -n -e "@a=split /\s|\00/; foreach (@a) {next
unless s/\\//;print `fsutil fsinfo drivetype $_`;};"


OK, this works on 2003 using the | more trick (with +1 /S for efficiency):


@for /f %%a in ('fsutil fsinfo drives ^| more +1 /S') do @fsutil fsinfo
drivetype %%a


The MORE trick is problematic however if your command line is SMALLER
than the list of drives -- probably not a frequent issue but it will be an
issue
on rare occasions for some people.

I also admit that I cannot figure out Dean's unicode trick (quickly enough)
to extract and use it here.

--
Herb




Posted by Esra Sdrawkcab on June 4th, 2008


Timo Salmi wrote:

Posted by Herb Martin on June 4th, 2008



The following works on 2003 and Vista but is really slow (at least
on 2003) when it encounters a disconnected network drive letter that *IS*
mapped:

ALL ONE LINE FOLLOWING:

for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do @if
exist %%a:\lpt1 @fsutil fsinfo drivetype %%a:


This takes advantage of the pseudo file "LPT1" which has been
around since early DOS to prevent confusion with file names
that are the same as the Print device name (IIRC).

"if exist c:\lpt1" is TRUE if c:\ exists otherwise it is false.

I like the " | more +1 /S" best I think for 2003.

I thought I found a trick with FindStr that would convert the nulls
to dots, but it was just cosmetic on the screen and the pipe didn't
do that.

The following LOOKS good on the screen but isn't useful as far
as I can tell:

fsutil fsinfo drives | findstr \\

Is there anyway to get a NULL into "delims="????



Posted by Herb Martin on June 4th, 2008


Ok (after obsessing a bit) this work on (at least) XP/2003:

[Following is all one line for a batch file: drives.cmd]

@for /f %%a in ('fsutil fsinfo drives ^| find "\"') do @fsutil fsinfo
drivetype %%a


And this version works on (at least) Vista:
[Following is all one line for a batch file: drives.cmd]

@for /f "tokens=1-26 delims=\ " %%a in ('fsutil fsinfo drives') do @for %%A
in (%%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s
%%t %%u %%v %%w %%x %%y %%z) do @fsutil fsinfo drivetype %%A


Oddly enought the XP/2003 is not less awkward.

Output looks like this (with fix width font so it lines up there):

A: - Remote/Network Drive
C: - Fixed Drive
D: - Fixed Drive
E: - Fixed Drive
F: - CD-ROM Drive
G: - CD-ROM Drive
H: - CD-ROM Drive
J: - Remote/Network Drive
O: - Remote/Network Drive
P: - Remote/Network Drive
Q: - Remote/Network Drive
R: - CD-ROM Drive
S: - Remote/Network Drive
T: - Remote/Network Drive
U: - Remote/Network Drive
V: - Remote/Network Drive
W: - Remote/Network Drive
X: - Remote/Network Drive
Y: - Remote/Network Drive
Z: - Remote/Network Drive



"Herb Martin" <news@learnquick.com> wrote in message
news:%23ufNxshxIHA.3968@TK2MSFTNGP04.phx.gbl...


Posted by Dean Wells \(MVP\) on June 4th, 2008


It's always strange to me just how much pleasure I seem to derive from
working in this (let's face it) old & crappy little shell ... but,
seemingly like the rest of you, I'm quite happy doing so and will
readily jump on the 'let's see how many alternatives there are'
band-wagon whilst striving for the apparent golden-egg of batch syntax:
that ever elusive 'let's make it a one-liner.' Anyway, I came up with
this improvement on my original syntax, it too limits the number of
iterations resulting from the for loop to only those necessary (note
that it's a one-liner; well, sorta) -

fsutil fsinfo drives >"%TEMP%\%~n0.$$$" && for /f %%d in ('cmd /u /c
type "%TEMP%\%~n0.$$$" ^| more +11 ^| findstr "[A-Z]"') do fsutil fsinfo
drivetype %%d: & del "%TEMP%\%~n0.$$$" 2>nul

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Herb Martin" <news@learnquick.com> wrote in message
news:OSAywudxIHA.2208@TK2MSFTNGP04.phx.gbl...



Posted by foxidrive on June 4th, 2008


On Wed, 4 Jun 2008 09:14:38 -0400, "Dean Wells \(MVP\)"
<dwells@maskmsetechnology.com> wrote:

Here's more conventional syntax in my contribution after following this
thread:


@echo off
for /f "delims=" %%a in ('fsutil fsinfo drives^|more') do call :next "%%a"
goto :EOF
:next
set "var=%~1"
set "var=%var:~-3,2%"
fsutil fsinfo drivetype %var%
shift
if not %1.==. goto :next




Posted by Dean Wells \(MVP\) on June 4th, 2008


Hi, I think there's an error in there, at least from Vista's perspective
.... this is the result from Vista's shell <paste> -

C:\>foo

C:\>rem @echo off

C:\>for /F "delims=" %a in ('fsutil fsinfo drives |more') do call :next
"%a"

C:\>call :next "Drives: C:\ D:\ E:\ "

C:\>set "var=Drives: C:\ D:\ E:\ "

C:\>set "var=:\"

C:\>fsutil fsinfo drivetype :\
:\ - No such Root Directory

C:\>shift

C:\>if not . == . goto :next

C:\>goto :EOF

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"foxidrive" <gotcha@woohoo.invalid> wrote in message
news:4vfd44l75et0ks3p1ses92n0t5b7hicgkp@4ax.com...


Posted by foxidrive on June 4th, 2008


On Wed, 4 Jun 2008 12:45:20 -0400, "Dean Wells \(MVP\)"
<dwells@maskmsetechnology.com> wrote:

Nods.

It works under XP pro - I don't have Vista to test with.


Posted by Herb Martin on June 4th, 2008



"foxidrive" <gotcha@woohoo.invalid> wrote in message
news:srnd449nnacjl4hfbheqcuar107o5gonr8@4ax.com...

I have some extra *personal* criteria:

General principle: Runs on anybody's machine (where it makes sense)

No temp files if they can be avoided
No VBScript if it can be avoided -- once necessary go to Perl or Ruby
(But ONLY Perl is on ALL of my machines though Ruby is gaining
ground)
No external utilities if they can be avoided, Support tools not as bad as
other
stuff, Reskit, then UnxUtils, and some of my own personal tools, then
Cygwin

Prefer not to use environment variables, but this is not a rule
Once line when possible but don't get crazy
Working as a DosKey macros is desirable
(Although I use this for starting externa programs that aren't on the
path too)
Preferably works on all versions of the OS (where the program makes sense)

If it is too difficult for those rules then use Perl, Ruby, or a custom C/C#
program

So repeating my best efforts from last night:

[Following is all one line for a batch file: drives.cmd]

@for /f %%a in ('fsutil fsinfo drives ^| find "\"') do @fsutil fsinfo
drivetype %%a


And this version works on (at least) Vista:
[Following is all one line for a batch file: drives.cmd]

@for /f "tokens=1-26 delims=\ " %%a in ('fsutil fsinfo drives') do @for %%A
in (%%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s
%%t %%u %%v %%w %%x %%y %%z) do @fsutil fsinfo drivetype %%A


Oddly enought the XP/2003 is not less awkward.

Output looks like this (with fix width font so it lines up there):

A: - Remote/Network Drive
C: - Fixed Drive
D: - Fixed Drive
E: - Fixed Drive
F: - CD-ROM Drive
G: - CD-ROM Drive
H: - CD-ROM Drive
J: - Remote/Network Drive
O: - Remote/Network Drive
P: - Remote/Network Drive
Q: - Remote/Network Drive
R: - CD-ROM Drive
S: - Remote/Network Drive
T: - Remote/Network Drive
U: - Remote/Network Drive
V: - Remote/Network Drive
W: - Remote/Network Drive
X: - Remote/Network Drive
Y: - Remote/Network Drive
Z: - Remote/Network Drive






Posted by Herb Martin on June 4th, 2008



"Dean Wells (MVP)" <dwells@maskmsetechnology.com> wrote in message
news:%23j5g1TkxIHA.1772@TK2MSFTNGP03.phx.gbl...
Cool. I never did understand (or really isolate) your Unicode trick
from that big batch file -- the one to remove the NULLS.

I ended up using a pipe to find on XP -- after using more as the
next best thing but admitting that more will fail (or at least ask for
key to continue) if there are more drives than screen lines.)

This last isn't a giant deal as I keep my screen MUCH bigger than
the maximum (?) 26 drive letters but not everyone does all of the
time.





Similar Posts