-1

Instead of the MsgBox i want the script to read a .txt file and write to the end of each line the result of a variable, in my case is $FileSize

Here is the code for autoit script

#include <MsgBoxConstants.au3>
#include <File.au3>

Test()

Func Test()

    For $i = 1 to _FileCountLines(@TempDir & "\myfiles.txt")
        $mread = FileReadLine(@TempDir & "\myfiles.txt", $i)

    Local $FileSize = FileGetSize($mread)

    MsgBox($MB_SYSTEMMODAL, "", ByteSuffix($FileSize)) ;this is just a test
    Next
EndFunc   ;==>Example

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   

Here is the content of .txt file

C:\Users\G-PC\Documents\setup.exe
C:\Users\G-PC\Documents\config.ini
C:\Users\G-PC\Documents\image001.jpg
C:\Users\G-PC\Documents\image002.jpg
C:\Users\G-PC\Documents\image003.jpg

I want the following result

C:\Users\G-PC\Documents\setup.exe [SIZE FOR THIS]
C:\Users\G-PC\Documents\config.ini [SIZE FOR THIS]
C:\Users\G-PC\Documents\image001.jpg [SIZE FOR THIS]
C:\Users\G-PC\Documents\image002.jpg [SIZE FOR THIS]
C:\Users\G-PC\Documents\image003.jpg [SIZE FOR THIS]
  • 1
    I gave you a solution here : https://stackoverflow.com/questions/48057487/autoit-get-the-file-size-of-all-files-in-path – Xenobiologist Jan 02 '18 at 13:58

3 Answers3

2

There is a size limit for both string variables and arrays, so you have to process one line at a time when working with big files. This uses a temporary file for output instead of keeping all the information in memory. You can rename (overwriting the original file) the temporary file to the original file name at the end.

Func Test()
    Local $InFile = FileOpen(@TempDir & "\myfiles.txt", $FO_READ)
    Local $OutFile = FileOpen(@TempDir & "\myfiles.tmp", $FO_OVERWRITE)
    While True
        $File = FileReadLine($InFile)
        If @error = -1 Then Return ; no more lines to process
        FileWrite($OutFile, $File & " [" & ByteSuffix(FileGetSize($File)) & "]" & @CRLF)
    WEnd
EndFunc   ;==>Test
user4157124
  • 2,809
  • 13
  • 27
  • 42
Stephan
  • 53,940
  • 10
  • 58
  • 91
1
$sResult = Test()
ConsoleWrite('SIZE LIST' & @CRLF & $sResult)

Func Test()
    Local $FileSize, $sReturn = ''

    For $i = 1 to _FileCountLines(@TempDir & "\myfiles.txt")
        $mread = FileReadLine(@TempDir & "\myfiles.txt", $i)

        $FileSize = FileGetSize($mread)

        ; MsgBox($MB_SYSTEMMODAL, "", ByteSuffix($FileSize)) ;this is just a test
        $sReturn &= $mread & " [" & ByteSuffix($FileSize) & "]" & @CRLF
    Next
    Return $sReturn
EndFunc   ;==>Test

But a better way instead of reading the text file line by line is the using of _FileReadToArray. Than you can iterate through the array with text lines.

1

Using line number parameter for FileReadLine in a loop resets the file pointer to the start and scans up to the line number which slows down the loop as line count increases. Omit that parameter.

Use a file handle so you do not keep on opening and closing the file with each line read in the loop.

#include <MsgBoxConstants.au3>
#include <File.au3>

$sResult = Test()
MsgBox(0, @ScriptName, $sResult)


Func Test()
    Local $iFileSize, $hFile, $sFilePath, $sResult

    ; Open a file handle to read.
    $hFile = FileOpen(@TempDir & "\myfiles.txt")
    If $hFile = -1 Then
        MsgBox(0x30, @ScriptName, 'Unable to open file to read.')
        Exit 1
    EndIf

    While 1
        ; Read and let AutoIt handle line count.
        $sFilePath = FileReadLine($hFile)
        If @error Then ExitLoop

        $iFileSize = FileGetSize($sFilePath)
        If @error Then ContinueLoop

        $sResult &= StringFormat('%s %s\r\n', $sFilePath, ByteSuffix($iFileSize))
    WEnd

    FileClose($hFile)

    Return $sResult
EndFunc

Func ByteSuffix($Bytes)
    Local $Index = 0
    Local $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']

    While $Bytes > 1023
        $Index += 1
        $Bytes /= 1024
    WEnd

    Return Round($Bytes) & $aArray[$Index]
EndFunc
michael_heath
  • 5,262
  • 2
  • 12
  • 22