Author Topic: Is there an easy way to retrieve a volume label in QB64?  (Read 3814 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Is there an easy way to retrieve a volume label in QB64?
« on: December 11, 2020, 02:46:57 pm »
My goal is to be able to retrieve the volume label of a disk (HD, flash drive, etc.) place that into a string variable.

I know that I could run a command line command via the shell command, redirect the output to a file, then read the contents of that file in QB64, but I'm wondering if there is a simpler way to do this.

Example:

From the "vol d:" command below, we can see that the volume label of the D: drive is "USB WINBOOT". I want to be able to grab that label (USB WINBOOT) and place it in a string variable.

C:\>vol d:
 Volume in drive D is USB WINBOOT
 Volume Serial Number is 8861-3C84

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #1 on: December 11, 2020, 03:16:03 pm »
Funny, I was trying for an hour to figure out a good way to do this. Minutes after my post I found a way:

SHELL "vol D: | CLIP"
Temp$ = _CLIPBOARD$
' Write code here to parse Temp$ for the info wanted.


You could also get fancier by saving the old contents of the clipboard and restoring it so that the user doesn't loose the clipboard contents like this:

SavedClip = _CLIPBOARD$

SHELL "vol D: | CLIP"
Temp$ = _CLIPBOARD$
' Write code here to parse Temp$ for the info wanted.

_CLIPBOARD$ = SavedClip$


EDIT: The idea of saving the initial contents of the clipboard was a bad idea. If what is stored in the clipboard initially is an image that won't work since QB64 is saving the contents as text to a string.
« Last Edit: December 11, 2020, 03:24:21 pm by hanness »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #2 on: December 11, 2020, 03:19:12 pm »
When I get home I'll share some code with you that works much quicker than using the clipboard
Shuwatch!

Marked as best answer by hanness on December 12, 2020, 07:54:32 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #3 on: December 11, 2020, 04:23:22 pm »
@hanness

Here you go. Doesn't use the clipboard so it should work faster plus this library can be used to store ANY console output like command prompt to a variable or print to the screen. Works better than outputting to a file or using the clipboard. Just put the header file with your source code and you will be golden. The header file will compile with the rest of your source and won't be needed if you distribute just the executable. Also, this library works with ALL systems. Linux, Mac, and Windows.

Code: QB64: [Select]
  1. OPTION _EXPLICIT 'force of habit. I use this quite often
  2.  
  3. DECLARE LIBRARY ".\pipecom"
  4.     FUNCTION pipecom$ (cmd AS STRING)
  5.  
  6. DIM volumelabel AS STRING
  7. volumelabel = pipecom("vol L:")
  8.  
  9. volumelabel = MID$(volumelabel, INSTR(volumelabel, " is ") + 4) 'Trimming the result
  10. volumelabel = MID$(volumelabel, 1, INSTR(volumelabel, CHR$(10)) - 1)
  11. PRINT volumelabel

Also, I did a test between my library and using _CLIPBOARD$ for this command specifically. My library got the volume label and trimmed it in 0.03125 seconds whereas using SHELL with clip took 0.1992188 seconds without changing the string.
« Last Edit: December 11, 2020, 04:48:07 pm by SpriggsySpriggs »
Shuwatch!

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Why not yes ?

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #5 on: December 12, 2020, 12:54:13 pm »
Thanks for the help. Very much appreciated!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #6 on: December 14, 2020, 01:46:22 pm »
@hanness

Another way you can get the volume label (Windows only)

Code: QB64: [Select]
  1.  
  2.     FUNCTION GetVolumeInformation% ALIAS GetVolumeInformationA (lpRootPathName AS STRING, BYVAL lpVolumeNameBuffer AS _OFFSET, BYVAL nVolumeNameSize AS LONG, BYVAL lpVolumeSerialNumber AS _OFFSET, BYVAL lpMaximumComponentLength AS _OFFSET, BYVAL lpFileSystemFlags AS _OFFSET, BYVAL lpFileSystemNameBuffer AS _OFFSET, BYVAL nFileSystemNameSize AS LONG)
  3.  
  4. PRINT VolumeName("L:\")
  5.  
  6. FUNCTION VolumeName$ (volumeletter AS STRING)
  7.     DIM volume AS STRING
  8.     volume = SPACE$(260)
  9.     IF GetVolumeInformation(volumeletter + CHR$(0), _OFFSET(volume), LEN(volume), 0, 0, 0, 0, 0) THEN
  10.         VolumeName = LTRIM$(volume)
  11.     END IF
« Last Edit: December 14, 2020, 01:50:20 pm by SpriggsySpriggs »
Shuwatch!

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Is there an easy way to retrieve a volume label in QB64?
« Reply #7 on: December 14, 2020, 04:29:18 pm »
Thanks for that!