Getting the size of a file in windows

I am trying to get the size of a file in windows, using

dim f as folderitem
f=getfolderitem(“thefileinquestion.txt”)
msgbox "Size is "+str(f.length)

For an empty file I get 2
For a file with one letter in it , I get 10?

Am I using the right command?

I’ve always opened the file as a binary stream then used the length from that.

I think you’re not seeing the actual bytes the file is using, but probably the file system block allocation? Try Wayne’s solution.

FolderItem.Length should return the actual file size. I wonder if perhaps your “empty” file is not empty? For example, on Windows a blank line is encoded as 2 bytes (CR + LF). Perhaps that’s what you are seeing?

What does Windows say the lengths are? And what type of encoding are they? An empty UTF-8 file will probably be 3 bytes while a Unicode file will probably be 2 bytes, plus whatever the text editor program left behind.

The only way that I’ve ever been able to get a 0 byte file on a Windows systems is like this from the command line:

TYPE NUL: > SomeFile.txt

Otherwise, there are always at least 2 characters in the resulting file.

If you created the file with WordPad, as it is not pure ASCII, that would make sense. Although you enter only one character, the RTF header takes room. Did you compare what the properties of the file tell you as opposed to FolderItem.Length ?

A nice application to create really small ASCII files is Notepad.

[quote=73534:@Tim Jones]The only way that I’ve ever been able to get a 0 byte file on a Windows systems is like this from the command line:

TYPE NUL: > SomeFile.txt

Actually, if you create the file by selecting New… from the context menu after Right-Clicking on the desktop, the file will be 0 bytes.

Good point, Dale. I’ve never used that as I tend to live at the command line with unxutils (my real command would have been “touch Somefile.txt”) or in my games (Diablo III Reaper of Souls hits tomorrow!). I never found enough of a use for Explorer to take it farther than “Double-Click to start” or to find a file and Windows 8.1 has even removed that requirement :). I do believe that I’ve used “New Folder” a few times, but hadn’t even noticed the “New File Type” options.

If you like to use OLEObject, you can do following.

  Dim fileSpec As String  = "c:\\temp\\test.txt"
  
  Dim oFSO AS OLEObject
  Dim oGFO As OLEObject 
  Dim fileName As String
  Dim fSize As UInt64
  
  oFSO = New OLEOBJECT("Scripting.FileSystemObject")
  
  If (oFSO.FileExists(filespec)) Then
        fileName = oFSO.GetFileName(filespec)
        oGFO = oFSO.GetFile(filespec)
        fSize = oGFO.size
        Msgbox("FileName " + fileName + "  uses  " + str(fSize) + " bytes")
    
  Else
    msgbox(filespec + " doesn't exist.")
  End If
    
  oFSO = Nil
  
exception err as oleexception
  msgbox err.message

More info here: http://msdn.microsoft.com/en-us/library/2d66skaf(v=vs.84).aspx

[quote=73423:@dave duke]I am trying to get the size of a file in windows, using …
[/quote]

Function getFileSize(f as FolderItem) As string
  Dim filesize As Double
  Dim size, s As String
  If f <> Nil and f.Exists Then
    filesize = f.Length
    If filesize < 1024 Then
      filesize = filesize
      size = "Size: " + str(filesize) + " Byte"
    elseif filesize = 1024 or filesize > 1024 _
      and filesize < 1024 * 1024 Then
      filesize = filesize / 1024
      s = CStr(filesize)
      size = "Size: " + str(s) + " KB"
    elseif filesize = 1024 * 1024 or filesize > 1024 * 1024 Then
      filesize = filesize / 1024 / 1024
      s = CStr(filesize)
      size = "Size: " + str(s) + " MB"
    elseif filesize = 1024 * 1024 * 1024 _
      or filesize > 1024 * 1024 * 1024 Then
      filesize = filesize / 1024 / 1024 / 1024
      s = CStr(filesize)
      size = "Size: " + str(s) + " GB"
    End If
    Return size
  End If
End Function
dim f as folderitem = // your folderitem
msgbox  getFileSize (f)