Jump to content

FileOpen/Read Problems


Recommended Posts

I'm making a program that will act like a torrent downloader in that it will be able to see how much it has downloaded of a selected file and be able to start where it left off. I have a problem though, i think have reached the limits of autoit's. I and a friend have our networks connected together, however different things on our networks make it unstable to download 2+ g, because it would get an error and you would have to start all over, so i am trying to make something that will just pick up where it left off when it can continue ( ie network returned ). first off here is the client applications needed: Cut.au3

#include <GuiConstants.au3>
#include <String.au3>
Global $Version = "1.0.6"
Global $PieceSize = "100"

GuiCreate("File Cut V: " & $Version, 369, 229,(@DesktopWidth-369)/2, (@DesktopHeight-229)/2)

                GuiCtrlCreateGroup("File and Info", 10, 10, 350, 140)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
$Location =     GuiCtrlCreateInput("", 20, 30, 280, 20)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
$Find =         GuiCtrlCreateButton("Find", 310, 30, 40, 20)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
                GuiCtrlCreateGroup("Info", 20, 50, 330, 90)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
$Size =         GuiCtrlCreateLabel("Size:", 30, 70, 130, 20)
$DateCre =      GuiCtrlCreateLabel("Date Created:", 30, 90, 130, 20)
$DataMod =      GuiCtrlCreateLabel("Date Modified:", 30, 110, 130, 20)
$NumPieces =        GuiCtrlCreateLabel("# of Pieces:", 180, 70, 140, 20)
                GuiCtrlCreateGroup("Gather and Place", 10, 150, 350, 70)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
$Start =        GuiCtrlCreateButton("Start", 20, 170, 60, 20)
                    GUICtrlSetFont(-1,9,400,0,"Times New Roman")
$ProgressAm =   GuiCtrlCreateLabel("# of # pieces", 210, 170, 130, 20)
$Progress =     GuiCtrlCreateProgress(20, 200, 330, 10)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $Find
        $File = FileOpenDialog  ( "Select File", "" , "All (*.*)", 3 )
        If $File <> "" Then
            GuiCtrlSetData( $Location, $File )
;~          file size
            $FileSize = FileGetSize( $File )
            GuiCtrlSetData( $Size, "Size: " & $FileSize & " Bytes" )
;~          pieces
            $Pieces = $FileSize / $PieceSize
            If IsFloat( $Pieces ) Then
                $Pieces = Int( $Pieces ) + 1
            EndIf
            GuiCtrLSetData( $NumPieces, "# of Pieces: " & $Pieces )
;~          created
            $Time = FileGetTime( $File, 1 )
            GuiCtrlSetData( $DateCre, "Date Created: " & $Time[2] & "/" & $Time[1] & "/" & $Time[0] )
