Small Basic #10: File Input and Output

As demonstrated in the following code, you can use the Microsoft Small Basic File class to create file directories, access directories' files, and copy files from one file directory to another. You can also read from files and write to files.

 ' This code assumes that a directory named C:\Temp\ already exists.
TextWindow.WriteLine("Subdirectory created: " + File.CreateDirectory("C:\Temp\TempSubdirectory\"))
subdirectories = File.GetDirectories("C:\Temp")

If Array.GetItemCount(subdirectories) = 1 Then
  TextWindow.WriteLine("C:\Temp has 1 subdirectory.")
Else
  TextWindow.WriteLine("C:\Temp has " + Array.GetItemCount(subdirectories) + " subdirectories.")
EndIf

For i = 1 To Array.GetItemCount(subdirectories)
  TextWindow.WriteLine("subdirectories[" + i + "] = " + subdirectories[i])
  files = File.GetFiles("C:\Temp\" + subdirectories[i])
  For j = 1 To Array.GetItemCount(files)
    TextWindow.WriteLine("    files[" + i + "] = " + files[i])
  EndFor
EndFor 

TextWindow.WriteLine("This program's file path is " + File.GetSettingsFilePath())

filePath = File.GetTemporaryFilePath()
TextWindow.WriteLine("A temporary file has been created at: " + filePath + ".")
TextWindow.WriteLine("Write to file: " + File.WriteContents(filePath, "Hello, World!"))
TextWindow.WriteLine("Write line to file: " + File.WriteLine(filePath, 15, "Hello Again, World!"))
TextWindow.WriteLine("Insert line in file: " + File.InsertLine(filePath, 20, "Hello Once Again, World!"))
TextWindow.WriteLine("Append to file: " + File.AppendContents(filePath, "Hello For the Last Time, World!"))
TextWindow.WriteLine("File contains: " + File.ReadContents(filePath))
TextWindow.WriteLine("Copy file: " + File.CopyFile(filePath, "C:\Temp\TempSubdirectory\"))

If File.LastError = "" Then
Else
  TextWindow.WriteLine("The last error was: " + File.LastError)
EndIf

Comments

  • Anonymous
    July 08, 2014
    Decent code ................. for an amateur Why is the i x derivitive controlling the variable. Everyone knows that an inverse dilema causes a response by the stem TO the leaf. It child's play

  • Anonymous
    July 08, 2014
    m8 dis iz basic ahah are u a special needz bent guy lmaoooooooooooooooooooooooooooooooooooooooooooooooo000000000000000000000000000000000000000000000000000000000000000000

  • Anonymous
    July 08, 2014
    The comment has been removed

  • Anonymous
    July 08, 2014
    trollking & MaStA-hAcKeR667 want a slap m8s im in the army

  • Anonymous
    July 08, 2014
    The comment has been removed

  • Anonymous
    October 16, 2014
    Program that calculates the sum of the two numbers SMALLBASIC please write in the language.The input data file to read and write the results file.

  • Anonymous
    November 10, 2014
    So is this what you do in your spare time?

  • Anonymous
    November 10, 2014
    I fluctuate my servers on the daily

  • Anonymous
    September 22, 2015
    ( ͡° ͜ʖ ͡°)( ͡° ͜ʖ ͡°)( ͡° ͜ʖ ͡°)( ͡° ͜ʖ ͡°)

  • Anonymous
    September 29, 2015
    JOHN CENA. du doo du dooooooooooo00ooo

  • Anonymous
    September 29, 2015
    the f*** you lyinnnnn oh my godddddddddddddddd stop f***ing lyinnnnnnn

  • Anonymous
    September 29, 2015
    The comment has been removed

  • Anonymous
    September 29, 2015
    A N D  H I S  N A M E  I S  J O H N  C E N A. duDUUuduu dooo000oooo

  • Anonymous
    September 29, 2015
    .... That's exactly what the undertaker told J O H N  C E N A yabadooo... du doo du doo00ooo

  • Anonymous
    October 13, 2015
    I well actually rekt  ya mayte, I can not fluctuate my servers :(( I hev to get me mum to halp me to do it, why do I even try at lfe sense im sooch a fayle :???

  • Anonymous
    November 20, 2015
    The comment has been removed

  • Anonymous
    March 06, 2016
    Hello! If anyone has any questions, please ask in the Small Basic forum: social.msdn.microsoft.com/.../threads Thanks!