自动 - 获取路径中所有文件的文件大小

问题描述 投票:-1回答:1

我使用脚本获取特定的所有文件和目录,并生成结果的.txt文件,但我需要使用FileGetSize + ByteSuffix函数添加每个文件的大小

    #include <File.au3>
    #include <WinAPIFiles.au3>

    Global Enum Step *2 $GETFILES_NOT_DIRECTORY, $GETFILES_NOT_EXISTS ; GetFiles @error


    Func G3tAllF1lesAndDirs($cpath, $txtname)
        Local $sFileList = ''
        GetFiles($sFileList, $cpath) ; This uses no global variable

            $sFileList = StringReplace($sFileList, '|', @CRLF) ; Replace the pipe char for @CRLF
        Local $handlepath = FileOpen(@DesktopDir & "\" & $txtname & ".txt",1)

    ; Write array to a file by passing the file name.
    FileWrite($handlepath, $sFileList & @CRLF)

    EndFunc   ;==>Example

    ; By guinness on 2015/03/15. Idea by Belini and every AutoIt user who has done file searching.
    Func GetFiles(ByRef $sFileList, $sFilePath)
        Local Static $iCounter = 0

        $sFilePath = _WinAPI_PathAddBackslash($sFilePath) ; Add backslash
       If _WinAPI_PathIsDirectory($sFilePath) <> $FILE_ATTRIBUTE_DIRECTORY Then
        Return SetError($GETFILES_NOT_DIRECTORY, 0, '')
    EndIf

        Local $hFileFind = FileFindFirstFile($sFilePath & '*')
        If $hFileFind = -1 Then ; File not found
            Return SetError($GETFILES_NOT_EXISTS, 0, '')
        EndIf

        Local $sFileName = ''
        While True
            $sFileName = FileFindNextFile($hFileFind)
            If @error Then
                ExitLoop
            EndIf

            If @extended Then ; Is directory.
                $iCounter += 1 ; Used for recursion level
                GetFiles($sFileList, $sFilePath & $sFileName)
                $iCounter -= 1 ; Used for recursion level
            Else
                $sFileList &= $sFilePath & $sFileName & '|'
            EndIf
        WEnd
        FileClose($hFileFind)

        If $iCounter = 0 Then ; First recursion level, therefore strip pipe char
            $sFileList = StringTrimRight($sFileList, StringLen('|'))
        EndIf
    EndFunc   ;==>GetFiles


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) & $aArray[$Index]
EndFunc


  G3tAllF1lesAndDirs(@ScriptDir, "files&folders")

这是我想要的.txt文件,只需修改脚本并使用FileGetSize + ByteSuffix函数

C:\Users\G-PC\Documents\setup.exe [size of this]
C:\Users\G-PC\Documents\config.ini [size of this]
C:\Users\G-PC\Documents\image001.jpg [size of this]
C:\Users\G-PC\Documents\image002.jpg [size of this]
C:\Users\G-PC\Documents\image003.jpg [size of this]
C:\Users\G-PC\Documents\Videos\vid001.avi  [size of this]
C:\Users\G-PC\Documents\Videos\vid002.avi [size of this]
C:\Users\G-PC\Documents\Videos\vid003.avi [size of this]
C:\Users\G-PC\Documents\Videos\vid004.avi [size of this]
C:\Users\G-PC\Documents\Videos\Comedy\vid001.avi [size of this]
C:\Users\G-PC\Documents\Videos\Comedy\vid002.avi [size of this]
C:\Users\G-PC\Documents\Videos\Comedy\vid003.avi [size of this]

列表很长,我尝试使用另一个脚本在生成后覆盖.txt文件,但它不起作用/崩溃

autoit filesize file-writing
1个回答
1
投票

这种方法怎么样?

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

Local $aArray = _FileListToArrayRec(@ScriptDir, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_FASTSORT, $FLTAR_FULLPATH)
;~ _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 & '\size.txt', $size_A, 1, Default, ' ')
ShellExecute(@ScriptDir & '\size.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

这是另一个易于使用的解决方案,让您有机会附加到现有文件并多次调用该函数。

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

; Usage  _outputFilesWithSize($STARTPATH, PATHOFOUTPUTFILE, MODE)
If _outputFilesWithSize(@ScriptDir, @ScriptDir & '\size.txt') <> 1 Then MsgBox(16, 'Error', 'Error') ;
If _outputFilesWithSize('c:\Users\xf01145\Documents\Books\', @ScriptDir & '\size.txt', 1) <> 1 Then MsgBox(16, 'Error', 'Error') ; 3rd Parameter 1 appends info to file
If _outputFilesWithSize('c:\Temp\', @ScriptDir & '\AnotherFile.txt') <> 1 Then MsgBox(16, 'Error', 'Error')

;~ ShellExecute(@ScriptDir & '\size.txt')

;===============================================================================
;
; Function Name:  _outputFilesWithSize
; Description:    Writes the full path and size of all files recursivly to a file
; Parameter(s):   1. startPath = starting directory
;                 2. outputFile = path to the outputfile
;                 3. Mode = Default = overwrite, 1 = append infos to file (e.g. multiple function calls)
; Requirement(s):
; Return Value(s): Error <> 1 = error writing the file
; Author(s):      Xenobiologist
; Modified:
; Remarks:
;===============================================================================
;
Func _outputFilesWithSize($startPath, $outputFile = @ScriptDir & '\size.txt', $mode = Default)

    Local $aArray = _FileListToArrayRec($startPath, "*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_FASTSORT, $FLTAR_FULLPATH)
    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
    If $mode = 1 Then $outputFile = FileOpen($outputFile, $FO_APPEND)
    If _FileWriteFromArray($outputFile, $size_A, 1, Default, ' ') = 0 Then  Return SetError(@error, 0, -1)
    Return 1
EndFunc   ;==>_outputFilesWithSize

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
© www.soinside.com 2019 - 2024. All rights reserved.