Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read an unique identification number of pendrive

Status
Not open for further replies.

manikulus

Technical User
Mar 3, 2010
5
DE
Hello everybody!

Is it possible to get an unique identification number for pendrive in Fortran?

Many thanks for answer.
 
What do you mean with the unique identification number?
Is this the volume serial number which will be displayed in the output of the command dir in windows (in the second line)?
For example:
Code:
 Volume Serial Number is [COLOR=red]0682-5FE5[/color]
Or did you mean something other?
 
I would like to get Disk Serial Number (not the Volume serial number) of a Generic USB/USB Flash Drive/USB Pen Drive.
Ie: Serial Number: 0013728EE05CF941E62B03E3 - it can be checked for instance with freeware CheckUDisk – USB Flashdrive Information Tool

 
Do you wish to do this in Windows, Linux or some other OS. Every OS treats pendrives differently.
 
I thing one way (and the only known for me) would be to execute the command which returns you the id, redirecting it's output into a text file and then read the text file in fortran and parse the id form it.
For example if I want to get a volume serial number of E: I would first execute in fortran

system("dir e: > dir_out.txt")

and then read the file dir_out.txt and parse the number 0682-5FE5 out form the second line:
Code:
Volume Serial Number is 0682-5FE5
 
ok, it looks nice - but it is volume serial number but I need serial number for USB device. Now when I'm still searching for the solutions I've found (I think so) solution but in VB - from vbforums.com:

vb Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colitems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", , 48)
For Each objitem In colitems
If objitem.interfacetype = "USB" Then sernum = objitem.pnpdeviceid
Next
sernum = Mid(sernum, InStrRev(sernum, "\") + 1)
sernum = Left(sernum, InStr(sernum, "&") - 1)

So it seems to be that the serial number is embedded in the PnPDeviceID. How to get it in file?

 
With your modified Visual Basic Script (VBS) example code
serial.vbs
Code:
strComputer [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"."[/color]
[COLOR=#804040][b]set[/b][/color] objWMIService [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]GetObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"winmgmts:\\"[/color] [COLOR=#804040][b]&[/b][/color] strComputer [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"\root\CIMV2"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]set[/b][/color] colitems [COLOR=#804040][b]=[/b][/color] objWMIService[COLOR=#804040][b].[/b][/color]ExecQuery[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"SELECT * FROM Win32_DiskDrive"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]48[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]set[/b][/color] stdout [COLOR=#804040][b]=[/b][/color] WScript[COLOR=#804040][b].[/b][/color]StdOut

[COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] objitem [COLOR=#804040][b]in[/b][/color] colitems
  [COLOR=#804040][b]if[/b][/color] objitem[COLOR=#804040][b].[/b][/color]interfacetype [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"USB"[/color] [COLOR=#804040][b]then[/b][/color] sernum [COLOR=#804040][b]=[/b][/color] objitem[COLOR=#804040][b].[/b][/color]pnpdeviceid
[COLOR=#804040][b]next[/b][/color]

sernum [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]mid[/b][/color][COLOR=#804040][b]([/b][/color]sernum[COLOR=#804040][b],[/b][/color] InStrRev[COLOR=#804040][b]([/b][/color]sernum[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"\"[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color][COLOR=#804040][b])[/b][/color]
sernum [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]left[/color][COLOR=#804040][b]([/b][/color]sernum[COLOR=#804040][b],[/b][/color] [COLOR=#008080]InStr[/color][COLOR=#804040][b]([/b][/color]sernum[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"&"[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]-[/b][/color] [COLOR=#ff00ff]1[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]'Write to the standard utput[/color]
stdout[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]writeline[/color] sernum
you can write to the standard output
Code:
cscript serial.vbs
and then you can redirect the output to a file
Code:
cscript serial.vbs > serial.txt
After that the output file serial.txt contains in my case
Code:
Microsoft (R) Windows Script Host verzia 5.7
Copyright (C) Microsoft Corporation 1996-2001. Vçetky pr va vyhraden‚.

[COLOR=red]058F312D81B1[/color]
Now you can parse the resulting file with Fortran.

But with VBS it should be possible to write directly to a file too, without a need of redirecting standard output.
 
Thanks for help with VBS code.
I'm still searching for some subroutine in Fortran to have the same - to ask windows of PnPDeviceID of plugged usb device to my pc. When I find something I will post here.
 
If you change the script to

cscript /nologo serial.vbs > serial.txt

you'll get just the number.
 
manikulus, I don't think that will you find such funcionality in Fortran. Fortran is fo scientific computing, not for OS programming.
The command line switch suggested by xwb simplifies the output to one line, so reading by fortran would be simple.
 
If you know which library the routine is in, you can always use LoadLibrary on the DLL with Cray pointers to get at the routine then just call the pointer with the relevant parameters. That is basically the Windows workaround for most languages.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top