none
Small Basic File objects RRS feed

  • Question

  • I am using Small Basic on Windows 10 64 bit and attempt to use File objects. All File objects appear to be ignored with no SUCCESS or FAILURE response. The only one that works is File.CreateDirectory but get no SUCCESS response. Any ideas that might help?

    Thanks to previous responses on code to try but it doesn't work on my system. 

    Monday, August 28, 2017 2:21 PM

Answers

  • The File Object works, but i bet that you're trying to write to an invalid filepath or to a path where you don't have the required permissions to Write/Read/Change/etc.

    A filepath under %TEMP% should grant the required rights, like eg.

    path = File.GetTemporaryFilePath()  ' or

    path = "%TEMP%\MyFile.txt"

    path = File.GetTemporaryFilePath()      '  will work
    'path = "%TEMP%\MyFile.txt"             ' will work
    'path = Program.Directory +"\MyFile.txt"  ' probably will not work
    'path = "Drive:\Your\path\here.txt"       ' probably will not work
    
    TextWindow.WriteLine("PATH: "+ path)
    TextWindow.WriteLine("")
    errWrite = File.WriteContents(path, "This is Demotext in "+ path)
    
    If errWrite <> "SUCCESS" Then
      TextWindow.WriteLine("Writing: "+ errWrite)
      TextWindow.WriteLine("ERROR: "+ File.LastError)
      Program.End()
    Else
      TextWindow.WriteLine("Writing: "+ errWrite)
      TextWindow.WriteLine("Reading: "+ File.ReadContents(path))
    EndIf
    TextWindow.WriteLine("")

    To visit the %temp% folder in explorer, enter %temp% in your address bar.

    And a hint:

    If you run into problems with your code, it always will be better to post some demo code or sample which fails.

    So people can see what you're trying to do and where the error could be.

    Monday, August 28, 2017 10:53 PM
    Answerer

All replies

  • Hi

    Refer This Article ,This May Help You = https://social.technet.microsoft.com/wiki/contents/articles/23607.small-basic-reference-documentation-file-object.aspx

    This Program May Help You

    Contents = "Hello World, This Is Small Basic" 'Stores Text In The Variable "Contents"
    Contentstowrite = File.WriteContents("E:/txt.txt",Contents) 'Writes The Content
    TextWindow.WriteLine(Contentstowrite)
    If Contentstowrite = "SUCCESS" Then
    ContentsToRead = File.ReadContents("E:/txt.txt") 'Reads The Content
    GraphicsWindow.BrushColor = "Red" 'Brush Colour
    txt = Shapes.AddText(ContentsToRead) 'Adds Text
    Shapes.Move(txt,0,0) ' Moves Text
    EndIf
    If You Small Basic Versions Of v1.2 or Below Then

    Try Switching To Small Basic 1.3 (Latest Version Of Small Basic). It Is Avaivable Here = https://www.microsoft.com/en-us/store/p/small-basic/9pfqkjljpb1r

    You Can Download Small Basic 1.3 If You Have Following System Requirements

    Thanks,





    Monday, August 28, 2017 2:44 PM
  • GLHBA,

    Try the below program. Just copy and put in small basic and run. What do you get? If SUCCESS look in the program directory and you should see the file that you created. 

    Data="Hello World"
    Status=File.WriteContents(Program.directory+"Test.txt",Data)
    TexGLHBAtWindow.WriteLine(Program.directory)
    TextWindow.WriteLine(Status)


    JR

    Monday, August 28, 2017 3:01 PM
    Answerer
  • GLHBA,

    Try the below program. Just copy and put in small basic and run. What do you get? If SUCCESS look in the program directory and you should see the file that you created. 

    Data="Hello World"
    Status=File.WriteContents(Program.directory+"Test.txt",Data)
    TexGLHBAtWindow.WriteLine(Program.directory)
    TextWindow.WriteLine(Status)


    JR


    JR, would you like to be an Answerer on the Small Basic forum? Basically, you help us by proposing answers and then you mark them 7 days later. Thanks!

    Ed Price, Azure Development Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    • Marked as answer by GLHBA Tuesday, August 29, 2017 9:39 PM
    • Unmarked as answer by Pappa LapubEditor Tuesday, September 5, 2017 3:48 PM
    Monday, August 28, 2017 8:21 PM
    Owner
  • The File Object works, but i bet that you're trying to write to an invalid filepath or to a path where you don't have the required permissions to Write/Read/Change/etc.

    A filepath under %TEMP% should grant the required rights, like eg.

    path = File.GetTemporaryFilePath()  ' or

    path = "%TEMP%\MyFile.txt"

    path = File.GetTemporaryFilePath()      '  will work
    'path = "%TEMP%\MyFile.txt"             ' will work
    'path = Program.Directory +"\MyFile.txt"  ' probably will not work
    'path = "Drive:\Your\path\here.txt"       ' probably will not work
    
    TextWindow.WriteLine("PATH: "+ path)
    TextWindow.WriteLine("")
    errWrite = File.WriteContents(path, "This is Demotext in "+ path)
    
    If errWrite <> "SUCCESS" Then
      TextWindow.WriteLine("Writing: "+ errWrite)
      TextWindow.WriteLine("ERROR: "+ File.LastError)
      Program.End()
    Else
      TextWindow.WriteLine("Writing: "+ errWrite)
      TextWindow.WriteLine("Reading: "+ File.ReadContents(path))
    EndIf
    TextWindow.WriteLine("")

    To visit the %temp% folder in explorer, enter %temp% in your address bar.

    And a hint:

    If you run into problems with your code, it always will be better to post some demo code or sample which fails.

    So people can see what you're trying to do and where the error could be.

    Monday, August 28, 2017 10:53 PM
    Answerer
  • Thanks for the help. The file objects below work on

    Windows 10 64 bit.

    path = File.GetTemporaryFilePath() 

    'path = "%TEMP%\MyFile.txt" 

    • Marked as answer by GLHBA Monday, September 4, 2017 11:43 AM
    • Unmarked as answer by Pappa LapubEditor Tuesday, September 5, 2017 3:48 PM
    Tuesday, August 29, 2017 10:04 PM