This article explains about files and directories in Microsoft Small Basic language.

What is a File?

A file is a data package stored in a disk of a computer.  Small Basic basically has operations to use following three types of files.

  • Audio files
  • Image files
  • Text files

A file has it's name.  For example, readme.txt is a filename.  A filename has a basename and a filename extension.  In the case of readme.txt, readme is it's basname and .txt is it's filename extension.  Filename extensions represents the file types.

Audio Files

Following audio files are supported by Small Basic.  Other file formats may or may not play depending on the audio codecs installed on the user's computer.

  • .mp3 - MPEG 3
  • .wav - Wave
  • .wma -Windows Media Audio

Image Files

An image file is such as  a .bmp (bitmap) file.  More details are described here.

Text Files

.txt is a filename extension for simple text file. But there are many text file formats such as follows.

  • .csv - Comma Separated Value
  • .html - Hyper Text Markup Language
  • .ppm - Portable Pixmap
  • .rtf - Rich Text Format
  • .sgf - Smart Game Format
  • .svg - Scalable Vector Graphics
  • .xml - Extensible Markup Language

Small Basic can read and write these file formats if that are coded as UTF-8.

What is a Directory?

A directory is also called as folder.  A directory is a place to contain files and sub directories.

What is a Path?

A path is a name of a directory or a file.  Following names are samples of full paths.  A full path is a name started from a drive name and a root directory.  The first one is a directory path (for documents folder).  The second one is file path (for Small Basic).

C:\Users\Nonki\Documents
C:\Program Files (x86)\Microsoft\Small Basic\SB.exe

A path is constructed with directory names and "\" (backslash) as the separators like:

<drive>:\<root>\<child>\<grandchild>\<basename>.<extension>

In Small Basic, all path names to be given to following operations as their parameters are full path names.  Because the current directory for a Small Basic program executed from the Small Basic environment (IDE) is %windir%\System32 .  But the current directory for a Small Basic program executed from the command prompt is the current directory.  So, in the latter case, relative path names are allowed.

What is a URL?

URL (uniform resource locator) is a path for an internet file.  For example, this article has following URL.

http://social.technet.microsoft.com/wiki/contents/articles/25576.small-basic-file.aspx

A URL is separated with "/" (slash), and started with network protocol name.  Note that a URL is case sensitive.

Operations for Files

Following three operations for any types of files.  First one copies a file.  Second one deletes a file.  Third one gets file name from a given directory path.

' for local files
err = File.CopyFile(path1, path2)
err = File.DeleteFile(path)
files = File.GetFiles(path)

Operations and a Property for Directories 

In following list, the first three operations are for creating, deleting directory and getting names of directories.  And the last one is a property to get directory path where the program is placed.

' for local directories
err = File.CreateDirectory(path)
err = File.DeleteDirectory(path)
dirs = File.GetDirectories(path)
path = Program.Directory

Operations for Text Files

There are seven operations of File object in Small Basic for text files.  First two are to get file paths.  The next one is for reading from a file.  The last four are to write to files.  These all operations treat UTF-8 encoded text files.

' for local text files
' to get file path
path = File.GetSettingsFilePath()
path = File.GetTemporaryFilePath()
' to read file
txt = File.ReadContents(path)
' to write file
err = File.AppendContents(path, txt)
err = File.InsertLine(path, lno, txt)
err = File.WriteContents(path, txt)
err = File.WriteLine(path, lno, txt)

Operations for Internet Files

Network object in Small Basic has two operations.  One is to download any type of file from internet.  The other is to read (get) contents of a web page.  These two operations can read from local files also. 

' for local / internet files
path = Network.DownloadFile(url)
' for local / internet text files
txt = Network.GetWebPageContents(url)

Operations for Audio Files

These four operations are to play, pause, and stop audio files such as .mp3, .wav, or .wma files.  Details are described here.

' for local / internet audio files
Sound.Play(url)
Sound.PlayAndWait(url)
Sound.Stop(url)
Sound.Pause(url)

Operations for Image Files

These five operations are for getting images from internet or local disk, or drawing them.

' for Internet / Local Image files
url = Flickr.GetPictureOfMoment()
url = Flickr.GetRandomPicture(tag)
img = ImageList.LoadImage(url)
GraphicsWindow.DrawImage(url, x, y)
obj = Shapes.AddImage(url)

Sample Programs

Run these programs in local - [Import] in IDE, remove single quotation marks in lines with File object and [Run].

  • Convert Relative Path to Absolute Path (JQK779) - This program converts relative local file path to absolute local file path.
  • Wildcard (SVV671) - This program matches file names with wild card "*" or "?" in Program.Directory and makes a list as an array.  "*" matches any characters including null and "?" matches any character.
  • Blog Article List (CPP576) - This program makes a list of articles in Small Basic official blog.

Following list shows links to sample programs using File object.

Known Issues

In following issues, remote means that the Small Basic programs run in smallbasic.com program listing server page on a browser with Silverlight add-in.

  • ImageList.GetHeightOfImage() and ImageList.GetWidthOfImage() return zeros in remote.
  • Network.GetWebPageContents() doesn't work in remote.
  • Program.Directory contains local directory path in local but program URL in remote.

See Also

Other Resources

Other Languages