Author Topic: Working with files located on USB devices  (Read 4991 times)

0 Members and 1 Guest are viewing this topic.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Working with files located on USB devices
« 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.  
Why not yes ?

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Working with files located on USB devices
« Reply #1 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Working with files located on USB devices
« Reply #2 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 !
« Last Edit: July 12, 2019, 06:16:19 am by euklides »
Why not yes ?

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Working with files located on USB devices
« Reply #3 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Working with files located on USB devices
« Reply #4 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Working with files located on USB devices
« Reply #5 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!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Working with files located on USB devices
« Reply #6 on: July 12, 2019, 11:51:25 am »
I haven't seen Bill "The Toolbox" Static in ages.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/