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 post
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 post
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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VollachR
      Hi,
      I'm using _FileListToArray to create an array listing .ini files in a specific folder, problem is, I want only the file named Settings.ini and any Files named Data#.ini where # is actually a number, I don't want any other .ini file that may be there even though it shouldn't be there to be included in the array.
      Is it possible to use multiple filters in _FileListToArray? or is there another way to do what I'm looking for?
      My current code:
      $IniList = _FileListToArray(@ScriptDir & "\Settings", "*.ini", 1, False) _ArraySort($IniList, 1, 1) _ArraySort($IniList, 0, 2) $IniOptions = _ArrayToString($IniList, "|", 1)  
      Thank you.
    • By 9252Survive
      Hi All, 
       
      I am fairly new to AutoIT and I am still trying to learn, I have been using _FileListToArray to list all the files with a particular extension in an array and then loop through it for operation  (   For $i = 1 To UBound($FileArray) - 1).
      So far this has been working fine. But I am not able to figure out a problem that I have; what if I have 50 files but I only want to loop through first 10 files and then next ten and so on?  Or rather I should say, how I can I only feed max 10 files to the array at a time when I do _FileListToArray regardless of the total number of files in the folder?
      Any insight/help will be much appreciated 
    • By rootx
      $text = 'C:\xxx\hh\hhhh\hhh\image.JPG' $out = StringRegExp($text,'.*\\(.*)\\',1)Return the last folder hhh.
      I would use this expression in the cycle below.
       
      Local $aArray = _FileListToArrayRec("C:\test\app", "*app.exe", $FLTAR_FILES, 1, $FLTAR_SORT,$FLTAR_FULLPATH) $fileArray = UBound($aArray) -1 For $y = 1 To $fileArray _GUICtrlListView_AddItem($ListView, StringRegExp($aArray[$y],'.*\\(.*)\\',1), 0) Next My target is to remove everything from the path, and take only the last folder.
      Thanks
×
×
  • Create New...