Sign in to follow this  
Followers 0
Santana

File size question

8 posts in this topic

#1 ·  Posted (edited)

Hi all,

I am working on a script and am stuck (again) in calculating the size of all files in a folder.

Basically, what I want to do is to copy any file that is less than 5k in size to another folder

I am able to loop through the files but it return zero for the file size. Here is the code I have so far. Any help will be appreciated.

$path = @ScriptDir & '\result\'
$search = FileFindFirstFile($path & '*.*')
while 1
$file = FileFindNextFile($search)
if @error then ExitLoop
$size = FileGetSize($file)
ConsoleWrite('File ' & $file & 'is '  & $size & @cr)
WEnd
Edited by Santana

Just another special date with a different challenge

Share this post


Link to post
Share on other sites



If $size > 5 Then

Share this post


Link to post
Share on other sites

The return value is the file name only, without the path. You'll need to concatenate the path also, or change the current working directory. If the current directory is the directory you're searching then it should work without any problem.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@ Authenticity

you're right, I tested only in the current dir

$path = @ScriptDir & "\result\"
$search = FileFindFirstFile($path & "*.*")
$size = 0

while 1
    $file = FileFindNextFile($search)
    if @error then ExitLoop
    $size = $size + FileGetSize($path & $file)
WEnd

MsgBox(4096, "Total size = ", $size)
FileClose($search)

Note that FileFindFirstFile does not recurse folders

Edited by Inverted

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks Inverted. It works great. Now, I thought the easiest part was to write

$kilo = $size/1024 to get the size in k but the size doesnt match the file size when I right click and hit property

any hint??

thanks again

Edited by Santana

Just another special date with a different challenge

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

The bytes are correct. (Unless you have other folder inside \result)

Or is the discrepancy after converting to kbs ?

Also, I hope you're not confusing "size" and "size on disk". Size on disk is bigger because of the way the filesystem works (depends on the allocation unit size)

EDIT: I forgot you want to check each file, I thought we were adding up the sizes, lol. Anyway, you can go on your own, it's not complex to compare each file's size to 5*1024

Edited by Inverted

Share this post


Link to post
Share on other sites

[RESOLVED] Thanks so much Inverted. got it working now.


Just another special date with a different challenge

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