Sign in to follow this  
Followers 0
CygnusX1

Stumped on FileGetSize

14 posts in this topic

Hi, 

I'm totally stumped on how to get a file size. I have read the help on FileGetSize(), the example doesn't work. I read I needed to use _WinAPI because FileGetSize() doesn't work on a search handle. I tried using the WinAPI but I can't seem to get that to work either. I have looked all over help and done Google searches. I'm sure it is something easy but I'm overlooking it.

The file search works, at least I'm getting the file names and VarGetType reveals it is a string. _WinAPI_GetFileSize show it is an Int64. I'm close....

Local $search = FileFindFirstFile(file_path & "*.*")

    While 1
        Local $file = FileFindNextFile($search)

        If @error Then ExitLoop
        $new_file = _WinAPI_CreateFile($file, 2)
        $size = _WinAPI_GetFileSizeEx($new_file)
        ConsoleWrite("File: " & $file & " Size: " & $size & @LF)
    WEnd

    ; Close the search handle
    _WinAPI_CloseHandle($file)

Thank you.

Brad


Cygnus

Share this post


Link to post
Share on other sites



This example works. I only added a check to drop directories.

#include <MsgBoxConstants.au3>

Local $hSearch = FileFindFirstFile("*.*")

; Check if the search was successful, if not display a message and return False.
If $hSearch = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
    Exit
EndIf

Local $sFileName = "", $iResult = 0

While 1
    $sFileName = FileFindNextFile($hSearch)
    ; If there is no more file matching the search.
    If @error Then ExitLoop

    ; if it is a file then get the filesize
    If @extended <> 1 Then
        $iResult = MsgBox(BitOR($MB_SYSTEMMODAL, $MB_OKCANCEL), "", "File: " & $sFileName & @CRLF & "FileSize: " & FileGetSize($sFileName))
        If $iResult <> $IDOK Then ExitLoop ; If the user clicks on the cancel/close button.
    Endif
WEnd

; Close the search handle.
FileClose($hSearch)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hey, 

Thanks for the quick reply. I changed the code just a bit to make it more simple to display my issue. If I run the code on my C:WindowsSystem32 look at work I get.

Local $hSearch = FileFindFirstFile("C:\Windows\System32\*.*")
    
; Check if the search was successful, if not display a message and return False.
If $hSearch = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
    Exit
EndIf

Local $sFileName = "", $iResult = 0
    
While 1
    $sFileName = FileFindNextFile($hSearch)
    ; If there is no more file matching the search.
    If @error Then ExitLoop
    ConsoleWrite("File: " & $sFileName & ", " & "Size: " & FileGetSize($sFileName) & @LF)
WEnd

; Close the search handle.
FileClose($hSearch)
File: 0409, Size: 0
File: 12520437.cpx, Size: 0
File: 12520850.cpx, Size: 0
File: aaclient.dll, Size: 0
File: accessibilitycpl.dll, Size: 0
File: ACCTRES.dll, Size: 0
File: acledit.dll, Size: 0
File: aclui.dll, Size: 0
File: acppage.dll, Size: 0
File: ActionCenter.dll, Size: 0
File: ActionCenterCPL.dll, Size: 0
File: activeds.dll, Size: 0
File: activeds.tlb, Size: 0
File: actxprxy.dll, Size: 0
...
...
...
...
etc....
 
What is going on?
 
I have the latest Version of AutoIt.

Cygnus

Share this post


Link to post
Share on other sites

CygnusX1,

 

I have read the help on FileGetSize(), the example doesn't work

So when you run the example in the Help file inside SciTE it does not produce a MsgBox showing a value of about 900 Kb? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I stand corrected the example did work.

What is my code doing that is different?

Thank you.

Brad


Cygnus

Share this post


Link to post
Share on other sites

You need the file's PATH and File name, not just the file's name.

#include<msgboxconstants.au3>
Global $sFilePath = "C:\Windows\System32\"
Local $hSearch = FileFindFirstFile($sFilePath & "*.*") ; Check if the search was successful, if not display a message and return False.
If $hSearch = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
    Exit
EndIf
Local $sFileName = "", $iResult = 0
While 1
    $sFileName = FileFindNextFile($hSearch) ; If there is no more file matching the search.
    If @error Then ExitLoop
    ConsoleWrite("File: " & $sFilePath & $sFileName & ", " & "Size: " & FileGetSize($sFilePath & $sFileName) & @LF)
WEnd ; Close the search handle.
FileClose($hSearch)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

My Fellow NH-ite!

Thank you!

That did it. 

It might be good if the help was updated to include some verbiage around this.

I guess this post can be marked as answered.

Thanks BrewManNH, "Live Free or Die"


Cygnus

Share this post


Link to post
Share on other sites

My Fellow NH-ite!

Thank you!

 

That did it. 

 

It might be good if the help was updated to include some verbiage around this.

I hadn't noticed that the help file doesn't specify that the path would be needed as well, but it wouldn't hurt to add it.

 

Thanks BrewManNH, "Live Free or Die"

:thumbsup:

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

CygnusX1,

 

It might be good if the help was updated to include some verbiage around this

In the Help file under <Using AutoIt - File Name and Path> you will find:

 

Whenever a function parameter is specified as "filename", it implies a filename with a full path

The syntax for FileGetSize says:

 

FileGetSize ( "filename" )

I think that is clear enough. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

RTFM on my part  :oops:


Cygnus

Share this post


Link to post
Share on other sites

CygnusX1,

 

In the Help file under <Using AutoIt - File Name and Path> you will find:

 

Whenever a function parameter is specified as "filename", it implies a filename with a full path

The syntax for FileGetSize says:

 

FileGetSize ( "filename" )

I think that is clear enough. ;)

M23

 

I would disagree that it's all that clear, in that I think it shouldn't just be mentioned in some obscure part of the help file. that information should be linked to the word "filename" in every function that uses it in the help file. We have links to practically everything else in there now, this should definitely be there as well.

I've used AutoIt for over 4 years, and I've never read that section of the help file, it might be a new addition which is why I haven't read it before. Maybe I'll take a look at that this week and see what's practical.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

BrewManNH,

 

that information should be linked to the word "filename" in every function that uses it in the help file

Over to you then! :D

Seriously, we added that page some time ago and made sure that function syntax used the correct terminology - before that there was a real mixture of terms: "file", "filename", "filepath", "file name and path", etc. I do not believe we need to do more - this is not a common problem as most people realise that both a name and a path are needed to identify a specific file. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

It can't hurt to make it clearer. Like I said, we have links to things in the help file now that were only recently linked in the remarks sections of functions, so linking to a page that tells you what "filename" means in the help file would be helpful.

EDIT: I will be taking a look at the help file txt files to see where "filename" is used and figuring out the link needed.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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
Sign in to follow this  
Followers 0