Jump to content

Recommended Posts

Greetings!

I am in need of listing several files in a folder with its respective sizes. I found this script online, which works fine. However I'd like to insert the folder path as a variable in a dialog box. How can I replace @ScriptDir with my variable? Thanks in advance.

$z = MsgBox(0,"Selected Folder",FileSelectFolder ( "Select Folder", "C:\")) ; THE VARIABLE I'D LIKE TO INCLUDE.

#include <File.au3>
#include <Array.au3>

Local $aArray = _FileListToArrayRec(@ScriptDir, "*", $FLTAR_SORT, $FLTAR_FILES) ;
;~ _ArrayDisplay($aArray, "Sorted tree")

Local $size_A[UBound($aArray)][2]

For $i = 1 To UBound($aArray) -1
        $size_A[$i][0] = $aArray[$i]
        $size_A[$i][1] = ByteSuffix(FileGetSize($aArray[$i]))
Next
;~ _ArrayDisplay($size_A)

_FileWriteFromArray(@ScriptDir & '\Tamanho.txt', $size_A, 1, Default, ' ')
ShellExecute(@ScriptDir & '\Tamanho.txt')

Func ByteSuffix($Bytes)
    Local $Index = 0, $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
    While $Bytes > 1023
        $Index += 1
        $Bytes /= 1024
    WEnd
    Return Round($Bytes, 2) & $aArray[$Index]
EndFunc   ;==>ByteSuffix

 

Link to comment
Share on other sites

Here a way :

#include <File.au3>
#include <Array.au3>

Local $sFolder = FileSelectFolder("Select a Folder", "C:\")
If @error Then Exit ; button cancel was selected

Local $aArray = _FileListToArrayRec($sFolder, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

_ArrayColInsert ($aArray, 1)

For $i = 1 To UBound($aArray) -1
  $aArray[$i][1] = ByteSuffix(FileGetSize($aArray[$i][0]))
Next

_FileWriteFromArray(@ScriptDir & '\Tamanho.txt', $aArray, 1, Default, ' ')
ShellExecute(@ScriptDir & '\Tamanho.txt')

Func ByteSuffix($Bytes)
    Local $Index = 0, $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
    While $Bytes > 1023
        $Index += 1
        $Bytes /= 1024
    WEnd
    Return Round($Bytes, 2) & $aArray[$Index]
EndFunc   ;==>ByteSuffix

Corrected small bugs along the way 

Edited by Nine
Link to comment
Share on other sites

20 minutes ago, Nine said:

Here a way :

#include <File.au3>
#include <Array.au3>

Local $sFolder = FileSelectFolder("Select a Folder", "C:\")
If @error Then Exit ; button cancel was selected

Local $aArray = _FileListToArrayRec($sFolder, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

_ArrayColInsert ($aArray, 1)

For $i = 1 To UBound($aArray) -1
  $aArray[$i][1] = ByteSuffix(FileGetSize($aArray[$i][0]))
Next

_FileWriteFromArray(@ScriptDir & '\Tamanho.txt', $aArray, 1, Default, ' ')
ShellExecute(@ScriptDir & '\Tamanho.txt')

Func ByteSuffix($Bytes)
    Local $Index = 0, $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
    While $Bytes > 1023
        $Index += 1
        $Bytes /= 1024
    WEnd
    Return Round($Bytes, 2) & $aArray[$Index]
EndFunc   ;==>ByteSuffix

Corrected small bugs along the way 

That was great. Thanks.

The only adjustment I'd make is that I don't need the full path on the TXT file, only the file name. However if I remove $FLTAR_FULLPATH the script returns all the files with 0kb.

Link to comment
Share on other sites

Yes you need to keep the full path otherwise it won't properly work.  But you can remove the full path for your .txt file by splitting the filenames and keep only the part you want (see _PathSplit in help file) 

Link to comment
Share on other sites

27 minutes ago, Nine said:

Yes you need to keep the full path otherwise it won't properly work.  But you can remove the full path for your .txt file by splitting the filenames and keep only the part you want (see _PathSplit in help file) 

I appreciate the help. 

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

×
×
  • Create New...