Microsoft Small Basic

Program Listing: PMQ660-1
' File Challenge Test
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Version 0.3a
' Last update 2015-05-25
' Program ID PMQ660-1
'
title = "File Challenge Test 0.3a"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
Not = "True=False;False=True;"
indent = " "
TextWindow.Title = title
TextWindow.WriteLine(title + CRLF)

TextWindow.WriteLine("1. File_Exists (exists)")
TextWindow.WriteLine(indent + "Expected: True")
' The following line could be harmful and has been automatically commented.
' temp = File.GetTemporaryFilePath()
TextWindow.WriteLine(indent + temp)
param = ""
param["path"] = temp
File_Exists()
TextWindow.WriteLine(indent + "Result: " + fileExists)
pass = fileExists
Test_Pass()

TextWindow.WriteLine("2. File_Exists (not exists)")
TextWindow.WriteLine(indent + "Expected: False")
' The following line could be harmful and has been automatically commented.
' result = File.DeleteFile(temp)
op = "DeleteFile"
File_CheckResult()
File_Exists()
TextWindow.WriteLine(indent + "Result: " + fileExists)
pass = Not[fileExists]
Test_Pass()

TextWindow.WriteLine("3. Directory_Exists (exists)")
TextWindow.WriteLine(indent + "Expected: True")
Path_GetDirectoryName()
GetStamp()
temp = directoryName + "\dir" + stamp
' The following line could be harmful and has been automatically commented.
' result = File.CreateDirectory(temp)
op = "CreateDirectory"
File_CheckResult()
param["path"] = temp
TextWindow.WriteLine(indent + temp)
Directory_Exists()
TextWindow.WriteLine(indent + "Result: " + directoryExists)
pass = directoryExists
Test_Pass()

TextWindow.WriteLine("4. Directory_Exists (not exists)")
TextWindow.WriteLine(indent + "Expected: False")
' The following line could be harmful and has been automatically commented.
' result = File.DeleteDirectory(temp)
op = "DeleteDirectory"
File_CheckResult()
Directory_Exists()
TextWindow.WriteLine(indent + "Result: " + directoryExists)
pass = Not[directoryExists]
Test_Pass()

Sub GetStamp
' return stamp
stamp = Clock.Year + "-" + Clock.Month + "-" + Clock.Day
stamp = stamp + "-" + Clock.Hour + "-" + Clock.Minute + "-" + Clock.Second
EndSub

Sub Directory_Exists
' param["path"] - the full path of the directory that needs to be checked
' return directoryExists - "True" if the directory exists
directoryExists = "False"
Path_GetDirectoryName()
' The following line could be harmful and has been automatically commented.
' dirs = File.GetDirectories(directoryName)
op = "GetDirectories"
File_CheckResult()
nDirs = Array.GetItemCount(dirs)
For i = 1 To nDirs
If Text.ConvertToLowerCase(dirs[i]) = Text.ConvertToLowerCase(param["path"]) Then
directoryExists = "True"
i = nDirs ' break
EndIf
EndFor
EndSub

Sub File_CheckResult
' param result, dirs or files - result of a File operation
' param op - name of File operation
If op = "CreateDirectory" And result = "FAILED" Then
FileHelper_ShowError()
ElseIf op = "DeleteDirectory" And result = "FAILED" Then
FileHelper_ShowError()
ElseIf op = "GetDirectories" And dirs = "FAILED" Then
FileHelper_ShowError()
ElseIf op = "DeleteFile" And result = "FAILED" Then
FileHelper_ShowError()
ElseIf op = "GetFiles" And files = "FAILED" Then
FileHelper_ShowError()
EndIf
EndSub

Sub File_Exists
' param["path"] - the full path of the file that needs to be checked
' return fileExists - "True" if the file exists
fileExists = "False"
Path_GetDirectoryName()
' The following line could be harmful and has been automatically commented.
' files = File.GetFiles(directoryName)
op = "GetFiles"
File_CheckResult()
nFiles = Array.GetItemCount(files)
For i = 1 To nFiles
If Text.ConvertToLowerCase(files[i]) = Text.ConvertToLowerCase(param["path"]) Then
fileExists = "True"
i = nFiles ' break
EndIf
EndFor
EndSub

Sub FileHelper_ShowError
' param op - oparation name
' param indent - indent space if needed
TextWindow.ForegroundColor = "Yellow"
TextWindow.WriteLine(indent + op + ":FAILED")
' The following line could be harmful and has been automatically commented.
' TextWindow.WriteLine(indent + "LastError:" + File.LastError)
TextWindow.ForegroundColor = "Gray"
EndSub

Sub Path_GetFileName
' param["path"] - the full path to be gotton file name
' return fileName - the file name
fileName = ""
For p = Text.GetLength(param["path"]) To 1 Step -1
c = Text.GetSubText(param["path"], p, 1)
If c = "\" Or c = "/" Then
fileName = Text.GetSubTextToEnd(param["path"], p + 1)
p = 1 ' break
EndIf
EndFor
EndSub

Sub Path_GetDirectoryName
' param["path"] - the full path to be gotton directory name
' return directoryName - the directory name
directoryName = ""
For p = Text.GetLength(param["path"]) To 1 Step -1
c = Text.GetSubText(param["path"], p, 1)
If c = "\" Or c = "/" Then
directoryName = Text.GetSubText(param["path"], 1, p - 1)
p = 1 ' break
EndIf
EndFor
EndSub

Sub Test_Pass
' param pass - if True show pass else show fail
' param indent - indent space if needed
TextWindow.Write(indent)
If pass Then
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("PASS" + CRLF)
Else
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("FAIL" + CRLF)
EndIf
TextWindow.ForegroundColor = "Gray"
EndSub