|
Post by stan58 on Oct 23, 2018 6:19:07 GMT -5
I want to develop a prog that would allow a user to write a recipe file consisting of several lines detailing product name and target weight values. To then transfer it to a USB memory stick that can be plugged into our weighing indicator product in order to control the formulation of a recipe. Is there any way of determining the path for the usb device and then do you just treat it like any other file?.
|
|
|
Post by Brandon Parker on Oct 23, 2018 9:39:03 GMT -5
You can definitely treat the file path on a USB device (presumably a thumb-drive or hard-drive....) just like any other file path you might encounter.
Are you using a specific folder structure and file naming convention with your program?
You can use the code below to get started..... Basically check to see if a specific folder and/ or file exists on each Drive that is installed in the system ....
The below is just a primer for you to see if you can work it out; let us know if you get stuck or otherwise require additional assistance.
'These are required for this code, but you can substitute the
'constant values for the global variables or use your own.
Global False : False = 0
Global True : True = 1
'Dimention the Double-Dimention Info$() Array for use
'with the Files command
Dim Info$(0, 0)
'Create our own variable for storing the information
'about which Drives are installed on the computer
'as to not mess around with the LB Drives$ variable
myInstalledDrives$ = Upper$(Drives$)
'See which Drives are installed
Print Drives$
'While Loop for checking each Drive installed for the folder you are looking for
While (Word$(myInstalledDrives$, (i + 1)) <> "")
Scan 'Included here just in case one creates an Infinite Loop
'Search each Drive for the folder where your file is expected to be
'Your folder on the thumbdrive should be as unique as possible to ensure you do not get false positives.
'Rename the folder you are looking for below; replace "Basic Languages" with your own
If (searchForFolder(Word$(myInstalledDrives$, (i + 1)), "Basic Languages") > False) Then
'You could do this first and just do this
'Search for a specific file; replace the strings below with your specific ones or with variables as necessary
If fileExists(Word$(myInstalledDrives$, (i + 1));"\Basic Languages", "myFileName.xxx")
'Do something here like open the file or whatever....
End If
End If
'Increment i to allow for moving along each of the installed Drives
i = (i + 1)
Wend
End
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Function fileExists(path$, filename$)
Files path$, filename$, Info$()
fileExists = Val(Info$(0, 0))
ReDim Info$(0, 0)
End Function
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Function FileName$(name$)
Do Until Instr(FileName$, "\", 1) > 0
FileName$ = Mid$(name$, (Len(name$) - i))
i = (i + 1)
Loop
FileName$ = Mid$(FileName$, 2)
End Function
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Function FilePath$(name$)
FilePath$ = Left$(name$, ((Len(name$) - Len(FileName$(name$))) - 1))
End Function
'_________________________________________________________________________________________________________________________________________________________
'_________________________________________________________________________________________________________________________________________________________
Function searchForFolder(directory$, folder$)
On Error GoTo [Error]
directory$ = directory$ + "\"
Files directory$, "*", Info$()
If Val(Info$(0,1)) > 0 Then
For i = 1 To Val(Info$(0,1))
'Print Lower$(Info$(Val(Info$(0,0)) + i, 1));" : ";Lower$(folder$)
If Lower$(Info$(Val(Info$(0,0)) + i, 1)) = Lower$(folder$) Then
searchForFolder = True
Exit For
End If
Next i
End If
ReDim Info$(0, 0)
[Error]
End Function
{:0)
Brandon R. Parker
|
|
|
Post by stan58 on Oct 23, 2018 14:29:19 GMT -5
Brendon Thank you for your help,I will let you know how it goes.
|
|
|
Post by stan58 on Oct 23, 2018 15:18:56 GMT -5
Brendon device$ = mid$ (Drives$,3,3) print device$ end
Well I am a novice! just needed to know where the thumb drive is, your info sorted me.I can now make my folder in the USB device and then create files.Is my code ok for clearing out the c: so that device$ holds my directory path
|
|
|
Post by Rod on Oct 24, 2018 1:51:14 GMT -5
The one problem you might have is that Drive$() finds the available drives when the program starts. It will not report a USB drive inserted after the program has started. So you might need to cater for closing and restarting your program if the user has to be prompted to insert the USB drive mid program.
|
|
|
Post by stan58 on Oct 24, 2018 4:59:04 GMT -5
thank you Rod. That will save me scratching my head later.
|
|
|
Post by Brandon Parker on Oct 24, 2018 7:34:25 GMT -5
Another thing you want to remember is that your USB device may not always be at that Drive Letter (ex. D: ). Windows can/ will shuffle things about and/ or give a device a different Drive letter due to a myriad of circumstances therefore you will need to search through the Drive Letters each time you start/ restart the program.
{:0)
Brandon R. Parker
|
|
|
Post by stan58 on Oct 24, 2018 7:41:42 GMT -5
path$= "d:" on error goto [nodrive] open path$+"testfile.txt" for output as #tst print #tst,"hi" close #tst print "opened"
kill path$+"testfile.txt" end [nodrive] print "no drive found" end
Thought that I
|
|
|
Post by stan58 on Oct 24, 2018 7:46:40 GMT -5
path$= "d:" on error goto [nodrive] open path$+"testfile.txt" for output as #tst print #tst,"hi" close #tst print "opened"
kill path$+"testfile.txt" end [nodrive] print "no drive found" end
Whoops pressed wrong button start again. I thought that I could test the drives in sequence by trying to open a file if I was able to open the file ,then the drive is present.I would then kill the file,but for some reason I can make the file but not kill it ?
|
|
|
Post by Brandon Parker on Oct 24, 2018 7:56:31 GMT -5
Normally you would have a "\" between the Drive Letter and the rest of the path. For some reason the file appears to be created perfectly fine without this in there, but the Kill command is definitely looking for it.
So...your path would be like this..
path$+"\testfile.txt"
Personally I would distinguish between the path and the filename, but everyone does things just a little different than the next person.
{:0)
Brandon R. Parker
|
|
|
Post by stan58 on Oct 24, 2018 8:24:49 GMT -5
Once again thank you. Is the principal of testing for drives in this way ok?
|
|
|
Post by Brandon Parker on Oct 24, 2018 8:35:08 GMT -5
I would use the code that I posted above; checking for either the folder or the file (or both.... ). I typically try not to just create files and delete them where it is unnecessary. {:0) Brandon R. Parker
|
|
|
Post by stan58 on Oct 24, 2018 8:55:53 GMT -5
Thanks for your advice , also the backslash sorted my kill command.
|
|