Small Basic Sample: Small DOS
This sample is for File.CopyFile, File.DeleteFile and File.GetFiles.
Code
' Small DOS - Sample Code for File.CopyFile, File.DeleteFile and File.GetFiles
' Version 0.1
' Program ID TJK128
TextWindow.WriteLine("Small DOS Version 0.1")
TextWindow.WriteLine("")
Init()
While "True"
TextWindow.Write(path + ">")
cmdLine = TextWindow.Read()
GetArg()
If cmd = "DIR" Then
files = File.GetFiles(path)
ShowFiles()
ElseIf cmd = "COPY" Then
err = File.CopyFile(path + "\" + file1, path + "\" + file2)
ShowError()
ElseIf cmd = "DEL" Then
err = File.DeleteFile(path + "\" + file1)
ShowError()
ElseIf cmd = "TYPE" Then
ShowFile()
ElseIf cmd = "EXIT" Then
Program.End()
Else
TextWindow.WriteLine("COPY <file1> <file2>")
TextWindow.WriteLine(" Copy <file1> as <file2>.")
TextWindow.WriteLine("DEL <file>")
TextWindow.WriteLine(" Delete <file>.")
TextWindow.WriteLine("DIR")
TextWindow.WriteLine(" Show filenames in the directory.")
TextWindow.WriteLine("EXIT")
TextWindow.WriteLine(" Exit from Small DOS.")
TextWindow.WriteLine("TYPE <file>")
TextWindow.WriteLine(" Show <file>.")
EndIf
TextWindow.WriteLine("")
EndWhile
Sub Init
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
Not = "True=False;False=True;"
path = Program.Directory
space = " "
EndSub
Sub GetArg
' param cmdLine - command line
' return cmd - command
' return file1, file2 - arguments
p = 1
len = Text.GetLength(cmdLine)
SkipSpace()
GetToken()
cmd = Text.ConvertToUpperCase(Token)
SkipSpace()
GetToken()
file1 = Token
SkipSpace()
GetToken()
file2 = Token
EndSub
Sub SkipSpace
' param cmdLine - command line
' param p - current pointer to command line
' param len - command line length
c = Text.GetSubText(cmdLine, p, 1)
While Text.IsSubText(space, c) And p <= len
p = p + 1
c = Text.GetSubText(cmdLine, p, 1)
EndWhile
EndSub
Sub GetToken
' param cmdLine - command line
' param p - current pointer to command line
' param len - command line length
' return token
Token = ""
c = Text.GetSubText(cmdLine, p, 1)
While Not[Text.IsSubText(space, c)] And p <= len
p = p + 1
Token = Text.Append(Token, c)
c = Text.GetSubText(cmdLine, p, 1)
EndWhile
EndSub
Sub ShowError
' param err - "FAILED" if error
If err = "FAILED" Then
TextWindow.ForegroundColor = "Red"
TextWIndow.WriteLine(File.LastError)
TextWindow.ForegroundColor = "Gray"
EndIf
EndSub
Sub ShowFile
' param file1 - to show
buf = File.ReadContents(path + "\" + file1)
p = 1
len = Text.GetLength(buf)
While p <= len
eol = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), CRLF)
If 0 < eol Then
line = Text.GetSubText(buf, p, eol + 1)
p = p + eol + 1
Else
line = Text.GetSubTextToEnd(buf, p)
p = len + 1
EndIf
TextWindow.Write(line)
EndWhile
EndSub
Sub ShowFiles
' param files - files array
If files = "FAILED" Then
ShowError()
Else
n = Array.GetItemCount(files)
len = Text.GetLength(path + "\")
For i = 1 To n
TextWindow.WriteLine(Text.GetSubTextToEnd(files[i], len + 1))
EndFor
EndIf
EndSub