Jump to content

get file size using handle


orbs
 Share

Recommended Posts

hello everyone,

so i need to get file size using only its handle as given by FileOpen(). i don't have the file name as string, so no go on FileGetSize().

trying _WinAPI_GetFileSizeEx(), i get an invalid handle error. _WinAPI_GetLastErrorMessage() says "The handle is invalid." the handle value as returned by FileOpen() is 1.

reproducer:

#include <WinAPIFiles.au3>
#include <WinAPIError.au3>

Global $hFile = FileOpen(@ScriptFullPath)
Global $iSize = _WinAPI_GetFileSizeEx($hFile)
ConsoleWrite(_WinAPI_GetLastErrorMessage() & @CRLF)
ConsoleWrite($hFile & ' : ' & $iSize & @CRLF)
FileClose($hFile)

any ideas?

 

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Developers

The returned $hFile from FileOpen() is not the real filehandle but rather a sequence number, so won't work with the _WinAPI call.
...  but when you do the FIleOpen() is guess you do know the actual filename, so am puzzled about your comment not knowing the filename?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

thank you @pixelsearch, your method works.

@Jos, the function i'm writing is inside a UDF, working with FileOpen() that is called by the main script. i cannot tell in advance what usage the main script is going to do with my UDF and i don't want to force the main script to pass the file name as string to my UDF.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...