473,805 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Name/ID of removable Media: how?

I am trying to find out (using Python under windows) the name of a CD that
is currently in the drive specified by a path name.

And while I am at it, I'd also like to know whether the specified drive
contains a CD at all, and whether the drive is actually a CD drive.

AFAIK, Python doesn't provide a way to do it, and a search in the web
yielded only soutions for Linux.

Can anyone show me a solution that works with windoze?

TIA,

Heiko
Jul 19 '05 #1
4 2902
There are sure thousand ways
of doing it "with windoze".
Here one of them (NOT tested) in form
of code snippets you can rearrange
for your purpose:

import win32com.client
axFSO = win32com.client .Dispatch("Scri pting.FileSyste mObject") # SCRRUN.DLL
axLstDrives = axFSO.Drives

dctAXaxFSO_NumC odeAsKeyVsTypeD escrText = {
0 : "unknown type of drive"
, 1 : "drive with removable medium (e.g. Floppy)"
, 2 : "fixed drive (e.g. harddisk)"
, 3 : "remote (i.e. network) drive"
, 4 : "CD-ROM drive"
, 5 : "RAM-Disk drive"
}

lstdriveLetterO FonSYSTstorageD evice = []
lstdriveTypeOFo nSYSTstorageDev ice = []

for axDrive in axLstDrives:
if(axDrive.IsRe ady): # checks if a CD is inserted
lstdriveLetterO FonSYSTstorageD evice.append(
axDrive.DriveLe tter.encode()
)
lstdriveTypeOFo nSYSTstorageDev ice.append(
dctAXaxFSO_NumC odeAsKeyVsTypeD escrText[axDrive.DriveTy pe]
)
# axDrive.SerialN umber
# axDrive.VolumeN ame.encode()
# for more of this kind just check out e.g. in the by Microsoft
# free available JScript tutorial the chapter
# "FileSystemObje ct Sample Code"
#:if
#:for

Hope this helps.

Claudio

"Heiko Selber" <he**********@s iemens.com> schrieb im Newsbeitrag
news:d4******** **@news.mch.sbs .de...
I am trying to find out (using Python under windows) the name of a CD that
is currently in the drive specified by a path name.

And while I am at it, I'd also like to know whether the specified drive
contains a CD at all, and whether the drive is actually a CD drive.

AFAIK, Python doesn't provide a way to do it, and a search in the web
yielded only soutions for Linux.

Can anyone show me a solution that works with windoze?

TIA,

Heiko

Jul 19 '05 #2
Hi All--

Heiko Selber wrote:

I am trying to find out (using Python under windows) the name of a CD that
is currently in the drive specified by a path name.

And while I am at it, I'd also like to know whether the specified drive
contains a CD at all, and whether the drive is actually a CD drive.

AFAIK, Python doesn't provide a way to do it, and a search in the web
yielded only soutions for Linux.

Can anyone show me a solution that works with windoze?


