Tech Note 03e: Accessing the File System

March 12, 2003

 

© NSB Corporation. All rights reserved.

For more information on this control, see Microsoft's documentation.


The File System can be controlled through NS Basic/CE using the File Control. To create a File System Object, use the following statement:

addObject "filesystem"[,"file control name as string"]

The second argument is optional. It allows you to specify the name to use for the object. If you do not specify it, use "File". A sample follows showing how the File System Object works.

 

File System Methods 

Type

Method

Description

Folder

Dir

Returns the name of a matching file, directory, or folder.

 

MkDir

Creates a new directory or folder.

 

RmDir

Removes an existing directory or folder.

File

MoveFile

Moves a file.

 

FileCopy

Copies a file.

 

FileDateTime

Returns the date and time stamp of a file.

 

FileLen

Returns the length of a file in bytes.

 

Kill

Deletes files from a disk.

Attributes

GetAttr

Returns the attributes of a file, directory, or folder.

 

SetAttr

Sets attribute information for a file.

 

  

Sample Code 

 

REM This program demonstrates the use of the File and Filesystem Controls

Option Explicit
Dim selection
addObject "filesystem"
addObject "file"

selection=0

while selection<>9
  selection=inputbox("Choose test number:" & chr(13) & "1. Attr" & chr(13) & "2. Dir" & chr(13) & "3. Get/Put" & chr(13) & "4. Line Write" & chr(13) & "5. Write/Read" & chr(13) &  "9. End Program", "File System Demo")
select case selection
  case 1
    cmdAttr_click
  case 2
    cmdDir_click
  case 3
    cmdGetPut_click
  case 4
    cmdLineWrite_click
  case 5
    cmdWrite_click
  case else
    selection=9
  end select
wEnd

Private Sub cmdAttr_Click()
  On Error Resume Next
  Dim sTheFile
   
  sTheFile = inputBox("Enter file name:","ATTR Command")
   
  msgbox "GetAttr:" & fileSystem.GetAttr(sTheFile) & chr(13) & "FileDateTime:" & fileSystem.FileDateTime(sTheFile) & chr(13) & "FileLen:" & fileSystem.fileLen(sTheFile),0,"File Attributes for " & sTheFile
  If Err <> 0 Then
    MsgBox "Error in GetAttr: " & Err.Description
    Err.Clear
  End If
   
End Sub

Private Sub cmdDir_Click()
  Dim sDir, sMsg, input

  input = inputBox("Enter file name (wildcards OK)","DIR Command")
  sDir = filesystem.dir(input)
  sMsg = "Directory contents of " & input & ": " & vbCrLf & sDir
  While (sDir <> "")
    sDir = fileSystem.Dir()
    If sDir <> "" Then
      sMsg = sMsg & ", " & sDir
    End If
  Wend
   
  msgbox  sMsg
End Sub

Private Sub cmdGetPut_Click()
  WriteUsingPut
  ReadUsingGet
End Sub

Private Sub cmdLineWrite_Click()
  Writeline
  Readline
End Sub

Private Sub cmdWrite_Click()
  WriteUsingControl
  ReadUsingControl
End Sub

Sub WriteUsingPut()
  On Error Resume Next
  Dim myArray
  ReDim myArray(6)
  file.Open "ceGetPut.txt", 4, 2, 3, 500 'fsAccessWrite

  myArray(1) = 1
  myArray(2) = "This is a string"
  myArray(3) = True
  myArray(4) = Date
  myArray(5) = 63.12

  file.Put myArray
  If Err <> 0 Then
    MsgBox "Error in WriteUsingPut: " & Err.Description
    err.clear
  End If

  myArray(1) = 2
  myArray(2) = "This is a string2"
  myArray(3) = True
  myArray(4) = Date + 1
  myArray(5) = 63.12
  file.Put myArray

  myArray(1) = 3
  myArray(2) = "This is a string3"
  myArray(3) = True
  myArray(4) = #11/16/34#
  myArray(5) = 63.12
  file.Put myArray

  msgbox "done with Put", 0, "Put"
  file.Close
End Sub

Sub ReadUsingGet()
  Dim myArray,i,msgString
  On Error Resume Next
  file.Open "ceGetPut.txt", 4, 1, 3, 500
  file.Get myArray, 2

  msgString = myArray(1)
  For i = 2 To UBound(myArray)
    msgString = msgString & ", " & myArray(i)
  Next

  msgBox msgString,0,"Contents of ceGetPut.txt"
  file.Close
End Sub

Sub WriteUsingControl()
  Dim myArray
  ReDim myArray(10)

  myArray(1) = Date
  myArray(2) = "This is a string with a "" (quote)."
  myArray(3) = True
  myArray(4) = #11/16/34#
  myArray(5) = 63.12
  myArray(6) = "Text"
  myArray(7) = Null
  myArray(8) = ""

  file.Open "ceWrite.txt", 2 'fsModeOutput
  file.WriteFields myArray
  file.Close
End Sub

Sub ReadUsingControl()
  Dim myArray, msgString, i
  file.Open "ceWrite.txt", 1 'fsModeInput
  myArray = file.InputFields(10)
    
  msgString = myArray(1)
  For i = 2 To UBound(myArray, 1)
    msgString = msgString & ", " & myArray(i)
  Next
    
  msgbox msgString,0,"Contents of ceWrite.txt"
  file.Close
End Sub

Sub Writeline()
  On Error Resume Next
  file.Open "ceLine.txt", 2 'fsModeOutput
  If Err <> 0 Then
    MsgBox "Error in writeline: " & Err.Description
    Err.Clear
  End If
  file.LinePrint "Hier ist unser erste Linie."
  file.LinePrint "Hier ist unser zweite Linie."
  file.Close
End Sub

Sub Readline()
  file.Open "ceLine.txt", 1 'fsModeInput
  dim sOut
  sOut = file.LineInputString()
  sOut =sOut & Chr(13) & file.LineInputString()
  msgBox sOut,0,"Contents of ceLine.txt"

  file.Close
End Sub