AutoIt脚本修改需求:为文件列表添加文件大小信息
Got it, let's fix this script to add file sizes directly (instead of post-processing the text file which was causing crashes). Here's the modified version with clear explanations:
Modified AutoIt Script with File Size Integration
#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) FileClose($handlepath) ; Ensure file handle is closed properly (prevents crashes) 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 If @extended Then ; Is directory. $iCounter += 1 ; Used for recursion level GetFiles($sFileList, $sFilePath & $sFileName) $iCounter -= 1 ; Used for recursion level ; Add directory path to the list (no size needed) $sFileList &= $sFilePath & $sFileName & '|' Else ; Fetch and format file size using your existing ByteSuffix function Local $rawSize = FileGetSize($sFilePath & $sFileName) Local $formattedSize = ByteSuffix($rawSize) ; Append formatted file line to the list $sFileList &= $sFilePath & $sFileName & ' [' & $formattedSize & ']|' 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")
Key Changes Made
- Fixed syntax error: Removed an extra
EndIfin theGetFilesloop that was causing unexpected behavior. - Included directories in output: The original script only added files to the list—now directories are included (they’ll appear without size info, as expected).
- Integrated file size logic: For each file, we:
- Use
FileGetSize()to get the raw byte count - Pass that value to your existing
ByteSuffix()function for human-readable formatting - Append the formatted line (
[path] [size]) to the output list
- Use
- Added proper file cleanup: Added
FileClose($handlepath)to ensure the text file is properly closed after writing—this likely contributed to your earlier crash issues when overwriting the file.
Output Example
Your files&folders.txt on the desktop will now look exactly like your requested format:
C:\Users\G-PC\Documents\setup.exe [15 MB] C:\Users\G-PC\Documents\config.ini [2 KB] C:\Users\G-PC\Documents\image001.jpg [345 KB] C:\Users\G-PC\Documents\SubFolder\
内容的提问来源于stack exchange,提问作者user3796104
相关产品推荐
相关产品推荐

