locked
See if text file is empty? RRS feed

  • Question

  • is there an easy way to check if a .txt file has no contents at all?
    Tuesday, March 17, 2020 4:39 PM

Answers

  • Use:  
    FI=Program.Directory+"\testfile.txt"
    a=File.ReadContents(FI)
    If Text.GetLength(a) Then
      TextWindow.WriteLine("File is empty or doesn't exist")
    Else
      TextWindow.WriteLine("Length of file: "+Text.GetLength(a))
    EndIf
    


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by litdev Tuesday, March 17, 2020 8:54 PM
    • Marked as answer by Nonki Takahashi Sunday, March 29, 2020 3:24 PM
    Tuesday, March 17, 2020 6:52 PM
    Answerer

All replies

  • Use:  
    FI=Program.Directory+"\testfile.txt"
    a=File.ReadContents(FI)
    If Text.GetLength(a) Then
      TextWindow.WriteLine("File is empty or doesn't exist")
    Else
      TextWindow.WriteLine("Length of file: "+Text.GetLength(a))
    EndIf
    


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by litdev Tuesday, March 17, 2020 8:54 PM
    • Marked as answer by Nonki Takahashi Sunday, March 29, 2020 3:24 PM
    Tuesday, March 17, 2020 6:52 PM
    Answerer
  • Hello Wilhem,

    The problem in this example is that an empty file is not always empty ...

    Just create a "new" small-basic file and save it immediately as untitled1.sb ... so I imagine that this file is empty ?

    However the program will give as length of the file  "0" - so you cannot use the program as a true or false condition.

    I thought you presented once a program to edit the hidden ascii characters in a file (some old ms utility)

    If you are using some "enters" to create "empty lines" in the untitled program you will even get a higher number.

    To find the EOF of a file I was comparing some lines ... and if the the length of the last three lines was "0" I thought it was EOF. It failed when the  texteditor  was putting some empty lines in the text. I couldn't find a trick to skip this empty lines where I thought to find CR/LF in stead of the real empty lines when small basic is reading behind  the EOF.  



    • Edited by Eddy Abts Wednesday, March 25, 2020 8:55 PM
    Wednesday, March 25, 2020 8:53 PM
  • Sorry, the If statement is wrong and should be

    If Text.GetLength(a) = 0 Then

    Then the program reports an empty or non existing file.

    To distinguish between empty / non existing use

    FI=Program.Directory+"\empty.sb"
    a=File.ReadContents(FI)
    If Text.GetLength(a)=0 Then
      fils=file.GetFiles(Program.Directory+"\")
      If Array.ContainsValue(fils,FI) Then
        TextWindow.WriteLine("File is empty")
      Else
        TextWindow.WriteLine("File doesn't exist")
      EndIf
    Else
      TextWindow.WriteLine("Length of file: "+Text.GetLength(a))
    EndIf


    Jan [ WhTurner ] The Netherlands




    Thursday, March 26, 2020 10:49 AM
    Answerer
  • Hello Jan,

    Ok the answer on the first question is perfect. But ...

    If you open a new sb screen and hit three times the return key you will get on the screen 1 2 3 and normally your untitled.sb is "empty" (it isn't).  So the program will give for the length "4". Probably CR and-or LF's we cannot test with small basic ???  Otherwise it could be a trick to overwrite "for x=1 to ..." the lines with an empty line or pop off the lines to another file. Your program can halt then the loop when the file becomes empty and x will give the EOF !!! Perhaps there is another solution to overwrite the line with really empty lines ????  There are a lot of topics on EOF ... but the solutions are only working if you know how the file is constructed. In XML files typically <gpx> files I am tracing the </gpx> statement and if the file is corrupted when I have 5 times an "emptyl" line. Otherwise I have to verify with another language and to hardcode the EOF. 


    • Edited by Eddy Abts Thursday, March 26, 2020 6:29 PM
    Thursday, March 26, 2020 6:28 PM
  • I made a program to list the bytes of a file with  LDFile.ReadbyteArray()

    The testfile is

    line 1
    
    
    line 2
    
    line 3
    With this program:
    Hex="123456789ABCDEF"
    
    FI=Program.Directory+"\empty6.sb"
    b=LDFile.ReadbyteArray(FI,"true")
    
    list()
    
    ''============================================
    Sub List
      TextWindow.WriteLine("LIST of "+FI)
      TextWindow.WriteLine("")
      TextWindow.WriteLine("bytepos  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F")
      For _i1=0 To Text.Getlength(b) Step 48
        _i0=100001+_i1/3
        TextWindow.Write(Text.GetSubTextToEnd(_i0,2)+"   ")
        For _i2=_i1+1 To _i1+48 Step 3
          TextWindow.Write(Text.GetSubText(b,_i2,2))
          TextWindow.Write(" ")
        EndFor
        TextWindow.CursorLeft=60
        For _i2=_i1+1 To _i1+48 Step 3
          charN=Text.GetIndexOf(Hex,Text.GetSubText(b,_i2,1))
          charN=Text.GetIndexOf(Hex,Text.GetSubText(b,_i2+1,1))+16*charN
          If charN<32 Then
            charN=46  ''point .  (for non printing chars)
          EndIf
          TextWindow.Write(Text.GetCharacter(charN))
        EndFor
        TextWindow.WriteLine("")
      EndFor
    EndSub
    
    the output will be

    Jan [ WhTurner ] The Netherlands

    Friday, March 27, 2020 10:37 AM
    Answerer