Active Forums => QB64 Discussion => Topic started by: hanness on December 11, 2020, 02:46:57 pm
Title: Is there an easy way to retrieve a volume label in QB64?
Post by: hanness 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
Title: Re: Is there an easy way to retrieve a volume label in QB64?
Post by: hanness 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.
Title: Re: Is there an easy way to retrieve a volume label in QB64?
Post by: SpriggsySpriggs 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
Title: Re: Is there an easy way to retrieve a volume label in QB64?
Post by: SpriggsySpriggs on December 11, 2020, 04:23:22 pm
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.
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.
Title: Re: Is there an easy way to retrieve a volume label in QB64?
Post by: euklides on December 12, 2020, 09:11:03 am