问题
Im using a script to get all files and dirs in a specific and generates a .txt file of the result but i need to add the size of each file using the FileGetSize + ByteSuffix functions
#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")
Here is what i want in the .txt file by just modifying the script and using FileGetSize + ByteSuffix functions
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]
The list is very long, i tried using using another script to overwrite the .txt file after it generates but it doesnt work/crash
回答1:
What about this approach?
#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
Here is another solution which should be easy to use and gives you the opportunity to append to existing files and to call the function multiple times.
#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
来源:https://stackoverflow.com/questions/48057487/autoit-get-the-file-size-of-all-files-in-path