This works. The only thing you have to do is stuff a floppy into the
drive & find out what the fstype is (that's inf[-1] in the code below)
so you can key on it. Try the docs for Mark Hammond's Win32, and
there's always his _Python Programming on Win32_.

import os
import os.path
import win32api

def findCDs():
cdDrives=[]
print "Searching for cd drives..."
drives=win32api .GetLogicalDriv eStrings().spli t(":")
for i in drives:
dr=i[-1].lower()
if dr.isalpha():
dr+=":\\"
inf=None
try:
inf=win32api.Ge tVolumeInformat ion(dr)
except:
pass # Removable drive, not ready
if inf!=None:
if inf[-1].lower().endswi th("cdfs"):
cdDrives.append ([dr,inf])
elif inf[-1].lower().endswi th("udf"):
cdDrives.append ([dr,inf])
return cdDrives

inf[0] contains the volume label if there is one and if there's a CD
loaded:
win32api.GetVol umeInformation( "c:\\") ('God_C', -798323922, 255, 459007, 'NTFS')


Note that you must use the full drive spec, "letter:\\" or
GetVolumeInform ation() doesn't always work.

There are probably better ways to do these things, but they do work;
I've been using them constantly the last few days.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #3
Hi All--
Tim's wmi stuff looked interesting, so I tried it out, and now I have a
question.
-----
#!/usr/bin/python

import wmi
import win32api
c=wmi.WMI()
for i in c.Win32_CDROMDr ive():
v=i.VolumeSeria lNumber
print "WMI serial",v,long( v,0x10)

vn,sn,ln,flg,fs type=win32api.G etVolumeInforma tion("d:\\")
print "win32api serial",sn,long (sn)
----

The output from the above script (drive d contains cd) is:

WMI serial D0ADBEE7 3501047527

win32api serial -793919769 -793919769
What's the difference between the two serial numbers? WMI is returning
a long converted to a hex repr string, while win32api is returning an
int (type(sn) is <type 'int'>), & converting to hex bears no resemblance
to what WMI shows. What am I missing?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #4
Maybe the function below can help?
I suppose WMI returns same values as FSO (File System Object):

def strDriveHexSeri alNumberFromFSO intRetVal(intDr iveSerialNumber ByFSO):
"""Supplied with an integer representing a drive serial number returns the
number in same format as \>dir command does (i.e. ####-####)."""
# The value of axDrive.SerialN umber property (an int or float number) can
be
# negative, so to get always a positive number as listed by e.g. \>dir
# command it is necessary to apply a workaround:
if(intDriveSeri alNumberByFSO < 0):
# dec 4294967295 == hex FFFFFFFF, dec 4294967296 == hex 100000000
intDriveSerialN umber = 4294967296L + intDriveSerialN umberByFSO
else:
intDriveSerialN umber = intDriveSerialN umberByFSO
#:if/else
strHexSerialNum ber = "%08X"%intDrive SerialNumber
strHexSerialNum ber = strHexSerialNum ber[:4]+"-"+strHexSerialN umber[4:]
return strHexSerialNum ber
#:def strDriveHexSeri alNumberFromFSO intRetVal(intDr iveSerialNumber ByFSO)

In your case
strDriveHexSeri alNumberFromFSO intRetVal(-793919769):
gives:
D0AD-BEE7

Claudio

"Ivan Van Laningham" <iv*****@pauaht un.org> schrieb im Newsbeitrag
news:ma******** *************** *************** @python.org...
Hi All--
Tim's wmi stuff looked interesting, so I tried it out, and now I have a
question.
-----
#!/usr/bin/python

import wmi
import win32api
c=wmi.WMI()
for i in c.Win32_CDROMDr ive():
v=i.VolumeSeria lNumber
print "WMI serial",v,long( v,0x10)

vn,sn,ln,flg,fs type=win32api.G etVolumeInforma tion("d:\\")
print "win32api serial",sn,long (sn)
----

The output from the above script (drive d contains cd) is:

WMI serial D0ADBEE7 3501047527

win32api serial -793919769 -793919769
What's the difference between the two serial numbers? WMI is returning
a long converted to a hex repr string, while win32api is returning an
int (type(sn) is <type 'int'>), & converting to hex bears no resemblance
to what WMI shows. What am I missing?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2606
by: Garet Cammer | last post by:
Hi Everyone, I am writing a program to perform backup operations using the VB scripting File System Object to copy to removable media storage devices. The backup operation works fine, but when we attempt to remove the backup device, we often get a "Delayed Write Failed" error message, indicating that data was cached and the copy was not actually finished before device removal. The problem now is how can we guarantee that all data is...
138
6617
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and apparatus for displaying information regarding stored data in a gaming system': 'When saving a game,...
1
451
by: Cliff Liao | last post by:
Hi all, I am trying to install the .NET framework 1.1 runtime package on Windows 2000 SP4. While the installation starts well, I get the following message box during the installation process : Please insert the disk : Microsoft .NET Framework (English) : OK Cancel Pressing the OK button will check the floppy drive for a disk and
0
2246
by: Ryan | last post by:
I'm writing a utility that needs to be aware of media (CD, DVD, Zip disk, usb stick, etc) insertions and removals. Which seems to be more of a problem than I original imagined. I can't use any auto-run features because this is actually running on Windows XP Embedded... so that's the first thing to be aware of. Using WM_DEVICECHANGE works about half the time...that is, it works when a DVD or CD is inserted, but it does not work for Zip...
3
5713
by: Max | last post by:
Not sure exactly how to describe this or if it's even possible, so I'll do my best and would appreciate some suggestions. I'm currently working on a service that will allow me to automate some file and folder manipulations. While this question is not concerning the primary purpose of the program, I think it would be a nice addition. What I'd like to be able to do is have the service be able to "detect" whenever the user attaches some...
0
1074
by: Allen | last post by:
Dim DiskMO As New ManagementObject("win32_logicaldisk.DeviceID=""" & sDrive & """") Then using DiskMO("DriveType") Case DRIVE_TYPES_REMOVABLE snip...
3
3897
by: Mike Joyce | last post by:
I am trying to write a portable script that will find removable media, such as compact flash, sd card, usb, etc. drive and then upload files from the media. I want this to be portable so that I can write and maintain one program for both Linux and Windows. Each platform uses different functions so even if I could find two platform dependent functions that would be fine. Basically, I would like to avoid checking fixed disks if possible. If...
7
2293
by: glenn | last post by:
Hi can anyone tell me how given a directory or file path, I can pythonically tell if that item is on 'removable media', or sometype of vfs, the label of the media (or volume) and perhaps any other details about the media itself? thanks Glenn
4
2910
by: Per Juul Larsen | last post by:
Hi my VB program must always start from a removable disk. In addition, on the removable media to be images of the type *. jpg *. tips. If not the two conditions are present, the programme will end with a message. How do I write this little control function in Visual Basic regards pjl
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.