Partilhar via


Small Basic - File Tips

Today I will introduce some tips about File object in Small Basic.

File.LastError

Have you ever used File.LastError?  Following program is a sample using File.LastError.  File.LastError property contains file error message from Windows system.  Following is a sample code.

GetPath``(``)

While `` path ``<`` > ``""

  ``TextWindow``.``WriteLine``(`` "[" `` + `` path `` + ``"]"``)

  `` files `` = ``File``.``GetFiles``(``path``)

  `` If `` files `` = `` "FAILED" ``Then

    ``TextWindow``.``WriteLine``(``"File.GetFiles: FAILED"``)

    ``TextWindow``.``WriteLine``(`` "File.LastError: " `` + ``File``.``LastError``)

    ``TextWindow``.``WriteLine``(``""``)

  ``Else

    `` n `` = ``Array``.``GetItemCount``(``files``)

    `` For `` i `` = `` 1 `` To ``n

      ``TextWindow``.``WriteLine``(``files``[``i``]``)

    ``EndFor

    ``TextWindow``.``WriteLine``(``""``)

  ``EndIf

  ``GetPath``(``)

EndWhile

Sub ``GetPath

  ``' return path

  ``TextWindow``.``Write``(``"Path? "``)

  `` path `` = ``TextWindow``.``Read``(``)

EndSub

Following list is a sample output of this program.  File.LastError message is written in local language of your system.

 Path? notexist
[notexist]
File.GetFiles: FAILED
File.LastError: Directory Path does not exist.

Path?

Dot in Path

Path names given in File operations should be full path names.  But "." and ".." are allowed as full path names.  "." means a current directory.  ".." means a parent directory.

 Path? ..\..
[..\..]
..\..\bootmgr
..\..\BOOTNXT
..\..\pagefile.sys
..\..\swapfile.sys

Path?

Slash in Path

A path name may contain back slash "\".  But slash "/" also can be used instead of back slash.

 Path? /Program Files (x86)/Microsoft/Small Basic/lib
[/Program Files (x86)/Microsoft/Small Basic/lib]
/Program Files (x86)/Microsoft/Small Basic/lib\LitDev.De.xml
/Program Files (x86)/Microsoft/Small Basic/lib\LitDev.dll
/Program Files (x86)/Microsoft/Small Basic/lib\LitDev.xml
/Program Files (x86)/Microsoft/Small Basic/lib\Microsoft.Kinect.dll
/Program Files (x86)/Microsoft/Small Basic/lib\Microsoft.Kinect.Face.dll
/Program Files (x86)/Microsoft/Small Basic/lib\Microsoft.Kinect.SmallBasic.dll
/Program Files (x86)/Microsoft/Small Basic/lib\Microsoft.Kinect.SmallBasic.XML
/Program Files (x86)/Microsoft/Small Basic/lib\Microsoft.Kinect.Wpf.Controls.dll

Path?

Comments

  • Anonymous
    May 27, 2015
    Awesome! This is great while people learn some best practices for how to handle files in Small Basic! Thanks, Nonki!

  • Anonymous
    May 28, 2015
    Great article Nonki, thanks.

  • Anonymous
    May 29, 2015
    Nice tip Nonki. Promote validation. If something independent of the program can fail then it's nice deal with it and let the user know what's happening.