Liberty BASIC Community Forum
Liberty BASIC Programming Discussions >> Novice >> Save to SD Card?
http://libertybasic.conforums.com/index.cgi?board=novice&action=display&num=1504638343

Save to SD Card?
Post by cocos on Sep 5th, 2017, 2:05pm

Hello,

I am quite a novice in programming but I wonder if I can use liberty basic for my project:

The program shall (besides some other simpler stuff) write data to an SD Card.
It shall be written to the SD Card after pressing a button (don't want to use a file-explorer dialog to keep it simple for the user).
There shall be an error message in case no SD Card has been inserted.
I have checked the LB-documentation but I am still unsure wether this can be done with liberty basic. If yes, I would appreciate some sample code.

Thanks!
Re: Save to SD Card?
Post by Stefan Pendl on Sep 5th, 2017, 4:05pm

An SD-Card is just another drive similar to a regular hard-disk, so there is no special code needed.
Re: Save to SD Card?
Post by Rod on Sep 6th, 2017, 03:31am

Here are two of the example files combined to do the job of finding your SD card. You would have a named file on the card. In this case "test.txt". The code finds all the drives that exist on the PC then search each in turn for the named file.


Code:
    'a simple example illustrating the use of the Drives$ variable
    'nomainwin

    dim letters$(25)
    index = 0
    while word$(Drives$, index + 1) <> ""
        letters$(index) = word$(Drives$, index + 1)
        index = index + 1
    wend
    numberOfDrives=index-1




    'fileExists.bas - Show how to determine if a file exists
    dim info$(10,10)
    for i = 0 to numberOfDrives
        fpath$=letters$(i)+"\test.txt"
        if fileExists(fpath$) then
            print fpath$; " exists!"
        else
            print fpath$; " doesn't exist!"
        end if
    next
    end

'return a true if the file in fullPath$ exists, else return false
function fileExists(fullPath$)
    files pathOnly$(fullPath$), filenameOnly$(fullPath$), info$()
    fileExists = val(info$(0, 0)) > 0
end function

'return just the directory path from a full file path
function pathOnly$(fullPath$)
    pathOnly$ = fullPath$
    while right$(pathOnly$, 1) <> "\" and pathOnly$ <> ""
        pathOnly$ = left$(pathOnly$, len(pathOnly$)-1)
    wend
end function

'return just the filename from a full file path
function filenameOnly$(fullPath$)
    pathLength = len(pathOnly$(fullPath$))
    filenameOnly$ = right$(fullPath$, len(fullPath$)-pathLength)
end function