Write My Own Software - Files and Disks

 


1. How do I tell whether a file exists or not?

When writing software one of the basic tasks is checking whether files exist and how big they are. In Ubercode these tasks are achieved with the Filesize function. This returns -1 if the file is not there, or if the file exists it returns a value >= 0 which is the size of the file in bytes. You do not need to trap any exception codes to use Filesize. For example:

  // Fsize.cls
  Ubercode 1 Class Fsize

  public function main()
  var
    filename : string[*]
    size : integer(-1:MAXINT)
  code
    filename <- Inputbox("Filesize", "Enter file name")
    size <- Filesize(filename)
    select size
      case -1 => call Msgbox("Filesize", filename + ": not there")
      case  0 => call Msgbox("Filesize", filename + ": is empty")
      else    => call Msgbox("Filesize", filename + ": " + Str(size) + " byte(s)")
    end select
  end function

  end class

2. How do I delete a file?

Use the Filedel function. For example:

  // Fdel.cls
  Ubercode 1 Class Fdel

  public function main()
  var
    filename:string[*] // single file name
  code
    filename <- Openfiledialog("Delete file", 0, Dirpath() + "*.*")
    if filename /= "" and
       Msgbox("Delete?", filename, "Yes" + NL + "No") = "Yes" then
      call Filedel(filename)
    end if
  end function

  end class

If you call Filedel when the file does not exist, no error occurs and nothing happens. The purpose of Filedel is to remove the file, and this purpose is met if the file never existed in the first place. Use Filesize if you want to know whether a file exists or not.

3. How do I delete a folder and all its files?

You can delete a folder using Dirdel and delete multiple files using Filedel. Thus to delete a folder and all its files, first use Filedel then Dirdel. For the next example, create a folder off the root of the C: drive called UnwantedFolder and copy some files to it. For example:

  // DeleteAllDemo.cls
  Ubercode 1 Class DeleteAllDemo

  public function main()
  code
    // Delete c:\UnwantedFolder and everything in it.
    if Dirsize("c:\UnwantedFolder\") >= 0 then
      call Filedel("c:\UnwantedFolder\*.*")
      call Dirdel("c:\UnwantedFolder\")
    end if

    // Check it has gone.
    call Msgbox("DeleteAll", "Size of c:\UnwantedFolder = " + Str(Dirsize("c:\UnwantedFolder\")))
  end function

  end class

Note that Filedel does not delete read-only files and does not delete folders, thus the technique above will not remove the folder if it contains sub-folders or read-only files. This is for reasons of safety.

4. How do I copy a file?

Use the Filecopy function which copies a source file to a destination file. The source file should exist and the destsination file should not exist (if it does, it will be overwritten). The following example shows how to use Filecopy:

  // Fcopy.cls
  Ubercode 1 Class Fcopy

  public function main()
  var
    source : string[*]  // source file
    dest   : string[*]  // destination file
  code
    source <- Openfiledialog("File to copy", 0, "*.*")
    if source /= "" then
      dest <- Saveasdialog("Destination file", 0, "*.*")
      if dest /= "" and source /= dest then
        call Filecopy(source, dest)
        call Msgbox("Filecopy", Str(Filesize(dest)) + " byte(s)")
      end if
    end if
  end function

  end class

The program works by prompting for source which is the file being copied, then prompting for dest which is the name to be given to the copy. If the filenames were entered correctly and the source file exists, then Filecopy is used to copy the file. After copying the file, the Msgbox shows the number of bytes that were copied.

Filecopy also works with files having a size of zero bytes. If a file of size zero is copied, the resulting destination file will also have a size of zero bytes.

5. How do I copy multiple files?

Filecopy also works with wildcards, for example C:\my folder\*.* can be used as the source and a folder name can be used as the destination. This example prompts for some files and copies them:

  // FilecopyWildcardDemo.cls
  Ubercode 1 Class FilecopyWildcardDemo

  public function main()
  var
    pattern:string[*] 
    dest:string[*]
    numFiles:integer(0:MAXINT)
  code
    pattern <- Inputbox("Files to copy", "", "c:\my folder\*.*")
    if pattern /= "" then
      dest <- Inputbox("Destination folder", "", "c:\other folder\")
      if dest /= "" then
        call Filecopy(pattern, dest, FILE_CONFIRM_REPLACE, numFiles) 
        call Msgbox("Filecopy", Str(numFiles) + " file(s) copied.")
      end if
    end if
  end function

  end class

The program works by prompting for a source pattern then a destination folder. If the filenames were entered correctly, the files are copied and the Msgbox shows the number of files copied. You can have Filecopy not prompt when replacing existing files by using zero instead of FILE_CONFIRM_REPLACE.

6. How do I edit the lines in a text file?

If you only want to edit part of the file, perhaps because the file has a special structure, read in the part of the file you want and display it in a multi line Edit object, or using an Editwindow object. After making changes merge the part back into the file.

If you want to edit all the file, use Loadtext to read it into an EditWindow, edit it, then call Savetext to write the changed text back out to disk. The Sdi example program shows how this is done.

7. How do I loop through a file containing binary data?

The data needs to have a fixed record size, and you need to know the layout of the data. You may be able to work this out by examining the data file with a debugger. Declare a record that maps onto the file records, and at run time use use Sizeof to check the record size matches the file size. Then declare an external list variable with the record as a component, then loop through the file from record 1 to the last record. Use Listread / Listwrite to read and write the records.