Jump to content

File Get Size


Recommended Posts

Hi All,

I have many files inside folder with ID Number. The problem here is I want to get ID Number,  file name inside folder and Get Size of its. However when I use loop "For" the result appeared with duplicate data. Please check the example in attached file and help me make the code below better for this case.

#include <File.au3>
#include <Array.au3>
Global $a, $b
$a = _FileListToArrayEx(@ScriptDir, '*.visf')
For $i = 1 to $a[0]
   $b = StringSplit($a[$i], "\")
;~    _ArrayDisplay($b)
   For $j = 1 to $b[0]
;~    ConsoleWrite($b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])   & @CRLF)
      FileWrite(@ScriptDir & '\result.txt', $b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])  & @CRLF)
   Next
Next



Func _FileListToArrayEx($s_path, $s_mask = "*.*", $i_flag = 0, $s_exclude = -1, $f_recurse = True, $f_full_path = True)

    If FileExists($s_path) = 0 Then Return SetError(1, 1, 0)

    ; Strip trailing backslash, and add one after to make sure there's only one
    $s_path = StringRegExpReplace($s_path, "[\\/]+\z", "") & "\"

    ; Set all defaults
    If $s_mask = -1 Or $s_mask = Default Then $s_mask = "*.*"
    If $i_flag = -1 Or $i_flag = Default Then $i_flag = 0
    If $s_exclude = -1 Or $s_exclude = Default Then $s_exclude = ""

    ; Look for bad chars
    If StringRegExp($s_mask, "[/:><\|]") Or StringRegExp($s_exclude, "[/:><\|]") Then
        Return SetError(2, 2, 0)
    EndIf

    ; Strip leading spaces between semi colon delimiter
    $s_mask = StringRegExpReplace($s_mask, "\s*;\s*", ";")
    If $s_exclude Then $s_exclude = StringRegExpReplace($s_exclude, "\s*;\s*", ";")

    ; Confirm mask has something in it
    If StringStripWS($s_mask, 8) = "" Then Return SetError(2, 2, 0)
    If $i_flag < 0 Or $i_flag > 2 Then Return SetError(3, 3, 0)

    ; Validate and create path + mask params
    Local $a_split = StringSplit($s_mask, ";"), $s_hold_split = ""
    For $i = 1 To $a_split[0]
        If StringStripWS($a_split[$i], 8) = "" Then ContinueLoop
        If StringRegExp($a_split[$i], "^\..*?\..*?\z") Then
            $a_split[$i] &= "*" & $a_split[$i]
        EndIf
        $s_hold_split &= '"' & $s_path & $a_split[$i] & '" '
    Next
    $s_hold_split = StringTrimRight($s_hold_split, 1)
    If $s_hold_split = "" Then $s_hold_split = '"' & $s_path & '*.*"'

    Local $i_pid, $s_stdout, $s_hold_out, $s_dir_file_only = "", $s_recurse = "/s "
    If $i_flag = 1 Then $s_dir_file_only = ":-d"
    If $i_flag = 2 Then $s_dir_file_only = ":D"
    If Not $f_recurse Then $s_recurse = ""

    $i_pid = Run(@ComSpec & " /c dir /b " & $s_recurse & "/a" & $s_dir_file_only & " " & $s_hold_split, "", @SW_HIDE, 4 + 2)

    While 1
        $s_stdout = StdoutRead($i_pid)
        If @error Then ExitLoop
        $s_hold_out &= $s_stdout
    WEnd

    $s_hold_out = StringRegExpReplace($s_hold_out, "\v+\z", "")
    If Not $s_hold_out Then Return SetError(4, 4, 0)

    ; Parse data and find matches based on flags
    Local $a_fsplit = StringSplit(StringStripCR($s_hold_out), @LF), $s_hold_ret
    $s_hold_out = ""

    If $s_exclude Then $s_exclude = StringReplace(StringReplace($s_exclude, "*", ".*?"), ";", "|")

    For $i = 1 To $a_fsplit[0]
        If $s_exclude And StringRegExp(StringRegExpReplace( _
            $a_fsplit[$i], "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\Q" & $s_exclude & "\E") Then ContinueLoop
        If StringRegExp($a_fsplit[$i], "^\w:[\\/]+") = 0 Then $a_fsplit[$i] = $s_path & $a_fsplit[$i]
        If $f_full_path Then
            $s_hold_ret &= $a_fsplit[$i] & Chr(1)
        Else
            $s_hold_ret &= StringRegExpReplace($a_fsplit[$i], "((?:.*?[\\/]+)*)(.*?\z)", "$2") & Chr(1)
        EndIf
    Next

    $s_hold_ret = StringTrimRight($s_hold_ret, 1)
    If $s_hold_ret = "" Then Return SetError(5, 5, 0)

    Return StringSplit($s_hold_ret, Chr(1))
EndFunc

 

Example.zip

Edited by tezhihi
Link to comment
Share on other sites

The 2nd For/Next loop is useless 

#include <File.au3>
#include <Array.au3>
Global $a, $b
$a = _FileListToArrayEx(@ScriptDir, '*.visf')
;  _ArrayDisplay($a)
For $i = 1 to $a[0]
      $b = StringSplit($a[$i], "\")
;~    ConsoleWrite($b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])   & @CRLF)
      FileWrite(@ScriptDir & '\result.txt', $b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])  & @CRLF)
Next

 

 

Link to comment
Share on other sites

4 hours ago, mikell said:

The 2nd For/Next loop is useless 

#include <File.au3>
#include <Array.au3>
Global $a, $b
$a = _FileListToArrayEx(@ScriptDir, '*.visf')
;  _ArrayDisplay($a)
For $i = 1 to $a[0]
      $b = StringSplit($a[$i], "\")
;~    ConsoleWrite($b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])   & @CRLF)
      FileWrite(@ScriptDir & '\result.txt', $b[$b[0] - 1] & '_' & $b[$b[0]] & '    ' & FileGetSize($a[$i])  & @CRLF)
Next

 

 

Thank you so muchhhhhhhhhh @mikell

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...