Batch to detect removeable media?

M

MikeYates

IF EXIST E: ECHO Drive E: exists

If E: is a hard-drive partition, the echo occurs.
If E: is a CD-rom with a disc inserted, the echo occurs,
but if there is no CD inserted, the script hangs and a separate window
says
"There is no disk in the drive. Please insert a disk into drive E:."
"Cancel" "Try Again" "Continue"
so "if exists" cannot be used to enumerate drives in an unattended
script.

What methods for doing so can you suggest?
 
G

Guest

Mike,

What's the point? Unless it's run as a service you will always have to
manually launch it to get the results
 
M

MikeYates

Mike,

What's the point? Unless it's run as a service you will always have to
manually launch it to get the results
Haven't your heard of the scheduler?
Or netlogon scripts in a domain?

I use many batch jobs for backup and auditing in small company
domains. Often they have to include all available partitions, maybe C:
and D: or C: and E: if the CD is D:
BTW
for %%A in (A B C D E F G H I) if exist %%A: call subroutine %%A
skips B: silently if there is only one floppy drive, despite
interchangeability, and only gives the "Insert a disc" dialogue once
if A: is empty.
In Win9x this was not a problem, as the prompt was in the same window
and enter keys could be stacked.
 
M

MikeYates

Meanwhile...
I've found it in Timo Salmi's http://garbo.uwasa.fi/pub/pc/link/tscmd.zip

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 (
dir %%a:\ 2>&1 | findstr /b /i /r /c:" Volume in drive ")

Until I tried that, DIR was also giving the separate dialog.
It seems that any "2>" stops it!
 
P

Pegasus [MVP]

MikeYates said:
IF EXIST E: ECHO Drive E: exists

If E: is a hard-drive partition, the echo occurs.
If E: is a CD-rom with a disc inserted, the echo occurs,
but if there is no CD inserted, the script hangs and a separate window
says
"There is no disk in the drive. Please insert a disk into drive E:."
"Cancel" "Try Again" "Continue"
so "if exists" cannot be used to enumerate drives in an unattended
script.

What methods for doing so can you suggest?

It is not clear from your post what you actually expect your batch file to
do when it detects a drive. Anyway, here are a couple of options that you
can tweak to suit your exact requirements. The first is a VB Script file.
You must save it as c:\Mike.vbs (for example), remove the line numbers, then
invoke it like so:
cscript //nologo c:\Mike.vbs

The second is the same script but wrapped into a batch file. Again you must
remove the line numbers.

VB Script File:
01. aDType = split("Unknown Removable Fixed Network CDROM RAMDISK")
02. Set oFSO = CreateObject("Scripting.FileSystemObject")
03. LF = Chr(10)
04.
05. sLine = "Drive Type State Label F/S Size
Free Serial"
06. WScript.Echo sLine
07. WScript.Echo String(Len(sLine)+2, "-")
08. For Each oDrive In oFSO.Drives
09. D = oDrive.DriveType
10. if D > UBound(aDType) then D = 0
11. sLine = oDrive.Path & " " & aDType(D)
12.
13. If oDrive.IsReady Then
14. sLine = Append(sLine, "ready", 18, False)
15. If oDrive.DriveType = 3 Then
16. sLine = Append(sLine, Left(oDrive.ShareName, 13), 29, False)
17. Else
18. sLine = Append(sLine, Left(oDrive.VolumeName, 13), 29, False)
19. End If
20.
21. sLine = Append(sLine, oDrive.FileSystem, 44, False)
22. sLine = Append(sLine, Int(oDrive.TotalSize/1000000), 51, True)
23. sLine = Append(sLine, Int(oDrive.FreeSpace/1000000), 59, True)
24. ' sLine = sLine & "Available=" & Int(oDrive.AvailableSpace/1000000)
25. sLine = Append(sLine, Hex(oDrive.SerialNumber), 67, False)
26. Else
27. sLine = Append(sLine, "not ready", 18, False)
28. End If
29. WScript.Echo sLine
30. Next
31.
32. Function Append(L, S, n, numerical)
33. if n < Len(L) + 2 then n = 2 Else n = n - Len(L)
34. If numerical Then
35. Append = L & Left(Space(60), n + 6 - Len(S)) & S
36. Else
37. Append = L & Left(Space(60), n) & S
38. End If
39. End Function

