To use this file copy and paste this:
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-4.flb" into BV Terminal 3
or here to download.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// ========== P A R T 4 ======================================
// This section is for write operations. It is much harder to write
// to the card than to read from it as things such as creating
// directory space, updating the fat, checking free space etc. need
// to be considered
//
// RTC is needed for creating and modifying files, for devices without
// an RTC then the words will need to be changed
// This shares many words with fat-1-a, fat-2 and fat-3 so it should
// have the same SID number
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// REQUIRES:
// #URL-Inc "http://pin1.org/forthlib/flb/General/soft1.flb" sid=99
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=100
// #URL-lib "http://pin1.org/forthlib/flb/General/SPI.flb" sid=101
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/MMC.flb" sid=102
// #URL-lib "http://pin1.org/forthlib/flb/General/rtc.flb" sid=103
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-1.flb"
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-2.flb"
// #URL-lib "http://pin1.org/forthlib/flb/SD-Card/fat-3.flb"
Full Contents of File
: fatClust!
    fatClust       
    word16!         
    d-sec  Dsec!       
;   
 
: fatNF
    1+
    TotalSec  d  @  SectClust  d  @ 
    swap
    do
        i  fatClust@  0=
        if  i  leave  then
    loop
    dup  TotalSec  d  @  >  if  drop  -1  then       
;         
: nfde 
        0         
        RootEntries  d  @   
        for
                i  dir-entry     
                c@       
                dup  &e5  =     
                swap  0=         
                or  if  drop  i  -1  leave  then
        next
;
: storeDT
    dup  rtc.time  m-time! 
    rtc.date  m-date!
;
: storeDTC
    dup  rtc.time  c-time! 
    rtc.date  c-date!
; 
 
: dir!   
    dirAdd                               
    en#  f  @                             
    dir-entry                         
    32  move                             
    d-sec  Dsec!                     
;
   
: erase-clusters 
        begin   
            dup     
            fatClust@   
            swap  0  swap  fatClust!     
            dup  &fff8  >     
            over  0=  or   
                if  0  swap  fatClust!  -1  else  0  then 
        until
;
: clearFile
        IsOpen  f  @  -1  >  if  121  error  then     
        dirAdd  start-clust@  dup 
        erase-clusters   
        &ffff  swap  fatClust!   
       
        0  dirAdd  fileSize! 
        dirAdd  storeDT             
        Dir!                   
;
: w-reset 
    r-open    error#  0=
    if
        clearFile     
    else
        ."  File  error  "  error#  .
    then
;
: deleteFile
    dir-search     
    if
        >r  r@  dir-entry                     
        start-clust@  dup       
        erase-clusters           
        0  swap  fatClust!   
        &e5  r>  dir-entry  c!         
        d-sec  Dsec!                     
        0  error
    else
        ."  file  not  found"  103  error
    then                           
;   
: last-clust
    begin
          dup  fatClust@  dup  &fff7  >  if  drop  -1  else  swap  drop  0  then
    until
;                   
: createFile
    dup  dir-search  if  ."  File  exists  use  reset"  130  error  then
    dot2pad       
    dir$  11  move   
    nfde               
    if
       
        0  fatNF             
        &ffff  over  fatClust!       
       
        swap  dir-entry    >r 
        r@  start-clust!       
        dir$  r@  11  move 
        r@  storeDTC       
        r@  storeDT       
        0  r>  fileSize! 
        d-sec  Dsec!                     
    else
        ."  card  full"
        131  error   
    then
;
: newFS
    1+       
    dirAdd  filesize@       
    swap  BytesSec  d  @    *
    max         
;         
: w-random
    swap  =>  f#   
    IsOpen  f  @  -1  >  if  122  error  then     
    dup  Ls2Phys         
    dup  0  <
    if
       
        drop 
        dup
        SectClust  d  @ 
        dirAdd  start-clust@  last-clust  swap 
        for
            dup  fatNF
            dup  rot  fatClust!     
        next
        &ffff  swap  fatClust!       
       
        dup  newFS  dirAdd  filesize! 
        Ls2Phys
        sec!       
    else
       
        sec!   
        newFS  dirAdd  filesize! 
    then
;
: f-close           
    dup  0  devices  between
    if
        =>  f#                 
        IsOpen  f  @  -1  >  if  122  error  then     
        dirAdd  storeDT         
        dir!                       
        0  isOpen  f  !           
    else
        ."  handle  wrong  value"  145  error   
    then       
; 
     
: <0>f.create   
    over  createFile
    f.open 
;
: <0>f.reset  w-reset  ; 
: <0>f.del  deleteFile  ; 
: <0>f.close  f-close  ; 
: <0>f.wrandom  w-random  ;