TheSaint

Getting the file size of a growing file (SOLVED)

14 posts in this topic

#1 ·  Posted (edited)

See Post #13 for the solution.

Not had any luck on this yet, but still trying ... testing any method that suggests itself.

THE SCENARIO

I have a third party program downloading a file, and it doesn't display size as the file grows ... or even show what the final size may be.

So I am attempting to at least report on the growing file size.

However, a well known issue with Windows Explorer, is that it doesn't continually refresh, so something needs to occur to make it do so.

In an open folder window, one can press F5 or send it via code. Or one can select another file. The true file size then shows.

THE QUESTION

Is their some way, programmatically, with the destination folder window closed, where you can force Explorer to refresh itself for the content of that folder and thus get the file size at the time of querying?

Note, I am using the FileGetSize command of course.

P.S. I attempted a Win API variant, but failed ... probably did it wrong. Perhaps VBScript has something, but I'd rather I just used some method via AutoIt ... but any solution will do in a pinch.

MY CODE

If FileExists($vidfle) Then
    ;_FileCreate($tmpfle)
    ;$size = FileGetSize($tmpfle)
    ;
    ;$handle = _WinAPI_CreateFile($vidfle, 2, 2)
    ;$size = _WinAPI_GetFileSizeEx($handle)
    ;_WinAPI_CloseHandle($handle)
    ;
    $size = FileGetSize($vidfle)
    If $size <> 0 Then
        $size = $size / 1048576
        If $size > 999 Then
            $size = Round($size / 1024, 2) & " Gb"
        Else
            $size = Round($size) & " Mb"
        EndIf
        GUICtrlSetData($Label_sze, $size)
    EndIf
EndIf

 

Edited by TheSaint

TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites



23 minutes ago, j0kky said:

Have you tried to FileFlush before getting the size?

FileFlush. Up until now, I never heard of that command. Doesn't sound suitable either, as it requires FileOpen ... plus it doesn't exist in v3.3.0.0. of AutoIt, which I am using ... and I am not about to update a whole script for it in any case, unless it is known definitely to work ... and no other solution exists.

25 minutes ago, Jfish said:

Another thought, could you use a command line function with StdoutRead and bypass any refreshing of explorer?

How? What? Where? No idea springs to mind.

Thanks guys, but they sound like experiments and not something that is known to work, and I was hoping for something from someone who has already solved this very common issue ... it can happen with both file move & copy too.

:)


TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

22 minutes ago, TheSaint said:

How? What? Where? No idea springs to mind.

I tested this which I got from Stackoverflow to get the file size:

forfiles /p C:\Temp /m file1.txt /c "cmd /c echo @fsize"

replace c:\temp with your path and file1,txt with your file and it will echo the filesize.  

Edited by Jfish
1 person likes this

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

@Jfish - Thanks for that, will give it a try. I have used forfiles before, but had forgotten about it. :D

Just for those wanting a little Valik nostalgia trip, of him in full flight ... and for those who never had the pleasure, but heard the rumors. :lol:

Gonna see if I can use the code from there as well.

1 person likes this

TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

A funny one indeed, of classic proportions.

It was certainly never dull for long with him around.


TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

8 hours ago, TheSaint said:

FileFlush. Up until now, I never heard of that command. Doesn't sound suitable either, as it requires FileOpen ... plus it doesn't exist in v3.3.0.0. of AutoIt, which I am using ... and I am not about to update a whole script for it in any case, unless it is known definitely to work ... and no other solution exists.

Yep, Fileflush is supported from the next version of AutoIt, 3.3.2.0.

Anyhow, if you don't try other solutions, this script does exatly what you want:

Local $hHandle = FileOpen("C:\a.txt"), $tTimer = TimerInit()
Do
    ConsoleWrite(FileGetSize("C:\a.txt") & @CRLF)
    Sleep(1000)
    FileFlush($hHandle)
Until TimerDiff($tTimer) > 20000
FileClose($hHandle)

 

Edited by j0kky
1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted

@j0kky - Thanks, will keep it in mind. May even write a little exe to use it with my older script.


TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

51 minutes ago, TheSaint said:

@j0kky - Thanks, will keep it in mind. May even write a little exe to use it with my older script.

On second thoughts, to be compatible with your version, FileFlush can be easily replaced with FlushFileBuffers API:

Local $hHandle = FileOpen("C:\a.txt"), $tTimer = TimerInit()
Do
    ConsoleWrite(FileGetSize("C:\a.txt") & @CRLF)
    Sleep(1000)
    DllCall("Kernel32.dll", "BOOLEAN", "FlushFileBuffers", "HANDLE", $hHandle)
Until TimerDiff($tTimer) > 20000
FileClose($hHandle)

 

Edited by j0kky
1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted

Once again thanks.

Will try that shortly.


TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

@j0kky - once more I thank you.

My first little test run worked beautifully. And on repeated use.

Local $handle

If FileExists($vidfle) Then
    $handle = FileOpen($vidfle, 0)
    ;$size = FileGetSize($vidfle)
    DllCall("Kernel32.dll", "BOOLEAN", "FlushFileBuffers", "HANDLE", $handle)
    $size = FileGetSize($vidfle)
    ;DllCall("Kernel32.dll", "BOOLEAN", "FlushFileBuffers", "HANDLE", $handle)
    ;$size = FileGetSize($vidfle)
    FileClose($handle)
    ;
    If $size <> 0 Then
        $size = $size / 1048576
        If $size > 999 Then
            $size = Round($size / 1024, 2) & " Gb"
        Else
            $size = Round($size) & " Mb"
        EndIf
        GUICtrlSetData($Label_sze, $size)
    EndIf
EndIf

Because of timing constraints, I could not use the Call in a loop or with any significant delay (part of a STD_READ loop writing to my own version of a Console).

I will now fine tune to see what I can get away with.

It's all a bit convoluted, because I code on my WinXP machine, then test and use on my Win 7 Netbook, and the Win XP Laptop just decided to freeze on me during a SciTE save ... it does that once in a while ... getting a bit old in the tooth. So here I am while it takes its sweet time restarting.

EDIT

Was able to do away with second DLL call and first FileGetSize.

Edited by TheSaint

TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
Share on other sites

#14 ·  Posted

JFYI - Here's a screenshot of my Console in action, where the 80 Mb is the growing size, being reported using the code from the previous post.

NOTE - The 541 Mb total, is just an estimation based on averaging the size of the first five fragments of the download, times total number of fragments.

Console_3-0.png


TheSaints' Robust Chat

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox

userbar.png

Share this post


Link to post
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