Batch file:
01. @echo off
02. set V=c:\TempVBS.vbs 2>c:\TempVBS.vbs
03. echo >> %V% aDType = split("Unknown Removable Fixed Network CDROM
RAMDISK")
04. echo >> %V% Set oFSO = CreateObject("Scripting.FileSystemObject")
05. echo >> %V% LF = Chr(10)
06. echo >> %V% sLine = "Drive Type State Label F/S
Size Free Serial"
07. echo >> %V% WScript.Echo sLine
08. echo >> %V% WScript.Echo String(Len(sLine)+2, "-")
09. echo >> %V% For Each oDrive In oFSO.Drives
10. echo >> %V% D = oDrive.DriveType
11. echo >> %V% if D ^> UBound(aDType) then D = 0
12. echo >> %V% sLine = oDrive.Path ^& " " ^& aDType(D)
13. echo >> %V% If oDrive.IsReady Then
14. echo >> %V% sLine = Append(sLine, "ready", 18, False)
15. echo >> %V% If oDrive.DriveType = 3 Then
16. echo >> %V% sLine = Append(sLine, Left(oDrive.ShareName, 13), 29,
False)
17. echo >> %V% Else
18. echo >> %V% sLine = Append(sLine, Left(oDrive.VolumeName, 13), 29,
False)
19. echo >> %V% End If
20. echo >> %V% sLine = Append(sLine, oDrive.FileSystem, 44, False)
21. echo >> %V% sLine = Append(sLine, Int(oDrive.TotalSize/1000000), 51,
True)
22. echo >> %V% sLine = Append(sLine, Int(oDrive.FreeSpace/1000000), 59,
True)
23. echo >> %V% ' sLine = sLine ^& "Available=" ^&
Int(oDrive.AvailableSpace/1000000)
24. echo >> %V% sLine = Append(sLine, Hex(oDrive.SerialNumber), 67, False)
25. echo >> %V% Else
26. echo >> %V% sLine = Append(sLine, "not ready", 18, False)
27. echo >> %V% End If
28. echo >> %V% WScript.Echo sLine
29. echo >> %V% Next
30. echo >> %V% Function Append(L, S, n, numerical)
31. echo >> %V% if n ^< Len(L) + 2 then n = 2 Else n = n - Len(L)
32. echo >> %V% If numerical Then
33. echo >> %V% Append = L ^& Left(Space(60), n + 6 - Len(S)) ^& S
34. echo >> %V% Else
35. echo >> %V% Append = L ^& Left(Space(60), n) ^& S
36. echo >> %V% End If
37. echo >> %V% End Function
38. cscript //nologo %V%
39. del %V%
 
M

MikeYates

Thanks Pegasus and Yandl!
Although I prefer Timo Salmi's pure batch solution (perhaps more
version-resilient) your VBscript solutions are very useful.
Oddly, although shorter, Yandl's is harder to get syntactically
correct and I'm still struggling, but Pegasus's works brilliantly.
Note the recognition of IFS-EXT2 drives with correct volume-IDs (and
my testing-PC's versatility) though USB drives are shown "Fixed" and
it's a pity the neat tab expansion in a Cmd Window is lost here:-

C:\Users\mike>cscript /nologo tst1.vbs
Drive Type State Label F/S Size Free
Serial
--------------------------------------------------------------------------
A: Removable ready DISK 1 FAT 1 0
1C2A0D0F
C: Fixed ready Vista NTFS 33355 14484
BC3873B7
D: Fixed ready MYWINME FAT32 20479 7376
1E4B1606
E: Fixed ready WIN311 FAT 1496 666
36946068
F: Fixed ready winxp NTFS 5160 494
681E5DB1
G: Fixed ready XPhome NTFS 18986 3651
D48AA137
H: Fixed ready CDS FAT32 32062 8268
D4D803F6
I: Fixed ready SpareNTFS NTFS 86505 12709
9C472B03
J: Fixed ready BIG FAT32 37696 9349
42F7491D
K: CDROM ready 2009-Free-i58 CDFS 4594 0
984915C
L: Fixed ready SuSE Ext2 10001 5120
A7085A4A
M: Fixed ready CADDYFAT FAT32 10000 9988
4980DFD4
N: Fixed ready CaddyExt3 Ext2 110021 10797
BCDD394A
O: Fixed ready MAXTOR FAT32 120002 24631
17EA3758
S: Fixed ready Space Ext2 95413 4656
E9ECAAC6#
C:\Users\mike>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top