;~          Modified
            $Time = FileGetTime( $File )
            GuiCtrlSetData( $DataMod, "Date Modified: " & $Time[2] & "/" & $Time[1] & "/" & $Time[0] )
        EndIf
    Case $msg = $Start 
        If GuiCtrlread( $Location ) <> "" Then
            GuiCtrlSetState( $Location, $GUI_DISABLE )
            GuiCtrlSetState( $Find, $GUI_DISABLE )
            GuiCtrlSetData( $Start, "Pause" )
            $FileName = GuiCtrlRead( $Location )
            $FileName = StringSplit( $FileName, "\", 1 )
;~          see if the files has been started already
            $Names = IniReadSectionNames ( @ScriptDir & "\" & "FileTracker" )
            $RunState = 0
            If NOT @error Then
                For $i = 1 to $Names[0]
                    If $Names[$i] = $FileName[$FileName[0]] Then
                        $RunState = 1
                    EndIf
                Next
            EndIf
            If $Runstate = 0 Then
                IniWrite ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], "NumPieces", $Pieces )
                IniWrite ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], "PieceSize", $PieceSize )
                IniWrite ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], "FileSize", $FileSize )
                $FileHandle = FileOpen( GuiCtrLRead( $Location ), 0 )
                For $i = 1 to $Pieces
                    $Data = FileRead ( $FileHandle, $FileSize )
                    If @error <> 1  Then
                        IniWrite( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], $i,  _StringToHex( $Data ) )
                    Else
                        Msgbox( 0, "ERROR", "There was a problem reading the file, ending download process" )
                        $i = $Pieces + 1
                    EndIf
                    GuiCtrlSetData( $Progress, ($i/$Pieces)*100 )
                    GuiCtrLSetData( $ProgressAm, $i & " of " & $Pieces & " pieces" )
                Next
                FileClose( $FileHandle )
                RunWait( @ScriptDir & "\" & "unpacker.exe " & chr(34) & $FileName[$FileName[0]] & Chr(34))
                msgbox(0,"Complete", "The file was unpacked")
            ElseIf $RunState = 1 Then
                $ReadSize = IniRead ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], "FileSize", "" )
                $ReadPiece = IniRead ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], "PieceSize", "" )
                If $ReadSize = $FileSize AND $ReadPiece = $PieceSize Then
                    $FileHandle = FileOpen( GuiCtrLRead( $Location ), 0 )
                    For $i = 1 to $Pieces
                        $Data = FileRead ( $FileHandle, $FileSize )
                        If NOT @error <> 1 Then
                            If ($Read = IniRead( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], $i, "Err" )) = "Err" Then
                                IniWrite( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], $i, _StringToHex( $Data ) )
                            Else
                                $FirstR = StringLeft( $Read, 1 )
                                $LastR = StringRight( $Read, 1 )
                                $FirstC = StringLeft( $Data, 1 )
                                $LastC = StringRight( $Data, 1 )
                                If $FirstC <> $FirstR OR $LastC <> $LastR Then
                                    IniWrite( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]], $i, _StringToHex( $Data ) )
                                EndIf                               
                            EndIf
                        Else
                            Msgbox( 0, "ERROR", "There was a problem reading the file, ending download process" )
                            $i = $Pieces + 1
                        EndIf
                        GuiCtrlSetData( $Progress, ($i/$Pieces)*100 )
                        GuiCtrLSetData( $ProgressAm, $i & " of " & $Pieces & " pieces" )
                    Next
                    FileClose( $FileHandle )
                    RunWait( @ScriptDir & "\" & "unpacker.exe " & chr(34) & $FileName[$FileName[0]] & Chr(34))
                    msgbox(0,"Complete", "The file was unpacked")
                
                Else
                    $Responce = Msgbox( 4, "ERROR", "There is a conflict with the file, there is already an enrty for it but the file sizes are different. Do you want to delete the entry?")
                    If $Responce = 6 Then
                        IniDelete ( @ScriptDir & "\" & "FileTracker", $FileName[$FileName[0]] )
                        ControlClick( "File Cut V: " & $Version, "", "Button5" )
                    EndIf
                EndIf
            Else    
                Msgbox(0,"Error", "An unknown error occured while checking the file")
                Exit
            EndIf
        EndIf
        
        GuiCtrlSetState( $Location, $GUI_ENABLE )
        GuiCtrlSetState( $Find, $GUI_ENABLE )
        GuiCtrlSetData( $Start, "Start" )
    EndSelect
WEnd

heres the unpacker.au3:

#include <String.au3>
If $CMDLine[0] > 0 Then
    $NumPieces = INIRead( @ScriptDir & "\" & "FileTracker", $CMDLine[1], "NumPieces", "ERR")
    If $NumPieces <> "ERR" Then
        $FileHandle = FileOpen( @ScriptDir & "\" & $CMDLine[1], 2 )
        For $i = 1 to $NumPieces
            $Data = IniRead( @ScriptDir & "\" & "FileTracker", $CMDLine[1], $i, "ERR" )
            If $Data  <> "ERR" Then
                FileWrite( $FileHandle, _HexToString( $Data ) )
            Else
                FileClose( $FileHandle )
                FileDelete( @ScriptDir & "\" & $CMDLine[1] )
            EndIf
        Next
        FileClose( $FileHandle )
        IniDelete ( @ScriptDir & "\" & "FileTracker", $CMDLine[1] )
    Else
    EndIf
EndIf

ok, if i open a music file, it works find, if i open an html page it works fine, but if i open a movie, which is 1g+, then i get an error of unable to allocate memory, but I don't know anything of how the autoit read/open commands work other then whats in the help file. also does anyone know anyway to start reading a file from a specific character? right now i have it just checking what it has and only writing it if its different but i do size checking so they shouldn't be that different, but as far as i can tell you can't start reading from character 500 or what have you. maybe someone has some cool dll's to make this all work like magic but thats an area i know nothing about :\, thanks guys

~Dark

Edited by DarkNecromancer
Link to comment
Share on other sites

  • Moderators

There was something similar just posted not to long ago about large files and FileCopy.

Specific Character - you could use StringInString() to find the 'special character'. Reading from a Character number you could look at StringTrimLeft(String, 500) that would hold the rest of the string in your variable.

I have to run (time to play Santa Clause (skinnier version :P) for the kids at the hospital, but if you haven't found the topic on File Size and FileCopy by then, I'll see if I can hunt it down...

Edit:

I found the Topic, but I don't think it will help you 'Pick up where you left off', but it may give a few other 'debug' ideas: http://www.autoitscript.com/forum/index.ph...855&hl=FileCopy

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...