QB64.org Forum

Active Forums => Programs => Topic started by: euklides on July 12, 2019, 05:09:44 am

Title: Working with files located on USB devices
Post by: euklides on July 12, 2019, 05:09:44 am
When you write a program using files located on USB drives, you don't
'be sure that the letter of this drive is always the same.
So you can use this little program



Code: QB64: [Select]
  1.  
  2. 'LETTER_OF_USB_DEVICE  'program by Euklides
  3. 'Working with files on USB devices
  4.  
  5. 'When you write a program using files located on USB drives, you don't be
  6. 'sure that the letter of this drive is always the same.
  7. 'You cannot write for instance:
  8.  
  9. '         OPEN "F:\MYFILE\info.txt" FOR INPUT AS #1
  10. '         ...
  11.  
  12. 'If your drive has a new letter, for instance "E", you must go into your program
  13. ' and change the letter
  14. 'So what you can do is this:
  15.  
  16. '1) '  find the serial of your drive
  17. '  Use below:
  18.  
  19. '         GOSUB SERIALSSHOW
  20.  
  21. '  and you will see all the serials of then actuel connected drives
  22. '  For instance, the good drive has the serial "<9911-447E>
  23.  
  24. '2) Now write your program so:
  25.  
  26. '        fic$ = "<9911-447E>:\MYFILE\info.txt"
  27. '        GOSUB DRIVELETTERFIND: IF _CLIPBOARD$ = "" THEN PRINT "USB unit is not connected for using " + fic$: END
  28. '        fic$ = _CLIPBOARD$:OPEN fic$ FOR INPUT AS #1
  29. '       ...
  30.  
  31.  
  32.  
  33.  
  34. '=====================================================================
  35. FUNCTION GetVolumeInformationA& (lpRootPathName$, lpVolumeNameBuffer$, BYVAL nVolumeNameSize~&, _
  36.     lpVolumeSerialNumber~&, lpMaximumComponentLength~&, lpFileSystemFlags~&, lpFileSystemNameBuffer$, BYVAL nFileSystemNameSize&)
  37. DECLARE LIBRARY: FUNCTION GetDriveType& (d$): END DECLARE
  38. DIM SHARED DriveType AS STRING, SERIALFOUND AS STRING
  39. '---
  40. SERIALSSHOW:
  41. FOR q = 1 TO 26: X = GetFileInfo(q): IF SERIALFOUND <> "<!!!-!!!>" THEN PRINT " "; CHR$(64 + q) + ":    "; SERIALFOUND
  42. '---
  43. DRIVELETTERFIND: 'in-->fic$ like' "<SER-IAL>ficname"  out--> like "D:\ficname"
  44. _CLIPBOARD$ = "": K$ = UCASE$(LEFT$(fic$, 2))
  45. IF RIGHT$(K$, 1) = ":" AND LEFT$(K$, 1) >= "A" AND LEFT$(K$, 1) <= "Z" AND DRIVEEXISTS(ASC(K$) - 64) = 1 THEN _CLIPBOARD$ = fic$: RETURN
  46. J1 = INSTR(fic$, "<"): J2 = INSTR(J1, fic$, ">")
  47. IF J1 = 0 OR J2 = 0 THEN PRINT fic$; " must be written like: <serial>\ficmame...": END
  48. Serialsearch$ = MID$(fic$, J1, J2 - J1 + 1): q = 0
  49. FOR q = 1 TO 26: X = GetFileInfo(q)
  50.     IF SERIALFOUND = Serialsearch$ THEN
  51.         fic$ = RIGHT$(fic$, LEN(fic$) - J2): IF LEFT$(fic$, 1) <> ":" THEN fic$ = ":" + fic$
  52.         fic$ = CHR$(64 + q) + fic$: _CLIPBOARD$ = fic$
  53.     END IF
  54. '---
  55. FUNCTION GetFileInfo (D)
  56.     SERIALFOUND = "<!!!-!!!>":
  57.     IF DRIVEEXISTS(D) <> 1 THEN GetFileInfo = 0: EXIT FUNCTION
  58.     Dname$ = CHR$(D + 64) + ":\": Sname$ = SPACE$(260)
  59.     R = GetVolumeInformationA(Dname$ + CHR$(0), Vname$, 260, serial~&, empty1~&, empty2~&, Sname$, 260)
  60.     IF R = 0 THEN EXIT FUNCTION
  61.     Sname$ = LEFT$(HEX$(serial~&), 4) + "-" + RIGHT$(HEX$(serial~&), 4)
  62.     SERIALFOUND = "<" + Sname$ + ">"
  63.     GetFileInfo = -1
  64. '---
  65. FUNCTION DRIVEEXISTS (V)
  66.     DRIVEEXISTS = 0: varX$ = CHR$(V + 64) + ":\" + CHR$(0): VarX = GetDriveType(varX$): IF VarX > 1 THEN DRIVEEXISTS = 1
  67. '=====================================================================
  68.  
  69.  
Title: Re: Working with files located on USB devices
Post by: RhoSigma on July 12, 2019, 05:40:13 am
That's a nice method euklides.
As the serial is usually the volume name I assume it will not work if you rename the USB device in between?
Guess I have to use the new name then instead of the serial.
Title: Re: Working with files located on USB devices
Post by: euklides on July 12, 2019, 06:04:26 am
I think "serial" and "volume name" are not the same thing... Or not ?

I did the test: changing the volume name does not change the serial !
Title: Re: Working with files located on USB devices
Post by: RhoSigma on July 12, 2019, 06:36:47 am
Well that's good to know. Not home now, otherwise I had test it myself :)

Thanks, there's certainly a good use for this in any projects.
Title: Re: Working with files located on USB devices
Post by: Pete on July 12, 2019, 11:33:32 am
In 1990-something, I used to use QBASIC error trapping and go through drive letters D-Z, until the drive was discovered. In my old 486 Win 3.1, I used to detect the A or B floppy drive, using a similar alphabetical trapping method. Ah, 1.44 megabytes of memory, all that anyone would ever need!

Pete
Title: Re: Working with files located on USB devices
Post by: Petr on July 12, 2019, 11:41:40 am
I wonder if this nice and useful code will be added to the "toolbox"  here in the forum!
Title: Re: Working with files located on USB devices
Post by: Pete on July 12, 2019, 11:51:25 am
I haven't seen Bill "The Toolbox" Static in ages.

Pete